#include "locmin.h"
#include <bits/stdc++.h>
using namespace std;
pair <int, int> find_locmin_point(int n, int m){
    int x = 1, y = 1;
    vector<int> dx = {0, 1, 0, -1}, dy = {1, 0, -1, 0};
    vector<vector<long long>> p(n + 1, vector<long long>(m + 1));
    p[1][1] = value(1, 1);
    while(1){
        int ok = 0, curx = x, cury = y;
        for(int i = 0; i < 4; i++){
            int nx = x + dx[i], ny = y + dy[i];
            if(nx < 1 || nx > n || ny < 1 || ny > m) continue;
            if(!p[nx][ny]) p[nx][ny] = value(nx, ny);
            if(p[nx][ny] < p[curx][cury]){
                curx = nx;
                cury = ny;
                ok = 1;
            }
        }
        x = curx;
        y = cury;
        if(!ok) break;
    }
    return {x, y};
}
