#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] = 0;
            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++ ) {
                int newl = l + dl[d] , newc = c + dc[d];
                if( mat[newl][newc] == 0 )
                    continue;

                if( mat[l][c] > mat[newl][newc] )
                    cnt[newl][newc]++;
                else
                    cnt[l][c]++;

                if( cnt[newl][newc] == 4 )
                    return { newl , newc };
                if( cnt[l][c] == 4 )
                     return { l , c };
            }
        }
    }

    return {1, 1};
}
