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

vector<pair<int, int>> delta = {{-1, 0}, {1, 0}, {0, 1}, {0, -1}};

int n, m;
vector<vector<int>> g;

bool ok(int i, int j) {
    for (auto [pi, pj]: delta) {
        if (i + pi <= 0 || i + pi > n || j + pj <= 0 || j + pj > m) continue;
        if (g[i + pi][j + pj] < g[i][j]) return false;
    }
    return true;
}

pair<int, int> find_locmin_point(int N, int M) {
    n = N;
    m = M;
    g.assign(n+1, vector<int>(m+1, 0));
    for (int i = 1; i <= n; i++) {
        for (int j = 1; j <= m; j++) {
            g[i][j] = value(i, j);
        }
    }
    for (int i = 1; i <= n; i++) {
        for (int j = 1; j <= m; j++) {
            if (ok(i, j)) return {i, j};
        }
    }
    assert(false);
    return {-1, -1};
}
