#include <cstdio>
#include <cstring>
#include <algorithm>
#include <iostream>
#include <sstream>
#include <vector>
using namespace std;
typedef long long LL;
const int N = 1000010,M = 20;
typedef pair<int,int> PII;
vector<PII> qry[N];
int h[N],ne[N * 4],e[N * 4],idx;
int fa[N][M];
int dist[N];
int ans[N];
string str[1010];
//int g[1010][1010*2];
int cnt;
int p[N];
int dx[4] = {-1,0,1,0} , dy[4] = {0,1,0,-1};
int q[N];
int st[N];
bool vis[N];
int n,m,s;
int get_p(int x,int y)
{
return ((x-2)*m + (y+1)/2);
}
void add(int a,int b)
{
//printf("%d %d\n",a,b);
e[idx] = b,ne[idx] = h[a],h[a] = idx ++;
}
int find(int x)
{
if(p[x]!=x) p[x] = find(p[x]);
return p[x];
}
void bfs()
{
int hh = 0,tt = 0;
memset(dist,0x3f,sizeof dist);
q[0] = 1;
dist[1] = 0;
while(hh <= tt)
{
int t = q[hh++];
for(int i=h[t];~i;i=ne[i])
{
int j = e[i];
if(dist[j] > dist[t] + 1)
{
dist[j] = dist[t] + 1;
q[++tt] = j;
}
}
}
}
void tarjan(int u)
{
st[u] = 1;
//printf("\n--%d:\n",&u);
for(int i=h[u];~i;i=ne[i])
{
int j = e[i];
if(!st[j])
{
tarjan(j);
p[j] = u;
//printf("%d ",j);
}
}
for(PII t : qry[u])
{
int a = t.first , id = t.second;
if(st[a]==2)
{
int anc = find(a);
ans[id] = dist[u] + dist[a] - dist[anc] * 2;
//if(id==6) cout << u << " "<<dist[u] << " "<< a << " " << dist[a] << " " <<anc << " " <<dist[anc]<< endl;
}
}
st[u] = 2;
}
int main()
{
scanf("%d%d",&n,&m);
getchar();
for(int i=1; i<=n+1; i++) getline(cin,str[i]);
/*for(int i=1;i<=n+1;i++)
for(int j=1;j<=m*2;j++)
{
if(str[i][j] =='|') g[i][j] = 1;
else if(str[i][j] =='_') g[i][j] = 2;
else g[i][j] = 3;
//cout << a << " " << g[i][j] << endl;
}*/
/*for(int i=1;i<=n+1;i++)
{
for(int j=1;j<=m*2;j++)
printf("%d",g[i][j]);
puts("");
}*/
memset(h,-1,sizeof h);
for(int i=2;i<=n+1;i++)
{
for(int j=1;j<=m*2;j+=2)
{
if(str[i][j]==' ')
{
for(int u=0;u<4;u++)
{
int a = i+dx[u] , b = j + dy[u];
if(a<=1 || a > n+1 || b<=0 || b >= m*2) continue;
if(str[a][b]==' ' || u==2)
{
if(u == 0) add(get_p(i,j),get_p(a,b));
else if(u == 1) add(get_p(i,j),get_p(a,b+1));
else if(u == 2) add(get_p(i,j),get_p(a,b));
else add(get_p(i,j),get_p(a,b-1));
}
}
}else if(str[i][j] == '_')
{
for(int u = 0;u<4; u++)
{
int a = i+dx[u] , b = j + dy[u];
if(a<=1 || a > n+1 || b<=0 || b >= m*2) continue;
if(str[a][b]==' ')
{
if(u == 0) add(get_p(i,j),get_p(a,b));
else if(u == 1) add(get_p(i,j),get_p(a,b+1));
else if(u == 3) add(get_p(i,j),get_p(a,b-1));
}
}
}
}
}
for(int i=1;i<=n*m;i++) p[i] = i;
bfs();
//for(int i=1;i<=n*m;i++) printf("%d ",dist[i]);
scanf("%d",&s);
int lx,ly,x,y;
LL res = 0;
for(int i=1;i<=s;i++)
{
scanf("%d%d",&x,&y);
if(i!=1)
{
qry[(x-1)*m+y].push_back({(lx-1)*m+ly,i});
qry[(lx-1)*m+ly].push_back({(x-1)*m+y,i});
}
lx = x,ly = y;
}
tarjan(1);
for(int i=1;i<=s;i++) res += ans[i];
printf("%lld\n",res);
return 0;
}