const dx:array[1..4]of integer=(1,0,-1,0);
dy:array[1..4]of integer=(0,1,0,-1);
var g:array[1..1000,1..1000]of integer;
//v:array[1..1000,1..1000]of boolean;
d:array[1..1000,1..1000]of longint;
qx,qy:array[1..1000000]of integer;
ans:array[1..100000]of longint;
i,j,k,x0,y0,x,y,n,m,h,t:longint;
st:string;
begin
readln(n,m);
fillchar(d,sizeof(d),0);
for i:=1 to n do
begin
readln(st);
for j:=1 to n do
g[i,j]:=ord(st[j])-48;
end;
for i:=1 to m do
begin
readln(x0,y0);
if d[x0,y0]<>0 then begin writeln(ans[d[x0,y0]]);continue;end;
h:=0;t:=1;ans[i]:=1;
d[x0,y0]:=i;
qx[1]:=x0;qy[1]:=y0;
while h<t do
begin
qx[h]:=0;qy[h]:=0;
inc(h);
for k:=1 to 4 do
begin
x:=qx[h]+dx[k];
y:=qy[h]+dy[k];
if(x<=n)and(x>=1)and(y<=n)and(y>=1)
and(d[x,y]=0)and(g[x,y]+g[qx[h],qy[h]]=1)then
begin
inc(t);inc(ans[i]);
qx[t]:=x;qy[t]:=y;
//v[x,y]:=false;
d[x,y]:=i;
end;
end;
end;
qx[t]:=0;qy[t]:=0;
writeln(ans[i]);
end;
end.
同学们,知道这个为什么每次只有60分吗?发现后,你会感慨细节是多么重要。