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

const int NMAX = 5e2;
const int dlin[] = {-1, 1, 0, 0};
const int dcoll[] = {0, 0, -1, 1};

using namespace std;

mt19937_64 rng(time(NULL));
int64_t mat[NMAX + 5][NMAX + 5];
int n_, m_;

bool inbound(int r, int c){
    return (r >= 1 && r <= n_ && c >= 1 && c <= m_);
}

std::pair <int, int> find_locmin_point(int N, int M) {
    n_ = N, m_ = M;

    for (int i = 1; i <= n_; ++i){
        for (int j = 1; j <= m_; ++j)
            mat[i][j] = -1;
    }

    vector<pair<int, int>> cells;
    for (int i = 1; i <= n_; ++i){
        for (int j = 1; j <= m_; ++j)
            cells.emplace_back(i, j);
    }

    shuffle(cells.begin(), cells.end(), rng);
    for (auto &[i, j] : cells) {
        if (mat[i][j] == -1)
            mat[i][j] = value(i, j);

        bool ok = true;
        for (int dir = 0; dir < 4; ++dir) {
            int new_i = i + dlin[dir];
            int new_j = j + dcoll[dir];

            if (!inbound(new_i, new_j)) continue;

            if (mat[new_i][new_j] == -1)
                mat[new_i][new_j] = value(new_i, new_j);

            if (mat[i][j] >= mat[new_i][new_j])
                ok = false;
        }

        if (ok)
            return make_pair(i, j);
    }

    return make_pair(1, 1);
}

#ifndef EVAL
#include<iostream>
using namespace std;
static const int MAXN=500,MAXM=500;
static long long int tab[MAXN][MAXM];
static int n,m;
static void finish (string message) {
    cout << message << endl ;
    exit(0);
}

static int cnt=0;
long long int value (int row, int col) {
    cout << row << ' ' << col << endl;
    row--; col--;
    if ((row<0)||(row>=n)||(col<0)||(col>=m)) finish("Invalid row or column number in question ("+to_string(row)+","+to_string(col)+")!");
    cnt++;
//    if (cnt>n*m) finish("Too many questions!");
    return tab[row][col];
}
extern pair <int, int> find_locmin_point (int, int);

static int drow[]={ -1, -0, +0, +1 };
static int dcol[]={ -0, -1, +1, +0 };
static bool check_saddle (int row, int col) {
    for (int i=0; i<4; i++) {
        int r=row+drow[i],c=col+dcol[i];
        if ((r<0)||(r>=n)||(c<0)||(c>=m)) continue;
        if (tab[r][c]<=tab[row][col]) return false;
    }
    return true;
}
int main () {
    ios_base::sync_with_stdio(false);
    cin.tie(nullptr);
    cin >> n >> m ;
    for (int i=0; i<n; i++) {
        for (int j=0; j<m; j++) {
            cin >> tab[i][j] ;
        }
    }
    auto [r, c]=find_locmin_point(n,m);
    cout << "Answer cell: (" << r << "," << c << ")\n";
    cout << "Number of value calls: " << cnt << endl ;
    r--; c--;
    if ((r<0)||(r>=n)||(c<0)||(c>=m)) finish("Invalid row or column in answer!");
    if (check_saddle(r,c)==false) finish("Answer is not a local minimum point!");
    finish("Correctly found local minimum point.");
    return 0;
}

#endif