NirvanaCeleste @ 2024-04-05 11:31:30
#include <bits/stdc++.h>
#include <deque>
using namespace std;
int n,m,i,j,temp;
char a[1005][1005];
bool b[1005][1005];
long long ans[100001];
typedef pair <int,int> PII;
deque<PII> mydeque;
inline void read(int& qwq){
int awa = 0,w = 1;
char ch = getchar();
while(ch < '0' || ch > '9'){
if(ch == '-') w = -1;
ch = getchar();
}
while(ch >= '0' && ch <= '9'){
awa = awa * 10 + ch - '0';
ch = getchar();
}
qwq = awa * w;
}
void bfs(int x,int y){
long long myans = 0;
mydeque.push_back({x,y});//这里不写push_front
while(mydeque.size()){
PII t = mydeque.front();
mydeque.pop_front();
if(b[t.first][t.second] == 1) continue;
b[t.first][t.second] = 1;
if(t.first + 1 <= n && a[t.first][t.second] != a[t.first + 1][t.second]) myans++,mydeque.push_back({t.first + 1,t.second});
if(t.first - 1 >= 1 && a[t.first][t.second] != a[t.first - 1][t.second]) myans++,mydeque.push_back({t.first - 1,t.second});
if(t.second + 1 <= n && a[t.first][t.second] != a[t.first][t.second + 1]) myans++,mydeque.push_back({t.first,t.second + 1});
if(t.second - 1 >= 1 && a[t.first][t.second] != a[t.first][t.second - 1]) myans++,mydeque.push_back({t.first,t.second - 1});
}
ans[++temp] = myans;
}
int main(){
scanf("%d %d",&n,&m);
for(int c=1;c<=n;c++) scanf("%s",a[c]);
for(int c=1;c<=m;c++){
read(i);read(j);
bfs(i,j);
memset(b,0,sizeof(b));
}
for(int c=1;c<temp;c++) printf("%lld\n",ans[c]);
printf("%lld",ans[temp]);
return 0;
}