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

const long long maxn=505;
int table[maxn][maxn];

pair <int,int> find_locmin_point(int n, int m) {
    int a=1,b=1,nexta,nextb;
    bool f=true;
    while(a<=n && b<=m){
        table[a][b]=value(a,b);
        f=true;
        nexta=0;
        nextb=0;
        if(a>1 && table[a-1][b]<table[a][b]) f=false;

        if(a<n && !table[a+1][b]) table[a+1][b]=value(a+1,b);
        if(a<n && table[a+1][b]<table[a][b]){
            nexta=a+1;
            nextb=b;
            f=false;
        }

        if(b<m && !table[a][b+1]) table[a][b+1]=value(a,b+1);
        if(b<m && table[a][b+1]<table[a][b]){
            nexta=a;
            nextb=b+1;
            f=false;
        }

        if(b>1 && table[a][b-1]<table[a][b]) f=false;

        if(f) return {a,b};
        if(nexta!=0 && nextb!=0){
            a=nexta;
            b=nextb;
        }else{
            if(b+2<=m) b+=2;
            else if(a+1<=n){
                a++;
                b=1;
            }else{
                a++;
                b++;
            }
        }
    }
    return {a,b};
}
