#include <cstdio>
#include <cstring>
#include <algorithm>
using namespace std;

int N,Q,K;

struct vocnjak{
   int x,y,v;
};

bool cmp_vocnjak(vocnjak a,vocnjak b){
   return a.v < b.v;
}

int A[1005][1005];
vocnjak Au[1000005];
int R[1005][1005];

void ucitajvocnjak(){
   scanf("%d",&N);
   int i,j;
   for (i=1; i<=N; i++){
      for (j=1; j<=N; j++){
         scanf("%d",A[i]+j);
      }
   }
}

void sortirajvocnjak(){
   int i,j;
   for (i=1; i<=N; i++){
      for (j=1; j<=N; j++){
         K++;
         Au[K].x = i;
         Au[K].y = j;
         Au[K].v = A[i][j];
      }
   }
   sort(Au+1,Au+K+1,cmp_vocnjak);
}


inline int aps(int x){
   if (x<0) return -x; else return x;
}

inline int vece(int x,int y){
   if (x<y) return y; else return x;
}

void resi_semibrut(){
   sortirajvocnjak();
   int x,y,v,i,j,k,dm;
   for (i=1; i<=Q; i++){
      scanf("%d%d%d",&j,&x,&y);
      v = A[x][y];
      if (R[x][y]!=0) printf("%d\n",R[x][y]); else {
         j=1;
         dm=-1;
         while (Au[j].v < v && j<=N*N){
            dm = vece(dm,aps(x-Au[j].x)+aps(y-Au[j].y));
            //printf("%d %d %d %d\n",j,Au[j].v,Au[j].x,Au[j].y);
            j++;
         }
         R[x][y] = dm;
         printf("%d\n",dm);
      }
   }
}

void resi_brut(){
   int x,y,v,i,j,k,dm;
   for (k=1; k<=Q; k++){
      scanf("%d%d%d",&j,&x,&y);
      if (j==2){
         scanf("%d",&v);
         A[x][y] = v;
      } else {
         v = A[x][y];
         dm = -1;
         for (i=1; i<=N; i++){
            for (j=1; j<=N; j++){
               if (A[i][j] < v){
                  dm = dm = vece(dm,aps(x-i)+aps(y-j));
               }
            }
         }
         printf("%d\n",dm);
      }
   }
}

int main(){
   freopen("pentranje.in","r",stdin);
   freopen("pentranje.out","w",stdout);
   ucitajvocnjak();
   scanf("%d",&Q);
   if (Q%2==0) resi_brut(); else resi_semibrut();
   return 0;
}



