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

using namespace std;

typedef long long ll;

const int MAXN = 505;

ll done[MAXN][MAXN];
int N, M;

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

pair<int, int> solve(int top, int bot, int left, int right)
{
    int mid = (left + right) / 2;

    int row = top;
    for (int i = top + 1; i < bot; 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)
        return solve(top, bot, left, mid - 1);
    else
        return solve(top, bot, mid + 1, right);
}

std::pair<int, int> find_locmin_point(int n, int m)
{
    N = n, M = m;

    for (int i = 0; i < MAXN; i++)
        for (int j = 0; j < MAXN; j++)
            done[i][j] = -1;

    return solve(1, N, 1, M);
}