#include <bits/stdc++.h>
#include "locmin.h"
#define ll long long
using namespace std;
int di[4] = {+0, -1, +0, +1};
int dj[4] = {+1, +0, -1, +0};
pair <int, int> DFS(int i, int j)
{
  for(int k = 0; k < 4; k++)
  {
    if((A[i + di[k]][j + dj[k]]) > 0) continue;
    A[i + di[k]][j + dj[k]] = value(i + di[k], j + dj[k]);
    if(A[i + di[k]][j + dj[k]] > A[i][j]) continue;
    return DFS(i + di[k], j + dj[k]);
  }
  return {i, j};
}
pair <int, int> find_locmin_point(int N, int M)
{
  ll A[N + 2][M + 2];
  __builtin_memset(A, -1, sizeof(A));
  for(int i = 0; i <= (N + 1); i++) A[i][0] = A[i][M + 1] = 1000000000000000001LL;
  for(int j = 0; j <= (M + 1); j++) A[0][j] = A[N + 1][j] = 1000000000000000001LL;
  A[1][1] = value(1, 1);
  return DFS(1, 1);
}
