#include <cstdio>
#include <algorithm>
#include <queue>
#include <cstring>

using namespace std;

const int maxn = 1e3 + 5;
const int dx[] = { 1, 0, -1, 0 };
const int dy[] = { 0, 1, 0, -1 };

int n, q, map[ maxn ][ maxn ], depth[ maxn ][ maxn ], low[ 5 ][ 2 * maxn ];

bool in( int x, int y )
{
    if ( x < 0 || x == n ) return false;
    if ( y < 0 || y == n ) return false;
    return true;
}

void bfs( int idx, int x, int y )
{
    queue < pair < int, int > > Q;
    memset( depth, -1, sizeof( depth ) );
    Q.push( make_pair( x, y ) );
    depth[ x ][ y ] = 0;
    low[ idx ][ 0 ] = map[ x ][ y ];
    while ( ! Q.empty() )
    {
        int x = Q.front().first;
        int y = Q.front().second;
        Q.pop();
        for ( int i = 0; i < 4; i++ )
            if ( in( x + dx[ i ], y + dy[ i ] ) && depth[ x + dx[ i ] ][ y + dy[ i ] ] == -1 )
            {
                depth[ x + dx[ i ] ][ y + dy[ i ] ] = depth[ x ][ y ] + 1;
                if ( low[ idx ][ depth[ x + dx[ i ] ][ y + dy[ i ] ] ] == -1 ||
                    low[ idx ][ depth[ x + dx[ i ] ][ y + dy[ i ] ] ] > map[ x + dx[ i ] ][ y + dy[ i ] ] )
                    low[ idx ][ depth[ x + dx[ i ] ][ y + dy[ i ] ] ] = map[ x + dx[ i ] ][ y + dy[ i ] ];
                Q.push( make_pair( x + dx[ i ], y + dy[ i ] ) );
            }
    }
}

int compute( int idx, int x, int y )
{
    if ( idx == 0 ) return ( x + y );
    if ( idx == 1 ) return ( x + n - 1 - y );
    if ( idx == 2 ) return ( n - 1 - x + y );
    return ( n - 1 - x + n - 1 - y );
}

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", &map[ i ][ j ] );

    memset( low, -1, sizeof( low ) );
    bfs( 0, 0, 0 );
    bfs( 1, 0, n - 1 );
    bfs( 2, n - 1, 0 );
    bfs( 3, n - 1, n - 1 );

    scanf( "%d", &q );
    while ( q-- )
    {
        int type;
        scanf( "%d", &type );
        if ( type == 2 )
        {
            int x, y, z;
            scanf( "%d %d %d", &x, &y, &z );
            x--; y--;
            map[ x ][ y ] = z;
            memset( low, -1, sizeof( low ) );
            bfs( 0, 0, 0 );
            bfs( 1, 0, n - 1 );
            bfs( 2, n - 1, 0 );
            bfs( 3, n - 1, n - 1 );
            continue;
        }
        int x, y;
        scanf( "%d %d", &x, &y );
        x--; y--;
        int dist = -1;
        for ( int i = 0; i < 4; i++ )
        {
            int k = compute( i, x, y );
            for ( int j = 0; j <= k; j++ )
                if ( low[ i ][ j ] < map[ x ][ y ] )
                {
                    dist = max( dist, k - j );
                    break;
                }
        }
        printf( "%d\n", dist );
    }

    return 0;
}
