#include <bits/stdc++.h>
using namespace std;
const int N = 1001;
int n, m, tx, ty;
char mp1[N][N];
int mp2[N][N];
struct inf {
int x, y, z;
} temp, site[N * N];
queue<inf> q;
int dx[5] = {0, 1, -1, 0, 0};
int dy[5] = {0, 0, 0, -1, 1};
void bfs(int x, int y) {
int ans = 1;
while (!q.empty()) {
for (int i = 1; i <= 4; i++) {
temp = q.front();
tx = temp.x, ty = temp.y;
tx += dx[i], ty += dy[i];
if (tx < 1 || tx > n || ty < 1 || ty > n || mp2[tx][ty] != -1 || mp1[tx][ty] == mp1[tx - dx[i]][ty - dy[i]])
continue;
temp.x = tx, temp.y = ty, temp.z++;
mp2[tx][ty] = temp.z;
q.push(temp);
ans++;
}
q.pop();
}
printf("%d\n", ans);
}
int main() {
scanf("%d%d", &n, &m);
for (int i = 1; i <= n; i++)
for (int j = 1; j <= n; j++)
cin >> mp1[i][j];
for (int i = 1; i <= m; i++) {
int x, y;
scanf("%d%d", &x, &y);
memset(mp2, -1, sizeof(mp2));
mp2[x][y] = 1;
temp.x = x, temp.y = y, temp.z = mp2[x][y];
q.push(temp);
bfs(x, y);
}
return 0;
}