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

#include "code.h"

int mat[1005][1005];

/*static int n, k;
static int question_num = 0;
static int h[1001][1001];

int Pitaj(int i, int j){
	question_num++;
	if ((i < 1) || (i > n)) return -1;
	if ((j < 1) || (j > n)) return -1;
	return h[i][j];
}*/

int maxred[1005];
int minkol[1005];
int cntred[1005];
int cntkol[1005];

int cnt = 0;

int getval(int i, int j){
    if(mat[i][j]) return mat[i][j];
    cnt++;
    int x = Pitaj(i, j);
    if(!maxred[i]){
        maxred[i] = x;
        cntred[i] = 1;
    }
    else{
        if(x > maxred[i]){
            maxred[i] = x;
            cntred[i] = 1;
        }
        else if(x == maxred[i]){
            cntred[i]++;
        }
    }
    if(!minkol[j]){
        minkol[j] = x;
        cntkol[j] = 1;
    }
    else{
        if(x < minkol[j]){
            minkol[j] = x;
            cntkol[j] = 1;
        }
        else if(x == minkol[j]){
            cntkol[j]++;
        }
    }
    return x;
}

vector <int> resi123(int N){
    for(int i=1; i<=N; i++){
        for(int j=1; j<=N; j++){
            if(cnt > 5000){
                int a = 1/0;
                cout << a;
            }
            if(minkol[j] == 1) continue;
            if(minkol[j] == 2){
                if(!(mat[i][j] == 2 && cntkol[j] == 1)) continue;
            }
            if(maxred[i] == 3) continue;
            if(maxred[i] == 2){
                if(!(mat[i][j] == 2 && cntkol[j] == 1)) continue;
            }
            mat[i][j] = getval(i, j);
        }
        for(int j=1; j<=N; j++){
            if(minkol[j] == 1) continue;
            if(minkol[j] == 2){
                if(!(mat[i][j] == 2 && cntkol[j] == 1)) continue;
            }
            if(maxred[i] == 3) continue;
            if(maxred[i] == 2){
                if(!(mat[i][j] == 2 && cntkol[j] == 1)) continue;
            }
            if(mat[i][j] == 2){
                bool moze = 1;
                for(int k=1; k<i; k++){
                    mat[i][k] = getval(i, k);
                    if(mat[i][k] > 1){
                        moze = 0;
                        break;
                    }
                }
                if(moze == 0) continue;
                for(int k=i+1; k<=N; k++){
                    mat[i][k] = getval(i, k);
                    if(mat[i][k] > 1){
                        moze = 0;
                        break;
                    }
                }
                if(moze == 0) continue;
                for(int k=1; k<i; k++){
                    mat[k][i] = getval(k, i);
                    if(mat[k][i] < 3){
                        moze = 0;
                        break;
                    }
                }
                if(moze == 0) continue;
                for(int k=i+1; k<=N; k++){
                    mat[k][i] = getval(k, i);
                    if(mat[k][i] < 3){
                        moze = 0;
                        break;
                    }
                }
                if(moze == 0) continue;
                return {i, j};
            }
        }
    }
    return {0, 0};
}

std::vector <int> Nadji(int N, int K){
    if(K == 5000){
        return resi123(N);
    }
    for(int i=1; i<=N; i++){
        for(int j=1; j<=N; j++){
            mat[i][j] = Pitaj(i, j);
            maxred[i] = max(maxred[i], mat[i][j]);
            if(!minkol[j]) minkol[j] = mat[i][j];
            else minkol[j] = min(minkol[j], mat[i][j]);
        }
    }
    for(int i=1; i<=N; i++){
        for(int j=1; j<=N; j++){
            if(mat[i][j] == maxred[i]) cntred[i]++;
            if(mat[i][j] == minkol[j]) cntkol[j]++;
        }
    }
    for(int i=1; i<=N; i++){
        for(int j=1; j<=N; j++){
            if(maxred[i] == mat[i][j] && minkol[j] == mat[i][j]){
                if(cntred[i] == 1 && cntkol[j] == 1) return {i, j};
            }
        }
    }
    return {0, 0};
}


/*int main()
{
	scanf("%d%d", &n, &k);

	for (int i = 1; i <= n; i++)
		for (int j = 1; j <= n; j++)
			scanf("%d", &h[i][j]);

	std::vector<int> out = Nadji(n, k);

	printf("%d %d %d\n", out[0], out[1], question_num);

	return 0;
}*/