#include "locmin.h"
#include<bits/stdc++.h>
using namespace std;

const long long MAXN = 507, INF = 1000000000000000007;
long long n, m, ans[MAXN][MAXN];
bool vis[MAXN][MAXN];

pair<int, int> find_locmin_point(int N, int M)
{
    n = N;
    m = M;
    long long l1 = 1, r1 = n, l2 = 1, r2 = m, mid1, mid2;
    while(r1 > l1 || r2 > l2)
    {
        if(r1 > l1)
        {
            mid1 = (r1+l1)/2;
            mid2 = (r1+l1)/2+1;
            long long mini1 = INF, mini2 = INF;
            for(long long i = l2; i <= r2; i++)
            {
                long long c1, c2;
                if(vis[mid1][i]) c1 = ans[mid1][i];
                else c1 = value(mid1, i);
                vis[mid1][i] = true;
                mini1 = min(mini1, c1);
                if(vis[mid2][i]) c2 = ans[mid2][i];
                else c2 = value(mid2, i);
                vis[mid2][i] = true;
                mini2 = min(mini2, c2);
            }
            if(mini1 < mini2) r1 = mid1;
            else l1 = mid2;
        }
        if(r2 > l2)
        {
            mid1 = (r2+l2)/2;
            mid2 = (r2+l2)/2+1;
            long long mini1 = INF, mini2 = INF;
            for(long long i = l1; i <= r1; i++)
            {
                long long c1, c2;
                if(vis[i][mid1]) c1 = ans[i][mid1];
                else c1 = value(i, mid1);
                vis[i][mid1] = true;
                mini1 = min(mini1, c1);
                if(vis[i][mid2]) c2 = ans[i][mid2];
                else c2 = value(i, mid2);
                vis[i][mid2] = true;
                mini2 = min(mini2, c2);
            }
            if(mini1 < mini2) r2 = mid1;
            else l2 = mid2;
        }
    }
    return {l1, l2};
}
