#include<iostream>
#include<random>
#include<climits>
#define MAXNN 510
#include "locmin.h"
using namespace std;
mt19937 mt(532);
long long table[MAXNN][MAXNN];
pair <int, int> find_locmin_point(int n, int m)
{
    int x=mt()%n+1,y=mt()%m+1;
    table[x][y]=value(x,y);
    while(1)
    {
        long long up=LLONG_MAX,down=LLONG_MAX,left=LLONG_MAX,right=LLONG_MAX;
        long long curr=table[x][y];
        if(x>1)
        {
            if(table[x-1][y]>0)up=table[x-1][y];
            else {up=value(x-1,y);table[x-1][y]=up;}
            if(up<=curr){x--;continue;}
        }
        if(x<n)
        {
            if(table[x+1][y]>0)down=table[x+1][y];
            else {down=value(x+1,y);table[x+1][y]=down;}
            if(down<=curr){x++;continue;}
        }
        if(y>1)
        {
            if(table[x][y-1]>0)left=table[x][y-1];
            else {left=value(x,y-1);table[x][y-1]=left;}
            if(left<=curr){y--;continue;}
        }
        if(y<m)
        {
            if(table[x][y+1]>0)right=table[x][y+1];
            else {right=value(x,y+1);table[x][y+1]=right;}
            if(right<=curr){y++;continue;}
        }
        return {x,y};
    }

    return {x, y};
}