#include <bits/stdc++.h>
#define LL long long
#define DB double
#define BUG cout << "OoO" << endl
using namespace std;
const int N = 1001, Max_N = 1000001;
int n, m, ax, ay;
int d, mp[N][N], vis[N][N], ans[Max_N];
int dx[] = {0, 0, -1, 1}, dy[] = {-1, 1, 0, 0};
struct Node {int x, y;};
int BFS(int ax, int ay, int d)
{
int ret = 1;
queue <Node> q;
q.push(Node{ax, ay});
vis[ax][ay] = 1;
while(!q.empty())
{
Node u = q.front();
q.pop();
for(int i = 0; i < 4; i ++ )
{
int xx = u.x + dx[i];
int yy = u.y + dy[i];
if(mp[u.x][u.y] == mp[xx][yy]) continue;
if(vis[xx][yy] > 0) continue;
if(xx < 1 || xx > n || yy < 1 || yy > n) continue;
q.push(Node{xx, yy});
ret ++ ;
vis[xx][yy] = d;
}
}
return ret;
}
int main()
{
scanf("%d%d", &n, &m);
for(int i = 1; i <= n; i ++ )
{
for(int j = 1; j <= n; j ++ )
{
char ch;
cin >> ch;
mp[i][j] = ch - '0';
}
}
memset(vis, 0, sizeof vis);
for(int i = 0; i < m; i ++ )
{
scanf("%d%d", &ax, &ay);
if(vis[ax][ay] == 0)
{
d ++ ;
ans[d] = BFS(ax, ay, d);
}
printf("%d\n", ans[vis[ax][ay]]);
}
return 0;
}
为什么不是RE就是WA啊!