#include <cstdio>

using namespace std;

const int MaxN = 1000 + 5;
const int MaxQ = 500000 + 5;


int N, Q, A[MaxN][MaxN];

int main ( void )
{
    scanf ( "%d", &N );

    for ( int i = 0; i < N; i++ )
        for ( int j = 0; j < N; j++ ) scanf ( "%d", &A[i][j] );

    scanf ( "%d", &Q );

    while ( Q-- )
    {
        int Command;

        scanf ( "%d", &Command );

        if ( Command % 2 )
        {
            int X, Y;

            scanf ( "%d %d", &X, &Y );

            X--;
            Y--;

            int Sol = -1;

            for ( int i = 0; i < N; i++ )
                for ( int j = 0; j < N; j++ )
                {
                    if ( ( i == X ) && ( j == Y ) ) continue;

                    if ( A[i][j] >= A[X][Y] ) continue;

                    int dx = i - X, dy = j - Y;

                    if ( dx < 0 ) dx *= -1;
                    if ( dy < 0 ) dy *= -1;

                    if ( dx + dy > Sol ) Sol = dx + dy;
                }

            printf ( "%d\n", Sol );
        }
        else
        {
            int X, Y, V;

            scanf ( "%d %d %d", &X, &Y, &V );

            X--;
            Y--;

            A[X][Y] = V;
        }
    }


    return 0;
}
