#include "locmin.h"
#include <map>
using namespace std;

map<pair<int,int>, long long> mp;
const long long INF = (long long)4e18;

long long val(int r,int c){
    if(mp.count({r,c})) return mp[{r,c}];
    return mp[{r,c}] = value(r,c);
}

pair<int,int> find_locmin_point(int N,int M){
    int h1=1,h2=N;
    int g1=1,g2=M;

    while(true){

        if((h2-h1) > (g2-g1)){
            int mid=(h1+h2)/2;

            int bc=1;
            long long bv=val(mid,1);

            for(int j=2;j<=M;j++){
                long long v=val(mid,j);
                if(v<bv){bv=v;bc=j;}
            }

            long long vg=(mid>1)?val(mid-1,bc):INF;
            long long vd=(mid<N)?val(mid+1,bc):INF;

            if(bv<vg && bv<vd) return {mid,bc};

            if(vg<bv) h2=mid-1;
            else h1=mid+1;
        }
        else{
            int mid=(g1+g2)/2;

            int br=1;
            long long bv=val(1,mid);

            for(int i=2;i<=N;i++){
                long long v=val(i,mid);
                if(v<bv){bv=v;br=i;}
            }

            long long vg=(mid>1)?val(br,mid-1):INF;
            long long vd=(mid<M)?val(br,mid+1):INF;

            if(bv<vg && bv<vd) return {br,mid};

            if(vg<bv) g2=mid-1;
            else g1=mid+1;
        }
    }
}