#include <stdio.h>
#include <stdlib.h>
static long n,a[1000][1000],d[1000][1000];



void pocpo(long i,long j)
 { 
     long k,t,m,max,rat;
   bool p;
      rat=1;
      d[i][j]=0;
     p=true;
     max=i+j;
     m=n-1-j+i;
     if (m>max)max=m;
     m=n-1-i+j;
     if (m>max)max=m;
     m=2*(n-1)-j-i;
     if (m>max)max=m;
     while(rat<=max)
        {for(k=rat;k>=-rat && p;k--)
           {t=rat-labs(k);
            if(i+k<n && i+k>=0 && j+t<n && j+t>=0 && 
               a[i][j]>a[i+k][j+t])
               {p=false;d[i][j]=rat;
               }
            if(p){t=-t;  
                  if(i+k<n && i+k>=0 && j+t<n && j+t>=0 && 
                  a[i][j]>a[i+k][j+t])
                     {p=false;d[i][j]=rat;
                     }  
                  }  
            } 
           rat++; p=true;
         }
     
     
     
     
     
 }    
     
void poc()
{ 
     long i,j,k,t,l,rat,m,max;
 bool p;
  for(i=0;i<n;i++)
   for(j=0;j<n;j++)
     { 
     rat=1;
     p=true;
     max=i+j;
     m=n-1-j+i;
     if (m>max)max=m;
     m=n-1-i+j;
     if (m>max)max=m;
     m=2*(n-1)-j-i;
     if (m>max)max=m;
     while(rat<=max)
        {for(k=rat;k>=-rat && p;k--)
           {t=rat-labs(k);
            if(i+k<n && i+k>=0 && j+t<n && j+t>=0 && 
               a[i][j]>a[i+k][j+t])
               {p=false;d[i][j]=rat;
               }
            if(p){t=-t;  
                  if(i+k<n && i+k>=0 && j+t<n && j+t>=0 && 
                  a[i][j]>a[i+k][j+t])
                     {p=false;d[i][j]=rat;
                     }  
                  }  
            } 
           rat++; p=true;
         }
     }
     
}    
          
     
int main()
{ FILE *in,*out;
  in=fopen("pentranje.in","r");
  out=fopen("pentranje.out","w");
    long i,j,q,k,p,h,nema=false;
  fscanf(in,"%ld",&n);
  for(i=0;i<n;i++)
   for(j=0;j<n;j++)fscanf(in,"%ld",&a[i][j]);
   poc();
  fscanf(in,"%ld",&q);
   for(k=1;k<=q;k++)
     {fscanf(in,"%ld%ld%ld",&p,&i,&j);
     i--;j--;
      if (p==1)
           {   if (nema)pocpo(i,j);
                if (d[i][j])fprintf(out,"%ld\n",d[i][j]);
                else fprintf(out,"-1\n");
           }    
     else   {
            nema=true;
            fscanf(in,"%ld",&h);
            a[i][j]=h;
            
            }        
     }
  
  
 fclose(in);
 fclose(out);
  
}
