#include <vector>
#include <utility>
#include <climits>
#include <algorithm>

std::pair<int, int> find_locmin_point(int N, int M, const std::vector<std::vector<int>>& matrix) {
    auto is_local_min = [&](int i, int j) {
        int cur = matrix[i][j];
        if ((i > 0 && matrix[i-1][j] < cur) ||
            (i < N-1 && matrix[i+1][j] < cur) ||
            (j > 0 && matrix[i][j-1] < cur) ||
            (j < M-1 && matrix[i][j+1] < cur)) {
            return false;
        }
        return true;
    };

    auto find_min = [&](auto&& find_min, int r1, int r2, int c1, int c2) -> std::pair<int, int> {
        if (r1 > r2 || c1 > c2) return {-1, -1};

        int m = r1 + (r2 - r1) / 2;
        int mc = c1;
        int mv = matrix[m][c1];

        for (int j = c1 + 1; j <= c2; ++j) {
            if (matrix[m][j] < mv) {
                mv = matrix[m][j];
                mc = j;
            }
        }

        if (is_local_min(m, mc)) return {m, mc};

        int l = (mc > c1) ? matrix[m][mc-1] : INT_MAX;
        int r = (mc < c2) ? matrix[m][mc+1] : INT_MAX;
        int u = (m > r1) ? matrix[m-1][mc] : INT_MAX;
        int d = (m < r2) ? matrix[m+1][mc] : INT_MAX;

        int mn = std::min({l, r, u, d});

        if (mn == l) return find_min(find_min, r1, r2, c1, mc - 1);
        if (mn == r) return find_min(find_min, r1, r2, mc + 1, c2);
        if (mn == u) return find_min(find_min, r1, m - 1, c1, c2);
        return find_min(find_min, m + 1, r2, c1, c2);
    };

    return find_min(find_min, 0, N - 1, 0, M - 1);
}