#include <iostream>
#include <cstdio>
#include <cmath>
#include <set>
#include <vector>
#include <cstring>


using namespace std;

const int N=1005;

int n, q, dim, m;
int a[N][N];
int b[4][33][33][N]; //sljive u i, j-tom nadpolju, sortirane po udaljenosti od NW, NE, SW, SE coskova

inline void preprocess()
{
    dim=sqrt((double)N);
    m=n/dim;
}

inline void update(int x, int y, int v)
{
    a[x][y]=v;
}

inline int abs(int x)
{
    return (x<0)?(-x):x;
}

struct bfs_block
{
    short x, y;
    bfs_block() {}
    bfs_block(short _x, short _y):x(_x), y(_y) {}
    const int dist(const int _x, const int _y) const
    {
        return abs(x-_x)+abs(y-_y);
    }
};

bool pos[N][N];

struct queue
{
    int first, last, cnt;
    static const int bufsize=N*N;
    bfs_block buf[bufsize];
    queue()
    {
        first=last=cnt=0;
    }
    void push(const bfs_block& x)
    {
        if(!pos[x.x][x.y])
        {
            pos[x.x][x.y]=1;
            buf[last]=x;
            last=(last+1)%bufsize;
            cnt++;
        }
    }
    void pop()
    {
        first=(first+1)%bufsize;
        cnt--;
    }
    bfs_block top()
    {
        return buf[first];
    }
    const bool empty() const
    {
        return cnt==0;
    }
} bfs_queue;

inline int query(int x, int y)
{
    int res=-1;
    memset(pos, 0, sizeof(pos));
    bfs_queue.push(bfs_block(x, y));
    while(!bfs_queue.empty())
    {
        bfs_block t=bfs_queue.top();
        bfs_queue.pop();
        if(t.dist(x, y)>res&&a[t.x][t.y]<a[x][y]) res=t.dist(x, y);
        if(t.x>0) bfs_queue.push(bfs_block(t.x-1, t.y));
        if(t.y>0) bfs_queue.push(bfs_block(t.x, t.y-1));
        if(t.x<n-1) bfs_queue.push(bfs_block(t.x+1, t.y));
        if(t.y<n-1) bfs_queue.push(bfs_block(t.x, t.y+1));
    }
    return res;
}

int main()
{
    freopen("pentranje.in", "r", stdin);
    freopen("pentranje.out", "w", stdout);
    scanf("%d", &n);
    for(int i=0; i<n; i++)
    {
        for(int j=0; j<n; j++)
        {
            scanf("%d", &a[i][j]);
        }
    }
    preprocess();
    scanf("%d", &q);
    for(int _q=0; _q<q; _q++)
    {
        int query_type;
        scanf("%d", &query_type);
        //fprintf(stderr, "query type: %d\n", query_type);
        if(query_type==1)
        {
            int x, y;
            scanf("%d%d", &x, &y);
            x--, y--;
            printf("%d\n", query(x, y));
        }
        else
        {
            int x, y, v;
            scanf("%d%d%d", &x, &y, &v);
            x--, y--;
            update(x, y, v);
        }
    }
    return 0;
}
