#include "locmin.h"
#include <bits/stdc++.h>
using namespace std;
vector <vector <int>> v;
int n, m;
int dx[] = {1, -1, 0, 0}, dy[] = {0, 0, 1, -1};
int get(int x, int y) {
    if (v[x][y] == -1) v[x][y] = value(x, y);
    return v[x][y];
}
bool ok = 0, rx, ry;
void dfs(int x, int y) {
    if (ok) return;
    int c = 0;
    for (int z = 0; z < 4; z++) {
        int i = x + dx[z], j = y + dy[z];
        if (i <= 0 || j <= 0 || i > n || j > m) continue;
        if (get(i, j) < get(x, y)) dfs(i, j), c++;
    }
    if (c == 0) {
        ok = 1;
        rx = x, ry = y;
    }
}
pair <int, int> find_locmin_point(int N, int M) {
    n = N, m = M;
    v.assign(N+1, vector <int>(M+1, -1));
    v[1][1] = value(1, 1);
    dfs(1, 1);
    return {rx,ry};
}
