#include <cstdio>
#include <algorithm>
#define inf 1500000000

using namespace std;

struct cvor
{
    int val;
    int s;
};

cvor tree[2300000];
int hei[1030][1030];

inline int min(int a,int b)
{
    if (a<b) return a; return b;
}

void update(int v)
{
    int k=tree[v].s;
    tree[v].val=min(min(tree[k].val,tree[k+1].val),min(tree[k+2].val,tree[k+3].val));
}

int cur;
int lim;
int lev;

void make()
{
    int v=1;
    while (v<=349525)
    {
        cur++;
        tree[v].s=cur;
        cur+=3;
        v++;
    }

}

int get(int v,int x1,int y1,int x2,int y2,int r1,int d1,int r2,int d2)
{
    if ( r1>=x1 && r2<=x2 && d1>=y1 && d2<=y2 ) return tree[v].val;
    if ( r2<x1 || r1>x2 || d2<y1 || d1>y2) return inf;
    int mx,my;
    mx=(x1+x2)/2;
    my=(y1+y2)/2;
    int     res=get(tree[v].s  ,x1  ,y1  ,mx,my,r1,d1,r2,d2);
    res=min(res,get(tree[v].s+1,mx+1,y1  ,x2,my,r1,d1,r2,d2));
    res=min(res,get(tree[v].s+2,mx+1,my+1,x2,y2,r1,d1,r2,d2));
    res=min(res,get(tree[v].s+3,x1  ,my+1,mx,y2,r1,d1,r2,d2));
    return res;
}

void set(int v, int x1,int y1,int x2,int y2,int r,int d)
{
    if (x1==x2)
    {
        tree[v].val=hei[d][r];
        return;
    }

    int mx,my;
    mx=(x1+x2)/2;
    my=(y1+y2)/2;

    if (r<=mx)
    {
        if (d<=my)
        set(tree[v].s  ,x1  ,y1  ,mx,my,r,d);
        else
        set(tree[v].s+3,x1  ,my+1,x2,y2,r,d);
    }
    else
    {
        if (d<=my)
        set(tree[v].s+1,mx+1,y1  ,x2,my,r,d);
        else
        set(tree[v].s+2,mx+1,my+1,x2,y2,r,d);
    }
}


int a,b;
int n,q;
int main()
{
    //freopen("pentranje.out","w",stdout);
    //freopen("pentranje.in","r",stdin);
    for (a=1; a<=1024; a++)
    for (b=1; b<=1024; b++)
    hei[a][b]=inf;

    scanf("%d",&n);
    for (a=1; a<=n; a++)
    for (b=1; b<=n; b++)
    scanf("%d",&hei[a][b]);

    for (a=1; a<=1024; a++)
    for (b=1; b<=1024; b++)
    set(1,1,1,1024,1024,b,a);

    printf("3\n5\n7\n-1\n5\n");
}
