#include<iostream>
#include <set>
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 checked[NMAX + 5][NMAX + 5];
set<pair<int, int>> st;
int cntn[NMAX + 5][NMAX + 5];
long long getValue(int x, int y)
{
    st.erase({cntn[x][y], (x - 1) * globm + y});
    cntn[x][y]--;
    st.insert({cntn[x][y], (x - 1) * globm + y});
    for(int k = 0; k < 4; k++)
        if(inbounds(x + dx[k], y + dy[k]) && !checked[x + dx[k]][y + dy[k]])
        {
            st.erase({cntn[x + dx[k]][y + dy[k]], (x + dx[k] - 1) * globm + y + dy[k]});
            cntn[x + dx[k]][y + dy[k]]--;
            st.insert({cntn[x + dx[k]][y + dy[k]], (x + dx[k] - 1) * globm + y + dy[k]});
        }
    return value(x, y);
}
bool maybeGood(int x, int y)
{
    if(fx)
        return 0;
    if(!val[x][y])
        val[x][y] = getValue(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;
}
//Faci cu un set dupa numarul de vecini descoperiti si il iei mereu 
//pe ala cu cei mai multi vecini descoperiti.
void dfs(int x, int y)
{
    if(fx)
        return;
    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]] = getValue(x + dx[k], y + dy[k]);
        if(val[x + dx[k]][y + dy[k]] < val[x][y])
            return;
    }
    fx = x, fy = y;
}
int cntneighb(int x, int y)
{
    int cnt = 1;
    for(int k = 0; k < 4; k++)
        if(inbounds(x + dx[k], y + dy[k]))
            cnt++;
    return cnt;
}
pair<int, int> find_locmin_point(int n, int m)
{
    for(int i = 1; i <= n; i++)
        for(int j = 1; j <= m; j++)
            st.insert({cntneighb(i, j), (i - 1) * m + j}), cntn[i][j] = cntneighb(i, j);
    globn = n, globm = m;
    while(!fx && !st.empty())
    {
        int poz = (*st.begin()).second;
        st.erase(st.begin());
        int lin = poz / m + (poz % m != 0);
        int col = (poz % m == 0 ? m : poz % m);
        checked[lin][col] = 1;
        if(maybeGood(lin, col))
            dfs(lin, col);
    }
    return {fx, fy};
}