#include "locmin.h"
#include <iostream>
#include <algorithm>
#include <utility>
#include <vector>

int dx[] = {-1, 0, 1, 0};
int dy[] = {0, -1, 0, 1};

std::pair<int, int> find_locmin_point(int N, int M) {
  vector<vector<long long>> t(N + 1, vector<long long>(M + 1, 0));
  for ( int i = 1; i <= N; ++i ) {
    for ( int j = 1; j <= M; ++j ) {
      t[i][j] = value(i, j);
    }
  }
  auto out = [&]( int i, int j ) -> bool {
    return i < 1 || j < 1 || i > N || j > M;
  };
  for ( int i = 1; i <= N; ++i ) {
    for ( int j = 1; j <= M; ++j ) {
      bool ok = true;
      for ( int d = 0; d < 4; ++d ) {
        int ni = i + dx[d];
        int nj = j + dy[d];
        if ( !out(ni, nj) && t[i][j] >= t[ni][nj] ) {
          ok = false;
        }
      }
      if ( ok ) {
        return make_pair(i, j);
      }
    }
  }
}