#include <iostream>
#include <stdio.h>
#include <vector>
#include <algorithm>
#include <cstdlib>
#include <utility>
#include <string>
#include <cmath>


using namespace std;

int n,i,j,Q; 
int matrica[1002][1002];

struct polje
{
     int vrednost;
     int x,y;
};
polje a[1000005];

bool uporedi(polje A, polje B)
{
   return A.vrednost < B.vrednost;
}

int bt(int poc, int kraj, int broj)
{
    int x = (poc + kraj) / 2;
    if (x == 0) return 0;
    if (a[x].vrednost == broj) 
    {
        if (a[x-1].vrednost == broj) return bt(poc, x-1, broj);
        return x;
    }
    if (a[x].vrednost > broj) return bt(poc, x-1, broj);
    else return bt(x+1, kraj, broj);
}

int absl(int r)
{
    if (r < 0) return -r;
    return r;
}

int main()
{
    FILE *fin, *fout;
    fin = fopen("pentranje.in", "r");
    fout = fopen("pentranje.out", "w");
    fscanf(fin,"%d", &n);
    //printf("spaka %d", n);
    int k = -1;
    for (i = 0; i < n; i++)
     for (j = 0; j < n; j++) 
      {
         int c;
         fscanf(fin,"%d", &c);
         matrica[i][j] = c;
         k++;
         a[k].vrednost = matrica[i][j];
         a[k].x = i;
         a[k].y = j;
      }
      //printf("spaka");
      k++;
     sort(a, a + k, uporedi);
    // for (i = 0; i < k; i++) printf("kordinate i vrednost: %d %d %d\n", a[i].x , a[i].y, a[i].vrednost); 
     
    fscanf(fin,"%d", &Q);
   // printf("%d", bt(0, k - 1, matrica[4][0]));
    /*printf("%d", Q);
    system("pause");*/
    
    for (int w = 0; w < Q; w++)
    {
       int x;
       fscanf(fin,"%d", &x);
       if (x == 1)
       {
             int A,B;
             fscanf(fin,"%d%d", &A,&B);
             A--;
             B--;
             int t = bt(0, k - 1, matrica[A][B]);
             //printf("t = %d\n", t);
             if (t == 0) fprintf(fout,"%d\n", -1);
             else
             {
                 int MAX = -1;
                 for (int d = t - 1; d >= 0; d--)
                 {
                     int razlika = absl(a[d].x - A) + absl(a[d].y - B);
                     if (razlika > MAX) MAX = razlika;
                 }
                 fprintf(fout,"%d\n", MAX);
             }
       }
       else 
       {
           int A,B,V;
           fscanf(fin,"%d%d%d", &A, &B, &V);
           A--; B--;  
           int ind = bt(0, k - 1, matrica[A][B]);
           matrica[A][B] = V;
           while (! (a[ind].x == A && a[ind].y == B))
           {
              ind++;
           }
           a[ind].vrednost = V;
           while (a[ind].vrednost > a[ind+1].vrednost && (ind < n - 1))
           {
               ind++;
               polje pom = a[ind];
               a[ind] = a[ind - 1];
               a[ind - 1] = pom;
           }
           while (a[ind].vrednost < a[ind-1].vrednost && ind > 0)
           {
               ind--;
               polje pom = a[ind];
               a[ind] = a[ind + 1];
               a[ind + 1] = pom;
           }
           
           
           
          
       }
    }
    //printf("%d", bt(0,20, 2));
    
    fclose(fin);
    fclose(fout);
    
    //system("pause");
    return 0;
}
