#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;

vector<int>dist;
vector<pair<int,int>> used;

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

    while(!q.empty())
     {
        pair<int,int>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; used.push_back({i0,j0});
             int d0=a[i0][j0]; d=max(d,d0);
             q.push({i0,j0});
            }
		 }
      }
}

void clear()
{
  for(int k=0;k<used.size();k++)
   a[used[k].first][used[k].second]=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) dist.push_back(-1);
    else
     {used.clear();
      int d=0; bfs(i,j,d); dist.push_back(d);
      clear();
	 }
	}

   cout << dist[0];
   for(int i=1;i<dist.size();i++) cout << " " << dist[i];
   cout << endl;

}


