#include <cstdio>
#include <algorithm>

using namespace std;

inline int dist(int a,int b,int x,int y)
{
    return abs(a-x)+abs(b-y);
}

inline int max(int a,int b)
{
    if (a>b) return a; return b;
}

int val[1010][1010];

int n,q;
int a,b,c,d,sol;

int main()
{
    freopen("pentranje.in","r",stdin);
    freopen("pentranje.out","w",stdout);
    scanf("%d",&n);
    for (a=1; a<=n; a++)
    for (b=1; b<=n; b++)
    scanf("%d",&val[a][b]);

    scanf("%d",&q);
    while (q--)
    {
        scanf("%d",&a);
        if (a==2)
        {
            scanf("%d%d",&a,&b);
            scanf("%d",&val[a][b]);
        }
        else
        {
            scanf("%d%d",&a,&b);
            sol=-1;
            for (c=1; c<=n; c++)
            for (d=1; d<=n; d++)
            if (val[c][d]<val[a][b]) sol=max(sol,dist(a,b,c,d));
            printf("%d\n",sol);
        }
    }
}
