#include "locmin.h"
#include <bits/stdc++.h>
using namespace std;
const long long inf = 2e18;
pair <int, int> find_locmin_point(int n, int m){
    vector<int> dx = {0, 1, 0, -1}, dy = {1, 0, -1, 0};
    vector<vector<long long>> p(n + 1, vector<long long>(m + 1));
    auto get = [&](int x, int y){
        if(x < 1 || x > n || y < 1 || y > m) return inf;
        if(!p[x][y]) p[x][y] = value(x, y);
        return p[x][y];
    };
    int l1 = 1, l2 = n, r1 = 1, r2 = m, t = 0;
    while(1){
        if(!t){
            int mid = (r1 + r2) / 2;
            long long mn = inf, x, y;
            for(int i = l1; i <= l2; i++){
                if(get(i, mid) < mn){
                    mn = get(i, mid);
                    x = i; y = mid;
                }
            }
            if(get(x, y) < min(get(x, y - 1), get(x, y + 1))) return {x, y};
            else if(get(x, y) > get(x, y - 1)) r2 = mid;
            else r1 = mid + 1;
            if(l2 - l1 > r2 - r1) t ^= 1;
        }else{
            int mid = (l1 + l2) / 2;
            long long mn = inf, x, y;
            for(int i = r1; i <= r2; i++){
                if(get(mid, i) < mn){
                    mn = get(mid, i);
                    x = mid; y = i;
                }
            }
            if(get(x, y) < min(get(x - 1, y), get(x + 1, y))) return {x, y};
            else if(get(x, y) > get(x - 1, y)) l2 = mid;
            else l1 = mid + 1;
            if(r2 - r1 > l2 - l1) t ^= 1;
        }
    }
}
