#include "locmin.h"
#include <bits/stdc++.h>
const int MAXN = 500;

long long mat[MAXN + 2][MAXN + 2];
int dl[] = { 0, -1, 1, 0 };
int dc[] = { 1, 0, 0, -1 };

bool verif( int i, int j ) {
    int cnt = 0;
    for( int dir = 0; dir < 4; dir++ )
        if( mat[i + dl[dir]][j + dc[dir]] >= mat[i][j] )
            cnt++;
    return cnt == 4;
}

std::pair <int, int> find_locmin_point(int N, int M) {
    int i, j, n, m;
    n = N;
    m = M;
    if( n == 1 && m == 1 )
        return { 1, 1 };

    for( i = 1; i <= n; i++ )
        mat[i][0] = mat[i][m + 1] = LLONG_MAX;

    for( j = 1; j <= m; j++ )
        mat[0][j] = mat[n + 1][j] = LLONG_MAX;

    i = 1;
    for( j = 1; j <= m; j++ ) {
        mat[i][j] = value( i, j );
        if( j >= 2 )
            if( verif( i, j - 1 ) )
                return { i, j - 1 };
    }

    return { 1, m };

    //for( i = 1; i <= n; i++ )
    //    for( j = 1; j <= m; j++ )
    //        if( verif( i, j ) )
    //            return { i, j };
}
