#include "stdio.h"
#include "stdlib.h"
//zad. 2

int m,*a,*x,*y,*temp,*tempx,*tempy,pok[1000][1000];

int abs(int a,int b,int x, int y){
    int q=a-b,w=x-y;
    if (q<0) q*=-1;
    if (w<0) w*=-1;
    return q+w;   
}

void mergesort(int w, int q){
          int v=(w+q)/2;
          int k=v+1;
          int i;
          if(v>w) mergesort(w,v);
          if(q>k) mergesort(k,q);
          for(i=w;i<=v;i++) {temp[i]=a[i];tempx[i]=x[i];tempy[i]=y[i];}
          for(i=w;i<=q;i++) if(w<=v){
                                if(k>q){
                                         a[i]=temp[w];x[i]=tempx[w];y[i]=tempy[w];
                                         pok[x[i]][y[i]]=i;
                                         w++;        
                                }
                                else {
                                     if(a[k]>temp[w]) {
                                         a[i]=temp[w];x[i]=tempx[w];y[i]=tempy[w];
                                         pok[x[i]][y[i]]=i;
                                         w++;        
                                     }
                                     else {
                                          a[i]=a[k];x[i]=x[k];y[i]=y[k];
                                          pok[x[i]][y[i]]=i;
                                          k++;
                                     }
                                }     
          }          
}

void change(int i, int j){
     int temp;
     temp=a[i];
     a[i]=a[j];
     a[j]=temp;
     temp=x[i];
     x[i]=x[j];
     x[j]=temp;
     temp=y[i];
     y[i]=y[j];
     y[j]=temp;
     pok[x[j]][y[j]]=j;
}

void mrdni(int i,int vr){
     if(a[i]<vr){
                 a[i]=vr;
                 i++;
                 while((vr>a[i])&&(i<m)) {change(i,i-1);i++;}
                 pok[x[i-1]][y[i-1]]=i;
     }
     else{
                 a[i]=vr;
                 i--;
                 while((vr<a[i])&&(i>=0)) {change(i,i+1);i--;}
                 pok[x[i+1]][y[i+1]]=i;
     }
}

int funk(int vr,int h,int u){
    int rast=-1;
    int i=0,r;
    while(a[i]<vr) {
                   r=abs(h,x[i],u,y[i]);
                   if(r>rast) rast=r;
                   i++;              
    }  
    return rast;
}

int main(){FILE *in=fopen("pentranje.in","r"),*out=fopen("pentranje.out","w");

    int n,i,j,p=0,t,h,u,v;
    fscanf(in,"%d",&n);
    m=n*n;
    a=(int *)malloc(m*sizeof(int));
    x=(int *)malloc(m*sizeof(int));
    y=(int *)malloc(m*sizeof(int));
    temp=(int *)malloc(m*sizeof(int));
    tempx=(int *)malloc(m*sizeof(int));
    tempy=(int *)malloc(m*sizeof(int));
    for(i=0;i<n;i++) for(j=0;j<n;j++) {fscanf(in,"%d",a+p);pok[i][j]=p;x[p]=i;y[p]=j;p++;}
    mergesort(0,p-1);
    fscanf(in,"%d",&t);
    while(t--){
              fscanf(in,"%d",&j);
              if(j==1){
                       fscanf(in,"%d %d",&h,&u);
                       h--;u--;
                       fprintf(out,"%d\n",funk(a[pok[h][u]],h,u));
              }
              else {
                       fscanf(in,"%d %d %d",&h,&u,&v);
                       h--;u--;
                       mrdni(pok[h][u],v);
              }          
    }
fclose(in);fclose(out);return 0;
}
