#include <bits/stdc++.h>

using namespace std;
using ll = long long;

mt19937 rnd(time(0));
ll value(int i, int j);

map < array < int , 2 > , ll > res;

ll val(int i, int j) {
    if (res.count({i, j})) return res[{i, j}];
    return res[{i, j}] = value(i, j);
}

pair < int , int > find_locmin_point(int n, int m) {
    res.clear();

    ll mn = (ll)4e18, oi, oj;

    for (int it = 1; it < 5000; it++) {
        int x = rnd() % n + 1, y = rnd() % m + 1;
        if (val(x, y) < mn) mn = val(x, y), oi = x, oj = y;
    }

    int x = oi, y = oj;

    while (true) {
        vector < array < int , 2 > > pt;
        if (x > 1 && val(x - 1, y) < val(x, y)) pt.push_back({x - 1, y});
        if (x < n && val(x + 1, y) < val(x, y)) pt.push_back({x + 1, y});
        if (y > 1 && val(x, y - 1) < val(x, y)) pt.push_back({x, y - 1});
        if (y < m && val(x, y + 1) < val(x, y)) pt.push_back({x, y + 1});
        if (pt.empty()) return make_pair(x, y);
        int i = rnd() % (int)pt.size();
        x = pt[i][0], y = pt[i][1];
    }

    exit(1);
}
