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

std::pair <int, int> find_locmin_point(int N, int M) {
    vector<vector<long long>> board(N, vector<long long>(M, INT_MIN));
    vector<vector<bool>> maybe(N, vector<bool>(M, true));

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

            if (board[i][j] == INT_MIN) {
                board[i][j] = value(i + 1, j + 1);
            }

            // board[i + 1][j]
            // board[i - 1][j]
            // board[i][j + 1]
            // board[i][j - 1]

            if (i + 1 >= N) {;}
            else if (board[i + 1][j] == INT_MIN) {;}
            else if (board[i + 1][j] <= board[i][j]) {
                maybe[i][j] = false; continue;
            }

            if (i - 1 < 0) {;}
            else if (board[i - 1][j] == INT_MIN) {;}
            else if (board[i - 1][j] <= board[i][j]) {
                maybe[i][j] = false; continue;
            }

            if (j + 1 >= M) {;}
            else if (board[i][j + 1] == INT_MIN) {;}
            else if (board[i][j + 1] <= board[i][j]) {
                maybe[i][j] = false; continue;
            }

            if (j - 1 < 0) {;}
            else if (board[i][j - 1] == INT_MIN) {;}
            else if (board[i][j - 1] <= board[i][j]) {
                maybe[i][j] = false; continue;
            }

            if (i + 1 >= N) {;}
            else if (board[i + 1][j] == INT_MIN) {
                board[i + 1][j] = value(i + 1 + 1, j + 1);
                if (board[i + 1][j] <= board[i][j]) {
                    maybe[i][j] = false; continue;
                }
            }

            if (i - 1 < 0) {;}
            else if (board[i - 1][j] == INT_MIN) {
                board[i - 1][j] = value(i - 1 + 1, j + 1);
                if (board[i - 1][j] <= board[i][j]) {
                    maybe[i][j] = false; continue;
                }
            }

            if (j + 1 >= M) {;}
            else if (board[i][j + 1] == INT_MIN) {
                board[i][j + 1] = value(i + 1, j + 1 + 1);
                if (board[i][j + 1] <= board[i][j]) {
                    maybe[i][j] = false; continue;
                }
            }

            if (j - 1 < 0) {;}
            else if (board[i][j - 1] == INT_MIN) {
                board[i][j - 1] = value(i + 1, j - 1 + 1);
                if (board[i][j - 1] <= board[i][j]) {
                    maybe[i][j] = false; continue;
                }
            }

            return {i + 1, j + 1};
        }
    }


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

            if (board[i][j] == INT_MIN) {
                board[i][j] = value(i + 1, j + 1);
            }

            // board[i + 1][j]
            // board[i - 1][j]
            // board[i][j + 1]
            // board[i][j - 1]

            if (i + 1 >= N) {;}
            else if (board[i + 1][j] == INT_MIN) {;}
            else if (board[i + 1][j] <= board[i][j]) {
                maybe[i][j] = false; continue;
            }

            if (i - 1 < 0) {;}
            else if (board[i - 1][j] == INT_MIN) {;}
            else if (board[i - 1][j] <= board[i][j]) {
                maybe[i][j] = false; continue;
            }

            if (j + 1 >= M) {;}
            else if (board[i][j + 1] == INT_MIN) {;}
            else if (board[i][j + 1] <= board[i][j]) {
                maybe[i][j] = false; continue;
            }

            if (j - 1 < 0) {;}
            else if (board[i][j - 1] == INT_MIN) {;}
            else if (board[i][j - 1] <= board[i][j]) {
                maybe[i][j] = false; continue;
            }

            if (i + 1 >= N) {;}
            else if (board[i + 1][j] == INT_MIN) {
                board[i + 1][j] = value(i + 1 + 1, j + 1);
                if (board[i + 1][j] <= board[i][j]) {
                    maybe[i][j] = false; continue;
                }
            }

            if (i - 1 < 0) {;}
            else if (board[i - 1][j] == INT_MIN) {
                board[i - 1][j] = value(i - 1 + 1, j + 1);
                if (board[i - 1][j] <= board[i][j]) {
                    maybe[i][j] = false; continue;
                }
            }

            if (j + 1 >= M) {;}
            else if (board[i][j + 1] == INT_MIN) {
                board[i][j + 1] = value(i + 1, j + 1 + 1);
                if (board[i][j + 1] <= board[i][j]) {
                    maybe[i][j] = false; continue;
                }
            }

            if (j - 1 < 0) {;}
            else if (board[i][j - 1] == INT_MIN) {
                board[i][j - 1] = value(i + 1, j - 1 + 1);
                if (board[i][j - 1] <= board[i][j]) {
                    maybe[i][j] = false; continue;
                }
            }

            return {i + 1, j + 1};
        }
    }

    return {-1, -1};
}
