#include<iostream>
#include<queue>
using namespace std;

const int M=510;
const int N=510;
int a[M][N];
int m,n;
const int AST=-2, DOT=-1;

int di[]={1,-1,0,0};
int dj[]={0,0,1,-1};
int bfs(int i, int j)
{
    queue<pair<int,int>> q;
	a[i][j]=0; q.push({i,j});
	int d=0;

    while(!q.empty())
     {
        auto p = q.front(); q.pop();
		i=p.first; j=p.second;
		for(int k=0;k<4;k++)
		 {
		   int i0=i+di[k]; int j0=j+dj[k];
		   if(a[i0][j0]==DOT)
			{a[i0][j0]=a[i][j]+1;
             d=a[i0][j0]; q.push({i0,j0});
            }
		 }
      }
   return d;
}

void clear()
{
  for(int i=1;i<=m;i++)
  for(int j=1;j<=n;j++)
  if(a[i][j]>DOT) a[i][j]=DOT;
}


int main()
{
   ios_base::sync_with_stdio(false);
   cin.tie(NULL); cout.tie(NULL);
   
   cin >> m >> n;

   for(int j=0;j<n+1;j++) {a[0][j]=a[m+1][j]=AST;}
   for(int i=1;i<=m;i++)
	{
	  a[i][0]=a[i][n+1]=AST;
	  for(int j=1;j<=n;j++)
		{ char c; cin >> c;
          if(c=='*') a[i][j]=AST; else a[i][j]=DOT;
		}
	}

   int k; cin >> k;
   for(int t=0;t<k;t++)
   {
     int i,j; cin >> i >> j;
     if(a[i][j]==AST)
      {if(t>0) cout << " "; cout << -1;}
     else
      {
	   clear(); int d=bfs(i,j);
       if(t>0) cout << " "; cout << d;
	  }
   }
   cout << endl;
}


