zhuona @ 2024-11-16 13:48:40
#include <iostream>
#include <queue>
#include <cstring>
using namespace std;
struct s{
int ax,ay;
};
int n,m;
char a[1005][1005];
bool v[1005][1005];
int lx[16]={1,2,3,4,-1,-2,-3,-4,0,0,0,0,0,0,0,0};
int ly[16]={0,0,0,0,0,0,0,0,1,2,3,4,-1,-2,-3,-4};
int b(int x,int y){
memset(v,0,sizeof(v));
v[x][y]=1;
queue <s> q;
s it;
it.ax=x;
it.ay=y;
q.push(it);
while(!q.empty()){
s h=q.front();
q.pop();
for(int i=0;i<16;i++){
int nx=h.ax+lx[i];
int ny=h.ay+ly[i];
if(nx>0 && nx<=n && ny>0 && ny<=n && a[h.ax][h.ay]!=a[nx][ny] && !v[nx][ny]){
it.ax=nx;
it.ay=ny;
q.push(it);
v[nx][ny]=1;
}
}
}
int s=0;
for(int i=1;i<=n;i++){
for(int j=1;j<=n;j++){
if(v[i][j])s++;
}
}
return s;
}
int main(){
cin>>n>>m;
for(int i=1;i<=n;i++){
cin>>a[i];
}
for(int i=1;i<=m;i++){
int x,y;
cin>>x>>y;
cout<<b(x,y)<<endl;
}
return 0;
}