#include<stdio.h>

int n,q,m[2000][2000],u,x,y,v,r[2000][2000];

FILE *fin,*fout;

void update()
{
 int i,j,k,l,tr;
 for(i=0;i<n;i++)
 {
  for(j=0;j<n;j++)
  {
   for(k=0;k<n;k++)
   {
    for(j=0;l<n;l++)
    {
     if(m[i][j]>m[k][l])
     {
      tr=i-k;
      if(tr<0)
       tr*=-1;
      if(j-l>0)
       tr+=j-l;
      else
       tr+=l-j; 
       if(tr>r[i][j])
        r[i][j]=tr; 
     }
    }
   }
  }
 }
}

int main()
{
 fin=fopen("pentranje.in","r");
 fout=fopen("pentranje.out","w");
 int i,j,k,l;
 fscanf(fin,"%d",&n);
 for(i=0;i<n;i++)
 {
  for(j=0;j<n;j++)
  {
   fscanf(fin,"%d",&m[i][j]);
  }
 }
 update();
 fscanf(fin,"%d",&q);
 for(i=0;i<q;i++)
 {
  fscanf(fin,"%d",&u);
  if(u==1)
  {
   fscanf(fin,"%d%d",&x,&y);
   if(r[x][y]==0)
    fprintf(fout,"-1");
   else
    fprintf(fout,"%d\n",r[x][y]); 
  }
  else
  {
   fscanf(fin,"%d%d%d",&x,&y,&v);
   m[x][y]=v;
   update();
  }
 }
 return 0;
}
