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

vector<vector<ll>> mem;

ll val(int i, int j) {
    // cout << i << " " << j << "\n";
    if (i < 0 || i >= mem.size() || j < 0 || j >= mem[0].size()) {
        return 1000000000000000001;
    }
    if (mem[i][j] != -1) {
        return mem[i][j];
    }
    return mem[i][j] = value(i + 1, j + 1);
}

pair<int, int> find_locmin_point(int n, int m) {
    mem.assign(n, vector<ll>(m, -1));
    int i = 0, j = 0;
    while (true) {
        vector<pair<int, int>> cs = {{i - 1, j}, {i, j - 1}, {i + 1, j}, {i, j + 1}};
        pair<int, int> bs = {-1, -1};
        bool ok = true;
        for (auto [i2, j2] : cs) {
            if (val(i2, j2) < val(i, j)) {
                ok = false;
                if (val(i2, j2) < val(bs.first, bs.second)) {
                    bs = {i2, j2};
                }
            }
        }
        if (ok) {
            return {i + 1, j + 1};
        }
        i = bs.first;
        j = bs.second;
    }
}
