#include<cstdio>
#include<cctype>
#include<queue>
#include<cmath>
#include<cstring>
#include<algorithm>
#define rep(i,s,t) for(int i=s;i<=t;i++)
#define dwn(i,s,t) for(int i=s;i>=t;i--)
#define ren for(int i=first[x];i;i=next[i])
using namespace std;
const int BufferSize=1<<16;
char buffer[BufferSize],*head,*tail;
inline char Getchar() {
if(head==tail) {
int l=fread(buffer,1,BufferSize,stdin);
tail=(head=buffer)+l;
}
return *head++;
}
inline int read() {
int x=0,f=1;char c=Getchar();
for(;!isdigit(c);c=Getchar()) if(c==‘-‘) f=-1;
for(;isdigit(c);c=Getchar()) x=x*10+c-‘0‘;
return x*f;
}
const int maxn=710;
const int INF=2147483647;
int n,A[maxn][maxn],B[maxn][maxn],f[maxn][maxn];
int dp(int x,int y) {
if(x<1||y<1||x>n||y>n) return 0;
if(B[x][y]) return 0;
int& ans=f[x][y];
if(ans) return ans;
if(A[x][y]>A[x-1][y]) ans=max(ans,dp(x-1,y)+1);
if(A[x][y]>A[x+1][y]) ans=max(ans,dp(x+1,y)+1);
if(A[x][y]>A[x][y-1]) ans=max(ans,dp(x,y-1)+1);
if(A[x][y]>A[x][y+1]) ans=max(ans,dp(x,y+1)+1);
return ans=max(ans,1);
}
int main() {
n=read();
rep(i,1,n) rep(j,1,n) A[i][j]=read();
dwn(i,read(),1) {
char c=Getchar();while(!isalpha(c)) c=Getchar();
if(c==‘C‘) {
int x=read(),y=read();
A[x][y]=read();
}
else if(c==‘S‘) {
int a=read(),b=read(),c=read(),d=read();
rep(i,a,c) rep(j,b,d) B[i][j]=1;
}
else if(c==‘B‘) {
int a=read(),b=read(),c=read(),d=read();
rep(i,a,c) rep(j,b,d) B[i][j]=0;
}
else {
int ans=0;memset(f,0,sizeof(f));
rep(i,1,n) rep(j,1,n) if(A[i][j]!=INF) ans=max(ans,dp(i,j));
printf("%d\n",ans);
}
}
return 0;
}