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

std::pair <int, int> find_locmin_point(int n, int m) {
	int i = 1,j = 1;
	vector <int> d1 = {0 , 0 , -1 , 1};
	vector <int> d2 = {-1 , 1 , 0 , 0};
	
	vector <vector <int>> a(n + 1, vector <int> (m + 1 , -1));
	
    while(true){
		if(a[i][j] == -1){
			a[i][j] = value(i , j);
		}
		
		bool f = true;
		
		for(int k = 0;k < 4;k++){
			int ni = i + d1[k];
			int nj = j + d2[k];
			
			if(ni <= 0 || ni > n || nj <= 0 || nj > m) continue;
			
			if(a[ni][nj] == -1){
				a[ni][nj] = value(ni , nj);
			}
			
			if(a[ni][nj] <= a[i][j]){
				f = false;
				i = ni;
				j = nj;
				break;
			}
		}
		
		if(f){
			return {i , j};
		}
	}
}
