#include<iostream>
#include<stdlib.h>
#include<stdio.h>
#include<algorithm>
#include<set>
#include<map>
#include<deque>
#include<queue>
#include<vector>
#include<utility>
#include<cstring>
using namespace std;

int a[6000][6000];

int main()
{

   freopen("pentranje.in","r",stdin);
   freopen("pentranje.out","w",stdout);
    int n,q;
   cin>>n;
   for (int i=1;i<=n;i++)
   for (int j=1;j<=n;j++) cin>>a[i][j];
   cin>>q;

   for (int i=1;i<=q;i++)
   {
       int c,x,y,v;
       cin>>c;
       if (c==1)
       {
           cin>>x>>y;
           int sol=0;
           for (int i=1;i<=n;i++)
           for (int j=1;j<=n;j++) if ((a[i][j]<a[x][y]) && (abs(x-i)+abs(y-j)>sol) ) sol=abs(x-i)+abs(y-j);
           if (sol==0) cout<<"-1"<<endl;
           else cout<<sol<<endl;
       }
       if (c==2)
       {
           cin>>x>>y>>v;
           a[x][y]=v;
       }
   }


return 0;
}




