#include <bits/stdc++.h>
#include "locmin.h"

long long inf = 1e18;
using namespace std;

long long mapa[505][505];
bool vis[505][505];
int n,m;

long long findV(pair<int,int> x)
{
    if(x.first < 1 || x.second < 1 || x.first > n || x.second > m) return inf;
    if(vis[x.first][x.second]) return mapa[x.first][x.second];
    vis[x.first][x.second] = 1;
    return mapa[x.first][x.second] = value(x.first, x.second);
}

pair<int,int> solve(int a1, int a2, int b1, int b2)
{
    if(a1 == a2 && b1 == b2) return {a1, b1};

    int mida = a1 + a2 >> 1;
    int midb = b1 + b2 >> 1;

    long long mn = inf;
    int tl = -1, tr = -1;

    if(a2 - a1 > b2 - b1)
    {
        for(int i = a1; i <= a2; i++)
        {
            long long v = findV({i, midb});
            if(v < mn)
            {
                mn = v;
                tl = i;
                tr = midb;
            }
        }
    }
    else
    {
        for(int i = b1; i <= b2; i++)
        {
            long long v = findV({mida, i});
            if(v < mn)
            {
                mn = v;
                tl = mida;
                tr = i;
            }
        }
    }

    long long v = findV({tl, tr});
    long long gore = findV({tl-1, tr});
    long long dole = findV({tl+1, tr});
    long long levo = findV({tl, tr-1});
    long long desno = findV({tl, tr+1});

    if(v < levo && v < desno && v < gore && v < dole)
        return {tl, tr};

    if(gore < v)
    {
        if(tr <= midb) return solve(a1, mida-1, b1, midb);
        else return solve(a1, mida-1, midb+1, b2);
    }
    if(dole < v)
    {
        if(tr <= midb) return solve(mida+1, a2, b1, midb);
        else return solve(mida+1, a2, midb+1, b2);
    }
    if(levo < v)
    {
        if(tl <= mida) return solve(a1, mida, b1, midb-1);
        else return solve(mida+1, a2, b1, midb-1);
    }
    if(desno < v)
    {
        if(tl <= mida) return solve(a1, mida, midb+1, b2);
        else return solve(mida+1, a2, midb+1, b2);
    }

    return {tl, tr};
}

std::pair <int, int> find_locmin_point(int N, int M)
{
    n = N;
    m = M;
    return solve(1, n, 1, m);
}
