#include<iostream>
#include<algorithm>
#include<vector>
#include<cmath>
#include<math.h>
#include<string>
#include <set>
#include<unordered_map>
#include<map>
#include<numeric>
#include<unordered_set>
#include<bit>
#include<iomanip>
#include<cstdint>
#include<sstream>
#include<queue>
#include<cstring>
#include<stack>
#include<functional>
#include<random>
#include<chrono>
#include<ctime>
#include<stdio.h>
#include<bitset>
#include<array>
#include<deque>


using namespace std;



//#define int long long
#define ll long long
#define pll pair<ll, ll>
#define ull unsigned long long
#define ui unsigned int
#define db long double
#define ld long double
#define for0(i, n) for (int i = 0; i < (n); ++i)
#define for1(i, n) for (int i = 1; i < (n); ++i)
#define rfor0(i, n) for (int i = (n) - 1; i >= 0; --i)
#define rfor1(i, n) for (int i = (n) - 1; i >= 1; --i)
#define cin1(a) for(int i=1;i<a.size();i++) cin>>a[i];
#define cin(a) for(auto &now:a) cin>>now;
#define all(a) a.begin(), a.end()
#define allr(a) a.rbegin(), a.rend()
#define chmin(a,b) a = min(a,b)
#define chmax(a,b) a = max(a,b)
#define print(a, s) for(auto i:a) cout<<i<<s;
#define ve vector
#define pii pair<int,int>
#define tiii tuple<int,int,int>
#define tiiii tuple<int,int,int,int>
#define tiiiii tuple<int, int, int, int, int>
#define vi vector<int>
#define vll ve<ll>
#define vvi vector<vi>
#define ret return
#define LOCAL

ll get_value(int r, int c, vvi & mt) {
    return mt[r][c] == -1 ? mt[r][c] = value(r, c) : mt[r][c];
}
pair <int, int> find_locmin_point(int n, int m) {
    vvi mt(n + 1, vi(m + 1, -1));
    int lx = 1, rx = n, ly = 1, ry = m;
    while (lx < rx || ly < ry) {
        if (lx < rx) {
            int mx = (lx + rx) / 2;
            pll mn = { 1e18, -1 };
            for (int j = ly; j <= ry; ++j) {
                chmin(mn, pll(get_value(mx, j, mt), 0ll));
                chmin(mn, pll(get_value(mx + 1, j, mt), 1ll));
            }
            if (mn.second == 0) rx = mx;
            else lx = mx + 1;
        }
        if (ly < ry) {
            int my = (ly + ry) / 2;
            pll mn = { 1e18, -1 };
            for (int i = lx; i <= rx; ++i) {
                chmin(mn, pll(get_value(i, my, mt), 0ll));
                chmin(mn, pll(get_value(i, my + 1, mt), 1ll));
            }
            if (mn.second == 0) ry = my;
            else ly = my + 1;
        }
    }
    return { lx, ly };
}
