#include "bits/stdc++.h"
#include "locmin.h"
// #include "grader.cpp"
using namespace std;
#define pii pair<int, int>
#define ll long long
#define wr puts("---------------")
const int N = 505;
int dx[]={0, 0, -1, 1}, dy[]={1, -1, 0, 0};
ll val[N][N];
int sz, sz2;

ll ask(int x, int y){
    if(val[x][y]==0)
        val[x][y]=value(x, y);
    return val[x][y];
}

bool is(int x, int y){
    return (x>=1 and x<=sz and y>=1 and y<=sz2);
}

bool can(int x, int y){
    ll now=ask(x, y);
    for(int ad = 0; ad < 4; ++ad)
        if(is(x+dx[ad], y+dy[ad]))
            if(ask(x+dx[ad], y+dy[ad])<now)
                return 0;
    return 1;
}

pii find_locmin_point(int N, int M){
    sz=N, sz2=M;
    int r1=1, r2=N, c1=1, c2=M;
    while(1){
        // wr;
        // printf("%d %d %d %d\n", r1, r2, c1, c2);
        int area=(r2-r1+1)*(c2-c1+1);
        if(area<=4){
            for(int i = r1; i <= r2; ++i)
                for(int j = c1; j <= c2; ++j)
                    if(can(i, j))
                        return {i, j};
        }
        if(r2-r1>=c2-c1){
            int mid=(r1+r2)>>1;
            int id=-1;
            ll mn=1e18+1;
            for(int j = c1; j <= c2; ++j)
                if(mn>ask(mid, j))
                    mn=ask(mid, j), id=j;
            if(can(mid, id))
                return {mid, id};
            else if(is(mid-1, id) and ask(mid-1, id)<ask(mid, id))
                r2=mid-1;
            else
                r1=mid+1;
            continue;
        }
        // wr;
        int mid=(c1+c2)>>1;
        int id=-1;
        ll mn=1e18+1;
        for(int i = r1; i <= r2; ++i)
            if(mn>ask(i, mid))
                mn=ask(i, mid), id=i;
        if(can(id, mid))
            return {id, mid};
        else if(is(id, mid-1) and ask(id, mid-1)<ask(id, mid))
            c2=mid-1;
        else
            c1=mid+1;
    }
}