1 /**************************************************************
2 Problem: 3208
3 User: 1090900715
4 Language: Pascal
5 Result: Accepted
6 Time:16576 ms
7 Memory:4544 kb
8 ****************************************************************/
9
10 program j01;
11 var f,h:array[0..700,0..700]of longint;
12 bo:array[0..700,0..700]of boolean;
13 c:array[0..4,0..1]of longint;
14 n,m,i,j,a,b,c1,d,ans:longint;
15 ch:char;
16
17 function max(a,b:Longint):longint;
18 begin
19 if a>b then exit(a) else exit(b);
20 end;
21
22 function jud(x,y,x1,y1:longint):boolean;
23 begin
24 if (x1<=0)or(x1>n) then exit(false);
25 if (y1<=0)or(y1>n) then exit(false);
26 exit((h[x,y]>h[x1,y1])and bo[x1,y1]);
27 end;
28
29 procedure dfs(i,j:longint);
30 var x:longint;
31 begin
32 if f[i,j]<>0 then exit;
33 f[i,j]:=1;
34 for x:=1 to 4 do
35 if jud(i,j,i+c[x,0],j+c[x,1]) then
36 begin
37 dfs(i+c[x,0],j+c[x,1]);
38 f[i,j]:=max(f[i,j],f[i+c[x,0],j+c[x,1]]+1);
39 end;
40 ans:=max(ans,f[i,j]);
41 end;
42
43 begin
44 readln(n);
45 for i:=1 to n do
46 for j:=1 to n do
47 read(h[i,j]);
48 fillchar(bo,sizeof(bo),1);
49 c[1,0]:=0;c[1,1]:=1;
50 c[2,0]:=0;c[2,1]:=-1;
51 c[3,0]:=-1;c[3,1]:=0;
52 c[4,0]:=1;c[4,1]:=0;
53 readln(m);
54 while m>0 do
55 begin
56 dec(m);
57 read(ch);
58 while not(ch in[‘C‘,‘S‘,‘B‘,‘Q‘]) do read(ch);
59 if ch=‘C‘ then
60 begin
61 readln(a,b,c1);
62 h[a,b]:=c1;
63 end;
64 if ch=‘S‘ then
65 begin
66 readln(a,b,c1,d);
67 for i:=a to c1 do
68 for j:=b to d do
69 bo[i,j]:=false;
70 end;
71 if ch=‘B‘ then
72 begin
73 readln(a,b,c1,d);
74 for i:=a to c1 do
75 for j:=b to d do
76 bo[i,j]:=true;
77 end;
78 if ch=‘Q‘ then
79 begin
80 fillchar(f,sizeof(f),0);
81 ans:=0;
82 for i:=1 to n do
83 for j:=1 to n do
84 if (f[i,j]=0)and(bo[i,j]) then dfs(i,j);
85 writeln(ans);
86 end;
87 end;
88 end.