#include <iostream>
#include <cstdio>

int v[1000][1000];
int maind[2000][4000];
int mainc[2000];
int secd[2000][4000];
int secc[2000];

inline int par(int n) {return n/2-1;}
inline int lc(int n) {return 2*n+1;}
inline int rc(int n) {return 2*n+2;}
inline int min(int a,int b){return a<b?a:b;}
inline int max(int a,int b){return a>b?a:b;}
inline int abs(int a){return a>0?a:-a;}

void swap(int* x, int* y)
{
     int p = *x;
     *x = *y;
     *y = p;
}

void loadtrees(int n)
{
     for(int i=0;i<n;i++) for(int j=0;j<n;j++)
     {
             maind[i-j+n-1][mainc[i-j+n-1]] = v[i][j];
             mainc[i-j+n-1]++;
             secd[i+j][secc[i+j]] = v[i][j];
             secc[i+j]++;
     }
}

void buildtrees(int n)
{
     for(int i=0;i<2*n-1;i++)
     {
             int npow = 1;
             while(npow < secc[i]) npow*=2;
             for(int j=0;j<n;j++) swap(&secd[i][j+npow-1],&secd[i][j]);
             for(int j=2*n-2;j>0;j--) secd[i][par(j)] = min(secd[i][par(j)], secd[i][j]);
     }
     for(int i=0;i<2*n-1;i++)
     {
             int npow = 1;
             while(npow < mainc[i]) npow*=2;
             for(int j=0;j<n;j++) swap(&maind[i][j+npow-1],&maind[i][j]);
             for(int j=2*n-2;j>0;j--) maind[i][par(j)] = min(maind[i][par(j)], maind[i][j]);
     }
}

int getmin(int i, int j, int num, int pos, int cfrom, int cto, bool mode) //true - main/false - sec
{
    if(cfrom==cto) return mode?maind[num][pos]:secd[num][pos];
    if(i==cfrom && j == cto) return mode?maind[num][pos]:secd[num][pos];
    if(i>=cfrom && j<=(cfrom+cto)/2) return getmin(i,j,num,lc(pos), cfrom, (cfrom+cto)/2, mode);
    if(i>=(cfrom+cto)/2+1 && j<=cto) return getmin(i,j,num,rc(pos), (cfrom+cto)/2+1, cto, mode);
    return min(getmin(i,(cfrom+cto)/2,num,lc(pos), cfrom, (cfrom+cto)/2, mode), getmin((cfrom+cto)/2+1,j,num,rc(pos), (cfrom+cto)/2+1, cto, mode));
}

void getcoord(int *i, int *j, int num, bool mode, int n)
{
    if(mode)
    {
            int c1 = *i;
            int c2 = num-n+ *i;
            *i = min(min(max(c2,0), n-1), c1);
            *j = max(min(max(c2,0), n-1), c1);
    }
    else
    {
            int c1 = *i;
            int c2 = num- *i;
            *i = n - 1 - max(min(max(c2,0), n-1), c1);
            *j = n - 1 - min(min(max(c2,0), n-1), c1);
    }
}

int ask(int i, int j, int n)
{
    int cmaind = i-j+n-1;
    int csecd = i+j;
    int maxdist = -1;
    for(int br=0;br<2*n-1;br++)
    {
            if(br!=cmaind)
            {
            int gi = i, gj = j;
            getcoord(&gi, &gj, br, true, n);
            int powziv = 1;
            while(powziv<mainc[br])powziv*=2;
            int gmin = getmin(gi,gj,br,0,0,powziv-1,true);
            if(gmin < v[i][j]) maxdist = max(abs(br-cmaind),maxdist);
            }
    }
    for(int br=0;br<2*n-1;br++)
    {
            if(br!=csecd)
            {
            int gi = i, gj = j;
            getcoord(&gi, &gj, br, false, n);
            int powziv = 1;
            while(powziv<secc[br])powziv*=2;
            int gmin = getmin(gi,gj,br,0,0,powziv-1,false);
            if(gmin < v[i][j]) maxdist = max(abs(br-csecd),maxdist);
            }
    }
    return maxdist;
}

void update()
{
     
}

int main()
{
    for(int i=0;i<2000;i++) for(int j=0;j<4000;j++)
    {
            maind[i][j] = 2000000000;
            secd[i][j] = 2000000000;
    }
    int n;
    FILE* infile = fopen("pentranje.in","r");
    FILE* outfile = fopen("pentranje.out","w");
    fscanf(infile, "%d", &n);
    for(int i=0;i<n;i++) for(int j=0;j<n;j++)
    {
            fscanf(infile, "%d", &v[i][j]);
    }
    loadtrees(n);
    buildtrees(n);
    int q;
    fscanf(infile, "%d", &q);
    for(int i=0;i<q;i++)
    {
            int x,y, newv;
            int mode;
            scanf("%d", &mode);
            if(mode==1)
            {
                       fscanf(infile, "%d%d", &x, &y);
                       int dist = ask(x-1,y-1,n);
                       fprintf(outfile, "%d\n", dist);
            }
            else
            {
                
            }
    }
    fclose(infile);
    fclose(outfile);
    return 0;
}
