1 #include<cstdio>
2
3 #include<cstdlib>
4
5 #include<cmath>
6
7 #include<cstring>
8
9 #include<algorithm>
10
11 #include<iostream>
12
13 #include<vector>
14
15 #include<map>
16
17 #include<set>
18
19 #include<queue>
20
21 #include<string>
22
23 #define inf 1000000000
24
25 #define maxn 1000000+5
26
27 #define maxm 705
28
29 #define eps 1e-10
30
31 #define ll long long
32
33 #define pa pair<int,int>
34
35 #define for0(i,n) for(int i=0;i<=(n);i++)
36
37 #define for1(i,n) for(int i=1;i<=(n);i++)
38
39 #define for2(i,x,y) for(int i=(x);i<=(y);i++)
40
41 #define for3(i,x,y) for(int i=(x);i>=(y);i--)
42
43 #define mod 1000000007
44 #define num(i,j) ((i-1)*n+j)
45
46 using namespace std;
47
48 inline int read()
49
50 {
51
52 int x=0,f=1;char ch=getchar();
53
54 while(ch<‘0‘||ch>‘9‘){if(ch==‘-‘)f=-1;ch=getchar();}
55
56 while(ch>=‘0‘&&ch<=‘9‘){x=10*x+ch-‘0‘;ch=getchar();}
57
58 return x*f;
59
60 }
61 struct edge{int go,next;}e[4*maxn];
62 int n,tot,a[maxm][maxm],head[maxn],inp[maxn],d[maxn];
63 queue<int>q;
64 bool flag[maxn];
65 const int dx[4]={0,0,1,-1};
66 const int dy[4]={1,-1,0,0};
67 inline void insert(int x,int y)
68 {
69 e[++tot].go=y;e[tot].next=head[x];head[x]=tot;
70 }
71 void work()
72 {
73 memset(d,0,sizeof(d));
74 memset(inp,0,sizeof(inp));
75 memset(head,0,sizeof(head));tot=0;
76 for1(i,n)
77 for1(j,n)
78 if(!flag[num(i,j)])
79 for0(k,3)
80 {
81 int ii=i+dx[k],jj=j+dy[k];
82 if(ii<1||ii>n||jj<1||jj>n||flag[num(ii,jj)]||a[ii][jj]>=a[i][j])continue;
83 insert(num(i,j),num(ii,jj));inp[num(ii,jj)]++;
84 }
85 for1(i,n)for1(j,n)if(!flag[num(i,j)]&&!inp[num(i,j)])q.push(num(i,j)),d[num(i,j)]=1;
86 while(!q.empty())
87 {
88 int x=q.front();q.pop();
89 for(int i=head[x];i;i=e[i].next)
90 {
91 int y=e[i].go;
92 inp[y]--;
93 d[y]=max(d[y],d[x]+1);
94 if(!inp[y])q.push(y);
95 }
96 }
97 int ans=0;
98 for1(i,n*n)ans=max(ans,d[i]);
99 printf("%d\n",ans);
100 }
101
102 int main()
103
104 {
105
106 freopen("input.txt","r",stdin);
107
108 freopen("output.txt","w",stdout);
109
110 n=read();
111 for1(i,n)for1(j,n)a[i][j]=read();
112 int m=read();char ch[4];
113 while(m--)
114 {
115 scanf("%s",ch);
116 if(ch[0]==‘Q‘)work();
117 else if(ch[0]==‘C‘){int x=read(),y=read();a[x][y]=read();}
118 else
119 {
120 int x1=read(),y1=read(),x2=read(),y2=read();
121 for2(i,x1,x2)
122 for2(j,y1,y2)
123 flag[num(i,j)]=ch[0]==‘S‘?1:0;
124 }
125 }
126
127 return 0;
128
129 }