为啥全部RE
#include<bits/stdc++.h>
using namespace std;
int n,m,a[1001][1001],x,y,fx[4][2]= {{1,0},{-1,0},{0,1},{0,-1}},d,c[100001],flag[1001][1001];
struct node {
int x,y,z;
};
int bfs() {
for(int i=1; i<=n; i++) {
for(int j=1; j<=n; j++) {
if(flag[i][j]==0) {
int cnt=0;
queue<node>q;
node t;
t.x=i;
t.y=j;
t.z=a[i][j];
flag[i][j]=++d;
q.push(t);
while(!q.empty()) {
t=q.front();
q.pop();
int nx,ny;
for(int i=0; i<4; i++) {
node tt;
tt.x=t.x+fx[i][0];
tt.y=t.y+fx[i][1];
if(tt.x>=1&&tt.y>=1&&tt.x<=n&&tt.y<=n&&flag[tt.x][tt.y]==0&&a[tt.x][tt.y]==(0-t.z)) {
cnt++;
flag[tt.x][tt.y]=d;
tt.z=0-t.z;
q.push(tt);
}
}
}
c[d]=cnt;
}
}
}
}
int main() {
scanf("%d%d",&n,&m);
for(int i=1; i<=n; i++) {
for(int j=1; j<=n; j++) {
scanf("%1d",&x);
a[i][j]=x;
if(x==0) {
a[i][j]=-1;
}
}
}
bfs();
for(int i=1; i<=m; i++) {
cin>>x>>y;
printf("%d\n",c[flag[x][y]]+1);
}
return 0;
}