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

const int MAXN = 500;

long long mat[MAXN + 5][MAXN + 5];
int cnt[MAXN + 1][MAXN + 1];
int dl[] = { -1 , 0 , 1 , 0 } , dc[] = { 0 , 1 , 0 , -1 };
pair< int , int >find_locmin_point( int n , int m ) {
    int l , c , d;
    for( l = 1 ; l <= n ; l++ ) {
        for( c = 1 ; c <= m ; c++ )
            mat[l][c] = cnt[l][c] = 0;
    }
    for( l = 1 ; l <= n ; l++ ) {
        for( c = 1 ; c <= m ; c++ ) {
            if( l == 1 )
                cnt[l][c]++;
            if( c == 1 )
                cnt[l][c]++;
            if( l == n )
                cnt[l][c]++;
            if( c == m )
                cnt[l][c]++;
        }
    }
    for( l = 1 ; l <= n ; l++ )
        for( c = 1 ; c <= m ; c++ ) {
            mat[l][c] = value( l , c );
            for( d = 0 ; d < 4 ; d++ ) {
                if( mat[l + dl[d]][c + dc[d]] == 0 )
                    continue;
                if( mat[l][c] > mat[l + dl[d]][c + dc[d]] )
                    cnt[l + dl[d]][c + dc[d]]++;
                else
                    cnt[l][c]++;
                if( cnt[l + dl[d]][c + dc[d]] == 4 )
                    return { l + dl[d] , c + dc[d] };
                if( cnt[l][c] == 4 )
                     return { l , c };
            }
        }
}

