#include "bits/stdc++.h"
#include "locmin.h"
// #include "grader.cpp"

using namespace std;

#define ll long long

vector <vector <ll>> a(501, vector <ll> (501, -1LL));

int NN, MM;

ll b(int x, int y) {
    if(!x or !y or x > NN or y > MM) return 1e18+1;
    if(~a[x][y]) return a[x][y];
    return a[x][y] = value(x, y);
}

pair <int, int> f(int x, int y) {
    if(b(x-1, y) < a[x][y]) return f(x-1, y);
    if(b(x+1, y) < a[x][y]) return f(x+1, y);
    if(b(x, y-1) < a[x][y]) return f(x, y-1);
    if(b(x, y+1) < a[x][y]) return f(x, y+1);
    return {x, y};
}

pair <int, int> find_locmin_point(int N, int M) {
    NN = N, MM = M;
    int x = (NN + 1) / 2, y = (MM + 1) / 2;
    a[x][y] = value(x, y);
    return f(x, y);
}
