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

typedef long long LL;

LL p[511][511];
int nn, mm;

LL getval(int i, int j) {
    if(i >= 1 && i <= nn && j >= 1 && j <= mm) {
        if(p[i][j] > 0) return p[i][j];
        return p[i][j] = value(i, j);
    }
    return p[i][j] = LLONG_MAX;
}

bool sed(int i, int j) {
    if(getval(i - 1, j) > getval(i, j) &&
               getval(i + 1, j) > getval(i, j) &&
               getval(i, j - 1) > getval(i, j) &&
               getval(i, j + 1) > getval(i, j) ) {
        return true;
    }
    return false;
}

pair<int, int> find_locmin_point(int N, int M) {
    int i = 1;
    nn = N, mm = M;
    deque<pair<int, int>> bfs;
    bfs.push_back({1, M});
    while(1) {
        auto [qa, qb] = bfs.front();
        bfs.pop_front();
        int mid = (qa + qb) >> 1;
        if(sed(qa, qb)) {
            return {qa, qb};
        }
        bfs.push_back({qa, mid});
         bfs.push_back({mid + 1, qb});
    }

}









