#include "locmin.h"
#include <bits/stdc++.h>

using namespace std;

typedef long long ll;

const int MAXN = 505;

ll done[MAXN][MAXN];

ll query(int r, int c)
{
    if (r < 1 or r >= MAXN or c < 1 or c >= MAXN)
        return LLONG_MAX;
    if (done[r][c] != -1)
        return done[r][c];
    return done[r][c] = value(r, c);
}

std::pair<int, int> find_locmin_point(int n, int m)
{
    for (int i = 0; i < MAXN; i++)
        for (int j = 0; j < MAXN; j++)
            done[i][j] = -1;

    int low = 1, high = m;

    while (low <= high)
    {
        int mid = (low + high) / 2;

        int row = 1;
        for (int i = 2; i <= n; i++)
            if (query(i, mid) < query(row, mid))
                row = i;

        ll curr = query(row, mid), l = query(row, mid - 1), r = query(row, mid + 1);

        if (curr < l and curr < r)
            return {row, mid};

        if (l < curr)
            high = mid - 1;
        else
            low = mid + 1;
    }

    return {0, 0};
}