#include<iostream>
using namespace std;
const int NMAX = 500;
long long int value(int row, int col);
long long val[NMAX + 5][NMAX + 5];
int dx[] = {-1, 1, 0, 0};
int dy[] = {0, 0, -1, 1};
int globn, globm;
bool inbounds(int x, int y){return x >= 1 && x <= globn && y >= 1 && y <= globm;}
int fx, fy;
bool maybeGood(int x, int y)
{
    if(fx)
        return 0;
    if(!val[x][y])
        val[x][y] = value(x, y);
    bool ok = 1;
    for(int k = 0; k < 4; k++)
        if(!(!val[x + dx[k]][y + dy[k]] || (val[x][y] < val[x + dx[k]][y + dy[k]])))
            ok = 0;
    return ok;
}
void dfs(int x, int y)
{
    if(fx)
        return;
    bool ok = 1;
    for(int k = 0; k < 4; k++)
    {
        if(!inbounds(x + dx[k], y + dy[k]))
            continue;
        if(!val[x + dx[k]][y + dy[k]])
            val[x + dx[k]][y + dy[k]] = value(x + dx[k], y + dy[k]);
        if(val[x + dx[k]][y + dy[k]] < val[x][y])
        {
            if(maybeGood(x + dx[k], y + dy[k]))
                dfs(x + dx[k], y + dy[k]);
            ok = 0;
            break;
        }
    }
    if(ok)
        fx = x, fy = y;
}
pair<int, int> find_locmin_point(int n, int m)
{
    globn = n, globm = m;
    for(int i = 1; i <= n; i++)
        for(int j = 1; j <= m; j++)
            if(maybeGood(i, j))
                dfs(i, j);
    return {fx, fy};
}