#include "locmin.h"
#include "bits/stdc++.h"

using namespace std;

typedef long long ll;
typedef pair<ll, ll> pll;

map<pll, ll> ma;

mt19937_64 rn(chrono::steady_clock::now().time_since_epoch().count());

ll rnd(ll l, ll r) {
    return rn() % (r - l + 1) + l;
}

ll z(ll i, ll j) {
    if (ma.find({i, j}) == ma.end()) {
        ma[{i, j}] = value(i + 1, j + 1);
    }
    return ma[{i, j}];
}

std::pair<int, int> find_locmin_point(int n, int m) {
    vector<int> x{-1, 1, 0, 0};
    vector<int> y{0, 0, 1, -1};
    while (1) {
        int i = rnd(0, n - 1), j = rnd(0, m - 1);
        bool fl = 1;
        for (int k = 0; k < 4; k++) {
            int i1 = i + x[k], j1 = j + y[k];
            if (min(i1, j1) < 0 || i1 == n || j1 == m) continue;
            if (z(i1, j1) < z(i, j)) {
                fl = 0;
                break;
            }
        }
        if (fl) {
            return {i + 1, j + 1};
        }
    }
}
