#include <vector>
#include <queue>

long long value(int r, int c);
std::pair<int, int> find_locmin_point(int N, int M) {
    std::vector<int> values(N * M, 0);
    const auto v = [N, M, &values] (int r, int c) {
        if (r < 0 || c < 0 || r >= N || c >= M) exit(-1);
        if (!values[r * M + c]) values[r * M + c] = value(r + 1, c + 1);
        return values[r * M + c];
    };

    int r = 0, c = 0;
    while (true) {
        if (r > 0 && v(r - 1, c) < v(r, c)) { r--; continue; }
        else if (c > 0 && v(r, c - 1) < v(r, c)) { c--; continue; }
        else if (r + 1 < N && v(r + 1, c) < v(r, c)) { r++; continue; }
        else if (c + 1 < M && v(r, c + 1) < v(r, c)) { c++; continue; }
        return { r + 1, c + 1 };
    }
}
