#include <bits/stdc++.h>
#define MAXN 500
#define fi first
#define se second
#include "locmin.h"
using namespace std;
long long val[MAXN+5][MAXN+5];
stack<pair<int, int>>q;
pair<int, int> dfs(int r, int c, int n, int m) {
    int nr=0;
    if (r>1) {
        if (val[r-1][c]==0) val[r-1][c]=value(r-1, c);
        if (val[r][c]>val[r-1][c])  q.push({r-1, c});
        else nr++;
    }
    if (r<n) {
        if (val[r+1][c]==0) val[r+1][c]=value(r+1, c);
        if (val[r][c]>val[r+1][c]) q.push({r+1, c});
        else nr++;
    }
    if (c>1) {
        if (val[r][c-1]==0) val[r][c-1]=value(r, c-1);
        if (val[r][c]>val[r][c-1]) q.push({r, c-1});
        else nr++;
    }
    if (c<m) {
        if (val[r][c+1]==0) val[r][c+1]=value(r, c+1);
        if (val[r][c]>val[r][c+1]) q.push({r, c+1});
        else nr++;
    }
    if (nr==4) return make_pair(r, c);
    auto x=q.top();
    q.pop();
    r=x.fi;
    c=x.se;
    q.pop();
    return dfs(r, c, n, m);
}

pair<int, int> find_locmin_point(int N, int M) {
    int r=(N+1)/2;
    int c=(M+1)/2;
    val[r][c]=value(r, c);
    return dfs(r, c, N , M);
}
