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

// Provided by the judge
long long value(int r, int c);

namespace {
    int N_global, M_global;
    map<pair<int,int>, long long> memo;
    mt19937 rng((uint32_t)chrono::steady_clock::now().time_since_epoch().count());
    const long long INF = (long long)4e18;

    long long getVal(int r, int c) {
        if (r < 1 || r > N_global || c < 1 || c > M_global) return INF;

        pair<int,int> p = {r, c};
        auto it = memo.find(p);
        if (it != memo.end()) return it->second;

        long long v = value(r, c);
        memo[p] = v;
        return v;
    }

    bool isLocalMin(int r, int c) {
        long long cur = getVal(r, c);
        return cur < getVal(r - 1, c) &&
               cur < getVal(r + 1, c) &&
               cur < getVal(r, c - 1) &&
               cur < getVal(r, c + 1);
    }

    pair<int,int> bestNeighbor(int r, int c) {
        pair<int,int> best = {r, c};
        long long bestVal = getVal(r, c);

        const int dr[4] = {-1, 1, 0, 0};
        const int dc[4] = {0, 0, -1, 1};

        for (int k = 0; k < 4; k++) {
            int nr = r + dr[k];
            int nc = c + dc[k];
            long long v = getVal(nr, nc);
            if (v < bestVal) {
                bestVal = v;
                best = {nr, nc};
            }
        }

        return best;
    }
}

pair<int, int> find_locmin_point(int N, int M) {
    N_global = N;
    M_global = M;
    memo.clear();

    int samples = min(200, N * M);

    pair<int,int> start = {1, 1};
    long long bestSample = INF;

    unordered_set<long long> used;
    used.reserve(samples * 2);

    while ((int)used.size() < samples) {
        int r = uniform_int_distribution<int>(1, N)(rng);
        int c = uniform_int_distribution<int>(1, M)(rng);

        long long key = 1LL * r * (M + 1) + c;
        if (used.count(key)) continue;
        used.insert(key);

        long long v = getVal(r, c);
        if (v < bestSample) {
            bestSample = v;
            start = {r, c};
        }
    }

    pair<int,int> cur = start;

    while (!isLocalMin(cur.first, cur.second)) {
        pair<int,int> nxt = bestNeighbor(cur.first, cur.second);

        // Safety: if for some reason no smaller neighbor is found, stop here.
        if (nxt == cur) break;

        cur = nxt;
    }

    return cur;
}