#include<iostream>
#include<queue>
using namespace std;
int n,m,visited[105][205],dist[105][105],q,sx,sy;
char a[105][105];
queue <int> qx, qy;
int yd[4][2] = {{0,1},{0,-1},{-1,0},{1,0}};
int main() {
cin>>n>>m;
for(int i = 1; i <= n; i++) {
for(int j = 1; j <= m; j++) {
cin>>a[i][j];
}
}
cin>>sx>>sy;
qx.push(sx), qy.push(sy);
visited[sx][sy] = 1;
dist[sx][sy] = 0;
while(!qx.empty()) {
int nx = qx.front(), ny = qy.front();
qx.pop(), qy.pop();
for(int i = 0; i < 4; i++) {
int tx = nx + yd[i][0], ty = ny + yd[i][1];
if(tx > 0 && tx <= n && ty > 0 && ty <= m) {
if(visited[tx][ty] == 0 && a[tx][ty] != '#') {
qx.push(tx), qy.push(ty);
visited[tx][ty] = 1;
dist[tx][ty] = dist[nx][ny] + 1;
}
}
}
}
cin>>q;
int xx,yy;
for(int i = 1; i <= q; i++) {
cin>>xx>>yy;
if(a[xx][yy] == '#' || visited[xx][yy] == 0) {
cout<<"-1\n";
}
else cout<<dist[xx][yy]<<endl;
}
return 0;
}