#include "locmin.h"
#include <bits/stdc++.h>
using namespace std;

pair<int, int> find_locmin_point(int N, int M) {
    vector<vector<int>> arr(N, vector<int>(M, -1));
    vector<vector<int>> imp(N, vector<int>(M));

    for (int i = 0; i < N; i++) {
        for (int j = 0; j < M; j++) {
            if (imp[i][j]) continue;

            if (arr[i][j] == -1) arr[i][j] = value(i+1, j+1);

            int poss = 1;
            if (i != 0) {
                if (arr[i-1][j] == -1) arr[i-1][j] = value(i, j+1);

                if (arr[i-1][j] > arr[i][j]) imp[i-1][j] = 1;
                else poss = 0;
            }
            if (i != N-1) {
                if (arr[i+1][j] == -1) arr[i+1][j] = value(i+2, j+1);

                if (arr[i+1][j] > arr[i][j]) imp[i+1][j] = 1;
                else poss = 0;
            }
            if (j != 0) {
                if (arr[i][j-1] == -1) arr[i][j-1] = value(i+1, j);

                if (arr[i][j-1] > arr[i][j]) imp[i][j-1] = 1;
                else poss = 0;
            }
            if (j != M-1) {
                if (arr[i][j+1] == -1) arr[i][j+1] = value(i+1, j+2);

                if (arr[i][j+1] > arr[i][j]) imp[i][j+1] = 1;
                else poss = 0;
            }

            if (poss) return {i+1, j+1};
        }
    }
}
