#include "locmin.h"
#include <bits/stdc++.h>
using namespace std;

#define int long long

int a[505][505], n, m;
int cnt = 0;

int ask(int i, int j) {
    if (a[i][j]) {
        return a[i][j];
    }
    if (!(1 <= i and i <= n and 1 <= j and j <= m)) {
        return 2e18;
    }
    if (++cnt > n * m) {
        assert(false);
    }
    return a[i][j] = value(i, j);
}

pair<int, int> f(int u, int d, int l, int r) {
    if (u == d and l == r) {
        return {u, l};
    }
    if (d - u > r - l) {
        int i = (u + d) / 2;
        int j = l, val = ask(i, j);
        for (int k = l + 1; k <= r; k++) {
            if (ask(i, k) < val) {
                j = k;
                val = ask(i, k);
            }
        }
        if (ask(i - 1, j) < val) {
            return f(u, i - 1, l, r);
        } else if (ask(i + 1, j) < val) {
            return f(i + 1, d, l, r);
        } else {
            return {i, j};
        }
    } else {
        int j = (l + r) / 2;
        int i = u, val = ask(i, j);
        for (int k = u + 1; k <= d; k++) {
            if (ask(k, j) < val) {
                i = k;
                val = ask(k, j);
            }
        }
        if (ask(i, j - 1) < val) {
            return f(u, d, l, j - 1);
        } else if (ask(i, j + 1) < val) {
            return f(u, d, j + 1, r);
        } else {
            return {i, j};
        }
    }
}

#undef int

pair<int, int> find_locmin_point(int N, int M) {
    n = N, m = M;
    return f(1, n, 1, m);
}
