#include "locmin.h"
#include <utility>
#define ll long long
int n, m, i, j;
ll a[501][501], viz[501][501];
ll get_value(int lin, int col){ /// own function for memory saving
    if(!a[lin][col]){
        a[lin][col] = value(lin, col);
    }
    return a[lin][col];
}
bool check(int lin, int col){
    ll cur = get_value(lin, col);
    if((lin > 1 && get_value(lin - 1, col) < cur) ||
        (lin < n && get_value(lin + 1, col) < cur) ||
        (col > 1 && get_value(lin, col - 1) < cur) ||
        (col < m && get_value (lin, col + 1) < cur)
       )
    {
        return false;
    }
    return true;
}
std::pair <int, int> find_locmin_point(int N, int M) {
    int rezl = 0, rezc = 0;
    n = N;
    m = M;
    for(i = 1;i<=n;i++){
        for(j = 1;j<=m;j++){
            if(check(i, j)){
                rezl = i, rezc = j;
                break;
            }
        }
    }
    return {rezl, rezc};
}

