#include <cstdio>
#include <vector>
#include <cstdlib>
#include <algorithm>

using namespace std;

const int MaxN = 1e3 + 13;

int n, q, tip, x, y, z, a[ MaxN ][ MaxN ];
vector< pair< int, int > > upiti;

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 );
  scanf( "%d", &q );
  while ( q-- )
  {
    scanf( "%d", &tip );
    if ( tip - 1 )
    {
      scanf( "%d %d %d", &x, &y, &z );
      a[ --x ][ --y ] = z;
    }
    else
    {
      scanf( "%d %d", &x, &y );
      int ret = -1;
      x--, y--;
      for ( int i = 0; i < n; i++ )
        for ( int j = 0; j < n; j++ )
          if ( a[ i ][ j ] < a[ x ][ y ] ) ret = max( ret, abs( x - i ) + abs( y - j ) );
      printf( "%d\n", ret );
    }
  }
  return 0;
}
