#include <bits/stdc++.h>
using namespace std;

int n,m,k;
char mat[1005][1005];
int x,y;
bool vis[1005][1005];
bool seen[1005][1005][4];
int aux[1005][1005];
int sol;
vector<int> ux,uy;

void dfs(int x,int y)
{
    vis[x][y]=true;
    ux.push_back(x);
    uy.push_back(y);
    if(mat[x+1][y]=='.' && !vis[x+1][y]) dfs(x+1,y);
    else if(mat[x+1][y]=='*' && !seen[x+1][y][0]) sol++,seen[x+1][y][0]=true;
    if(mat[x][y+1]=='.' && !vis[x][y+1]) dfs(x,y+1);
    else if(mat[x][y+1]=='*' && !seen[x][y+1][1]) sol++,seen[x][y+1][1]=true;
    if(mat[x-1][y]=='.' && !vis[x-1][y]) dfs(x-1,y);
    else if(mat[x-1][y]=='*' && !seen[x-1][y][2]) sol++,seen[x-1][y][2]=true;
    if(mat[x][y-1]=='.' && !vis[x][y-1]) dfs(x,y-1);
    else if(mat[x][y-1]=='*' && !seen[x][y-1][3]) sol++,seen[x][y-1][3]=true;
}

void room()
{
    for(int i=0; i<ux.size(); i++)
        for(int j=0; j<uy.size(); j++)
            if(vis[ux[i]][uy[j]])
                aux[ux[i]][uy[j]]=sol;
}

void reset()
{
    sol=0;
    for(int i=0; i<ux.size(); i++)
        for(int j=0; j<uy.size(); j++)
            vis[ux[i]][uy[j]]=false;
    for(int i=0; i<ux.size(); i++)
        for(int j=0; j<uy.size(); j++)
            for(int k=0; k<4; k++)
                seen[ux[i]][uy[j]][k]=false;
    ux.empty();
    uy.empty();
}

int main()
{
    scanf("%d%d%d",&n,&m,&k);
    for(int i=0; i<n; i++) scanf("%s",mat[i]);
    while(k--)
    {
        scanf("%d%d",&x,&y);
        if(aux[x-1][y-1])
        {
            sol=aux[x-1][y-1];
            printf("%d\n",sol);
            sol=0;
        }
        else
        {
            dfs(x-1,y-1);
            room();
            printf("%d\n",sol);
            reset();
        }
    }
    return 0;
}


#include <bits/stdc++.h>
using namespace std;

int n,m,k;
char mat[1005][1005];
int x,y;
bool vis[1005][1005];
bool seen[1005][1005][4];
int aux[1005][1005];
vector<int> ux,uy;
int sol;

void dfs(int x,int y)
{
    vis[x][y]=true;
    ux.push_back(x);
    uy.push_back(y);
    if(mat[x+1][y]=='.' && !vis[x+1][y]) dfs(x+1,y);
    else if(mat[x+1][y]=='*' && !seen[x+1][y][0]) sol++,seen[x+1][y][0]=true;
    if(mat[x][y+1]=='.' && !vis[x][y+1]) dfs(x,y+1);
    else if(mat[x][y+1]=='*' && !seen[x][y+1][1]) sol++,seen[x][y+1][1]=true;
    if(mat[x-1][y]=='.' && !vis[x-1][y]) dfs(x-1,y);
    else if(mat[x-1][y]=='*' && !seen[x-1][y][2]) sol++,seen[x-1][y][2]=true;
    if(mat[x][y-1]=='.' && !vis[x][y-1]) dfs(x,y-1);
    else if(mat[x][y-1]=='*' && !seen[x][y-1][3]) sol++,seen[x][y-1][3]=true;
}

void room()
{
    for(int i=0; i<ux.size(); i++)
        aux[ux[i]][uy[i]]=sol;
}

void reset()
{
    sol=0;
    for(int i=0; i<ux.size(); i++)
        vis[ux[i]][uy[i]]=false;
    for(int i=0; i<ux.size(); i++)
        for(int k=0; k<4; k++)
            seen[ux[i]][uy[i]][k]=false;
    ux.clear();
    uy.clear();
}

int main()
{
    scanf("%d%d%d",&n,&m,&k);
    for(int i=0; i<n; i++) scanf("%s",mat[i]);
    while(k--)
    {
        scanf("%d%d",&x,&y);
        if(aux[x-1][y-1])
        {
            sol=aux[x-1][y-1];
            printf("%d\n",sol);
            sol=0;
        }
        else
        {
            dfs(x-1,y-1);
            room();
            printf("%d\n",sol);
            reset();
        }
    }
    return 0;
}


#include <bits/stdc++.h>
using namespace std;

int n,m,k;
char mat[1005][1005];
int x,y;
bool vis[1005][1005];
int aux[1005][1005];
vector<int> ux,uy;
int sol;

void dfs(int x,int y)
{
    if(vis[x][y]) return;
    if(mat[x][y]=='*')
    {
        sol++;
        return;
    }
    vis[x][y]=true;
    ux.push_back(x);
    uy.push_back(y);
    dfs(x+1,y);
    dfs(x,y+1);
    dfs(x-1,y);
    dfs(x,y-1);
}

void room()
{
    for(int i=0; i<ux.size(); i++)
        for(int j=0; j<uy.size(); j++)
            if(vis[ux[i]][uy[j]])
                aux[ux[i]][uy[j]]=sol;
}

void reset()
{
    sol=0;
    for(int i=0; i<ux.size(); i++)
        for(int j=0; j<uy.size(); j++)
            vis[ux[i]][uy[j]]=false;
    ux.clear();
    uy.clear();
}

int main()
{
    scanf("%d%d%d",&n,&m,&k);
    for(int i=0; i<n; i++) scanf("%s",mat[i]);
    while(k--)
    {
        scanf("%d%d",&x,&y);
        if(aux[x-1][y-1])
        {
            sol=aux[x-1][y-1];
            printf("%d\n",sol);
            sol=0;
        }
        else
        {
            dfs(x-1,y-1);
            room();
            printf("%d\n",sol);
            reset();
        }
    }
    return 0;
}


#include <bits/stdc++.h>
using namespace std;

int n,m,k;
char mat[1005][1005];
int x,y;
bool vis[1005][1005];
int aux[1005][1005];
vector<int> ux,uy;
int sol;

void dfs(int x,int y)
{
    if(vis[x][y]) return;
    if(mat[x][y]=='*')
    {
        sol++;
        return;
    }
    vis[x][y]=true;
    ux.push_back(x);
    uy.push_back(y);
    dfs(x+1,y);
    dfs(x,y+1);
    dfs(x-1,y);
    dfs(x,y-1);
}

void room()
{
    for(int i=0; i<ux.size(); i++)
        for(int j=0; j<ux.size(); j++)
            if(vis[ux[i]][uy[j]])
                aux[ux[i]][uy[j]]=sol;
}

void reset()
{
    sol=0;
    for(int i=0; i<ux.size(); i++)
        for(int j=0; j<ux.size(); j++)
            vis[ux[i]][uy[j]]=false;
    ux.clear();
    uy.clear();
}

int main()
{
    scanf("%d%d%d",&n,&m,&k);
    for(int i=0; i<n; i++) scanf("%s",mat[i]);
    while(k--)
    {
        scanf("%d%d",&x,&y);
        if(aux[x-1][y-1])
        {
            sol=aux[x-1][y-1];
            printf("%d\n",sol);
            sol=0;
        }
        else
        {
            dfs(x-1,y-1);
            room();
            printf("%d\n",sol);
            reset();
        }
    }
    return 0;
}


#include <bits/stdc++.h>
using namespace std;

int n,m,k;
char mat[1005][1005];
int x,y;
bool vis[1005][1005];
bool seen[1005][1005][4];
int pre[1005][1005];
vector<int> ux,uy;
int sol;

void dfs(int x,int y)
{
    vis[x][y]=true;
    ux.push_back(x);
    uy.push_back(y);
    if(mat[x+1][y]=='.' && !vis[x+1][y]) dfs(x+1,y);
    else if(mat[x+1][y]=='*' && !seen[x+1][y][0]) sol++,seen[x+1][y][0]=true;
    if(mat[x][y+1]=='.' && !vis[x][y+1]) dfs(x,y+1);
    else if(mat[x][y+1]=='*' && !seen[x][y+1][1]) sol++,seen[x][y+1][1]=true;
    if(mat[x-1][y]=='.' && !vis[x-1][y]) dfs(x-1,y);
    else if(mat[x-1][y]=='*' && !seen[x-1][y][2]) sol++,seen[x-1][y][2]=true;
    if(mat[x][y-1]=='.' && !vis[x][y-1]) dfs(x,y-1);
    else if(mat[x][y-1]=='*' && !seen[x][y-1][3]) sol++,seen[x][y-1][3]=true;
}

void room()
{
    for(int i=0; i<ux.size(); i++)
        for(int j=0; j<uy.size(); j++)
            if(vis[ux[i]][uy[j]])
                pre[ux[i]][uy[j]]=sol;
}

void reset()
{
    sol=0;
    for(int i=0; i<ux.size(); i++)
        for(int j=0; j<uy.size(); j++)
            vis[ux[i]][uy[j]]=false;
    for(int i=0; i<ux.size(); i++)
        for(int j=0; j<uy.size(); j++)
            for(int k=0; k<4; k++)
                seen[ux[i]][uy[j]][k]=false;
    ux.clear();
    uy.clear();
}

int main()
{
    scanf("%d%d%d",&n,&m,&k);
    for(int i=0; i<n; i++) scanf("%s",mat[i]);
    for(int i=0; i<n; i++)
    {
        for(int j=0; j<m; j++)
        {
            if(mat[i][j]=='.' && !pre[i][j])
            {
                dfs(i,j);
                room();
                reset();
            }
        }
    }
    while(k--)
    {
        scanf("%d%d",&x,&y);
        printf("%d\n",pre[x-1][y-1]);
    }
    return 0;
}


#include <bits/stdc++.h>
using namespace std;

int n,m,k;
char mat[1005][1005];
int x,y;
bool vis[1005][1005];
int aux[1005][1005];
vector<int> ux,uy;
int sol;

void dfs(int x,int y)
{
    if(vis[x][y]) return;
    if(mat[x][y]=='*')
    {
        sol++;
        return;
    }
    vis[x][y]=true;
    ux.push_back(x);
    uy.push_back(y);
    dfs(x+1,y);
    dfs(x,y+1);
    dfs(x-1,y);
    dfs(x,y-1);
}

void room()
{
    for(int i=0; i<ux.size(); i++)
        aux[ux[i]][uy[i]]=sol;
}

void reset()
{
    sol=0;
    for(int i=0; i<ux.size(); i++)
        vis[ux[i]][uy[i]]=false;
    ux.clear();
    uy.clear();
}

int main()
{
    scanf("%d%d%d",&n,&m,&k);
    for(int i=0; i<n; i++) scanf("%s",mat[i]);
    while(k--)
    {
        scanf("%d%d",&x,&y);
        if(aux[x-1][y-1])
        {
            sol=aux[x-1][y-1];
            printf("%d\n",sol);
            sol=0;
        }
        else
        {
            dfs(x-1,y-1);
            room();
            printf("%d\n",sol);
            reset();
        }
    }
    return 0;
}


#include <bits/stdc++.h>
using namespace std;

int n,m,k;
char mat[1005][1005];
int x,y;
bool vis[1005][1005];
int aux[1005][1005];
vector<int> ux,uy;
int sol;

void dfs(int x,int y)
{
    if(vis[x][y]) return;
    if(mat[x][y]=='*')
    {
        sol++;
        return;
    }
    vis[x][y]=true;
    ux.push_back(x);
    uy.push_back(y);
    dfs(x+1,y);
    dfs(x,y+1);
    dfs(x-1,y);
    dfs(x,y-1);
}

void room()
{
    for(int i=0; i<ux.size(); i++)
        for(int j=0; j<uy.size(); j++)
            aux[ux[i]][uy[j]]=sol;
}

void reset()
{
    sol=0;
    for(int i=0; i<ux.size(); i++)
        for(int j=0; j<uy.size(); j++)
            vis[ux[i]][uy[j]]=false;
    ux.clear();
    uy.clear();
}

int main()
{
    scanf("%d%d%d",&n,&m,&k);
    for(int i=0; i<n; i++) scanf("%s",mat[i]);
    while(k--)
    {
        scanf("%d%d",&x,&y);
        if(aux[x-1][y-1])
        {
            sol=aux[x-1][y-1];
            printf("%d\n",sol);
            sol=0;
        }
        else
        {
            dfs(x-1,y-1);
            room();
            printf("%d\n",sol);
            reset();
        }
    }
    return 0;
}


#include <bits/stdc++.h>
using namespace std;

int n,m,k;
char mat[1005][1005];
int x,y;
bool vis[1005][1005];
bool seen[1005][1005][4];
int aux[1005][1005];
int sol;
vector<int> ux,uy;

void dfs(int x,int y)
{
    vis[x][y]=true;
    ux.push_back(x);
    uy.push_back(y);
    if(mat[x+1][y]=='.' && !vis[x+1][y]) dfs(x+1,y);
    else if(mat[x+1][y]=='*' && !seen[x+1][y][0]) sol++,seen[x+1][y][0]=true;
    if(mat[x][y+1]=='.' && !vis[x][y+1]) dfs(x,y+1);
    else if(mat[x][y+1]=='*' && !seen[x][y+1][1]) sol++,seen[x][y+1][1]=true;
    if(mat[x-1][y]=='.' && !vis[x-1][y]) dfs(x-1,y);
    else if(mat[x-1][y]=='*' && !seen[x-1][y][2]) sol++,seen[x-1][y][2]=true;
    if(mat[x][y-1]=='.' && !vis[x][y-1]) dfs(x,y-1);
    else if(mat[x][y-1]=='*' && !seen[x][y-1][3]) sol++,seen[x][y-1][3]=true;
}

void reset()
{
    for(int i=0; i<ux.size(); i++)
        for(int j=0; j<uy.size(); j++)
            vis[ux[i]][uy[j]]=false;
    for(int i=0; i<ux.size(); i++)
        for(int j=0; j<uy.size(); j++)
            for(int k=0; k<4; k++)
                seen[ux[i]][uy[j]][k]=false;
    ux.empty();
    uy.empty();
}

int main()
{
    scanf("%d%d%d",&n,&m,&k);
    for(int i=0; i<n; i++) scanf("%s",mat[i]);
    while(k--)
    {
        scanf("%d%d",&x,&y);
        if(aux[x-1][y-1])
        {
            sol=aux[x-1][y-1];
            printf("%d\n",sol);
            sol=0;
        }
        else
        {
            dfs(x-1,y-1);
            for(int i=0; i<n; i++)
                for(int j=0; j<m; j++)
                    if(vis[i][j])
                        aux[i][j]=sol;
            printf("%d\n",sol);
            sol=0;
            reset();
        }
    }
    return 0;
}


#include <bits/stdc++.h>
using namespace std;

int n,m,k;
char mat[1005][1005];
int x,y;
bool vis[1005][1005];
bool seen[1005][1005][4];
int pre[1005][1005];
vector<int> ux,uy;
int sol;

void dfs(int x,int y)
{
    vis[x][y]=true;
    ux.push_back(x);
    uy.push_back(y);
    if(mat[x+1][y]=='.' && !vis[x+1][y]) dfs(x+1,y);
    else if(mat[x+1][y]=='*' && !seen[x+1][y][0]) sol++,seen[x+1][y][0]=true;
    if(mat[x][y+1]=='.' && !vis[x][y+1]) dfs(x,y+1);
    else if(mat[x][y+1]=='*' && !seen[x][y+1][1]) sol++,seen[x][y+1][1]=true;
    if(mat[x-1][y]=='.' && !vis[x-1][y]) dfs(x-1,y);
    else if(mat[x-1][y]=='*' && !seen[x-1][y][2]) sol++,seen[x-1][y][2]=true;
    if(mat[x][y-1]=='.' && !vis[x][y-1]) dfs(x,y-1);
    else if(mat[x][y-1]=='*' && !seen[x][y-1][3]) sol++,seen[x][y-1][3]=true;
}

void room()
{
    for(int i=0; i<ux.size(); i++)
        for(int j=0; j<uy.size(); j++)ž
            if(vis[ux[i]][uy[j]])
                pre[ux[i]][uy[j]]=sol;
}

void reset()
{
    sol=0;
    for(int i=0; i<ux.size(); i++)
        for(int j=0; j<uy.size(); j++)
            vis[ux[i]][uy[j]]=false;
    for(int i=0; i<ux.size(); i++)
        for(int j=0; j<uy.size(); j++)
            for(int k=0; k<4; k++)
                seen[ux[i]][uy[j]][k]=false;
    ux.clear();
    uy.clear();
}

int main()
{
    scanf("%d%d%d",&n,&m,&k);
    for(int i=0; i<n; i++) scanf("%s",mat[i]);
    for(int i=0; i<n; i++)
    {
        for(int j=0; j<m; j++)
        {
            if(mat[i][j]=='.' && !pre[i][j])
            {
                dfs(i,j);
                room();
                reset();
            }
        }
    }
    while(k--)
    {
        scanf("%d%d",&x,&y);
        printf("%d\n",pre[x-1][y-1]);
    }
    return 0;
}


#include<iostream>
#include<algorithm>
#include<stdio.h>
#include<math.h>
#include<stdlib.h>
#include<string>
#include<queue>
#include<set>
#define intt long long
using namespace std;
int dx[10]= {1,-1,0,0};
int dy[10]= {0,0,-1,1};
struct code
{
    int x,y;
};
int res,k;
string s;
int gcd(int a , int b)
{
    while(b)
    {
        a%=b;
        swap(a,b);
    }
    return a;
}
bool compare(code a , code b)
{
    return a.x<b.x or (a.x==b.x and a.y<b.y);
}
char a[1001][1001];
int used[1001][1001];
int path[1001][1001]= {0};
queue<pair<int,int> >q;
main()
{
    int n,m,k;
    cin>>n>>m>>k;

    for(int i=1; i<=n; i++)
    {
        for(int j=1; j<=m; j++)
        {
            cin>>a[i][j];
        }
    }
    pair<int,int>to;
    int xx,yy,X,Y,x,y,q1,q2;
    while(k--)
    {
        int cnt=0;
        cin>>x>>y;
        X=x;
        Y=y;
        if(used[x][y]!=0)
        {
            cout<<used[X][Y]<<endl;
            continue;
        }
        else
        {
            q.push(make_pair(x,y));
            path[x][y]=1;
            while(q.size())
            {
                to=q.front();
                q.pop();
                x=to.first;
                y=to.second;
                for(int l=0; l<4; l++)
                {
                    q1=x+dx[l];
                    q2=y+dy[l];
                    if(a[q1][q2]=='*')
                    {
                        cnt++;
                    }
                }
                for(int j=0; j<4; j++)
                {
                    xx=x+dx[j];
                    yy=y+dy[j];
                    if(xx>=1 and xx<=n and yy<=m and yy>=1 and path[xx][yy]==0 and a[xx][yy]=='.')
                    {
                        path[xx][yy]=1;
                        q.push(make_pair(xx,yy));
                    }
                }
            }
            q.push(make_pair(X,Y));
            used[X][Y]=cnt;
            path[x][y]=0;
            while(q.size())
            {
                to=q.front();
                q.pop();
                x=to.first;
                y=to.second;
                for(int j=0; j<4; j++)
                {
                    xx=x+dx[j];
                    yy=y+dy[j];
                    if(xx>=1 and xx<=n and yy<=m and yy>=1 and a[xx][yy]=='.')
                    {
                        path[x][y]=0;
                        used[xx][yy]=cnt;
                    }
                }
            }
        }
        cout<<used[X][Y]<<endl;
    }
}


#include<iostream>
#include<algorithm>
#include<stdio.h>
#include<math.h>
#include<stdlib.h>
#include<string>
#include<queue>
#include<set>
#define intt long long
using namespace std;
int dx[10]= {1,-1,0,0};
int dy[10]= {0,0,-1,1};
struct code
{
    int x,y;
};
int res,k;
string s;
int gcd(int a , int b)
{
    while(b)
    {
        a%=b;
        swap(a,b);
    }
    return a;
}
bool compare(code a , code b)
{
    return a.x<b.x or (a.x==b.x and a.y<b.y);
}
char a[1001][1001];
int used[1001][1001];
int path[1001][1001]= {0};
queue<pair<int,int> >q;
main()
{
    int n,m,k;
    cin>>n>>m>>k;

    for(int i=1; i<=n; i++)
    {
        for(int j=1; j<=m; j++)
        {
            cin>>a[i][j];
        }
    }
    pair<int,int>to;
    int xx,yy,X,Y,x,y,q1,q2;
    while(k--)
    {
        int cnt=0;
        cin>>x>>y;
        X=x;
        Y=y;
//        if(used[x][y]=0)
//        {
//            cout<<used[X][Y]<<endl;
//            continue;
//        }
//        else
        {
            q.push(make_pair(x,y));
            path[x][y]=1;
            while(q.size())
            {
                to=q.front();
                q.pop();
                x=to.first;
                y=to.second;
                for(int l=0; l<4; l++)
                {
                    q1=x+dx[l];
                    q2=y+dy[l];
                    if(a[q1][q2]=='*' and a[x][y]=='.')
                    {
                        cnt++;
                    }
                }
                for(int j=0; j<4; j++)
                {
                    xx=x+dx[j];
                    yy=y+dy[j];
                    if(xx>=1 and xx<=n and yy<=m and yy>=1 and path[xx][yy]==0 and a[xx][yy]=='.')
                    {
                        path[xx][yy]=1;
                        q.push(make_pair(xx,yy));
                    }
                }
            }
            q.push(make_pair(X,Y));
            while(q.size())
            {
                to=q.front();
                q.pop();
                x=to.first;
                y=to.second;
                used[x][y]=cnt;
                path[x][y]=0;
                for(int j=0; j<4; j++)
                {
                    xx=x+dx[j];
                    yy=y+dy[j];
                    if(xx>=1 and xx<=n and yy<=m and yy>=1 and a[xx][yy]=='.' and path[xx][yy]==1)
                    {
                        q.push(make_pair(xx,yy));
                    }
                }
            }
        }
        cout<<used[X][Y]<<endl;
    }
}


#include<iostream>
#include<algorithm>
#include<stdio.h>
#include<math.h>
#include<stdlib.h>
#include<string>
#include<queue>
#include<set>
#define intt long long
using namespace std;
int dx[10]= {1,-1,0,0};
int dy[10]= {0,0,-1,1};
struct code
{
    int x,y;
};
int res,k;
string s;
int gcd(int a , int b)
{
    while(b)
    {
        a%=b;
        swap(a,b);
    }
    return a;
}
bool compare(code a , code b)
{
    return a.x<b.x or (a.x==b.x and a.y<b.y);
}
char a[1001][1001];
int used[1001][1001];
int path[1001][1001]= {0};
queue<pair<int,int> >q;
main()
{
    int n,m,k;
    cin>>n>>m>>k;

    for(int i=1; i<=n; i++)
    {
        for(int j=1; j<=m; j++)
        {
            cin>>a[i][j];
        }
    }
    pair<int,int>to;
    int xx,yy,X,Y,x,y,q1,q2;
    while(k--)
    {
        int cnt=0;
        cin>>x>>y;
        X=x;
        Y=y;
        if(used[x][y]!=0)
        {
            cout<<used[X][Y]<<endl;
            continue;
        }
        else
        {
            q.push(make_pair(x,y));
            path[x][y]=1;
            while(q.size())
            {
                to=q.front();
                q.pop();
                x=to.first;
                y=to.second;
                for(int l=0; l<4; l++)
                {
                    q1=x+dx[l];
                    q2=y+dy[l];
                    if(a[q1][q2]=='*')
                    {
                        cnt++;
                    }
                }
                for(int j=0; j<4; j++)
                {
                    xx=x+dx[j];
                    yy=y+dy[j];
                    if(xx>=1 and xx<=n and yy<=m and yy>=1 and path[xx][yy]==0 and a[xx][yy]=='.')
                    {
                        path[xx][yy]=1;
                        q.push(make_pair(xx,yy));
                    }
                }
            }
            q.push(make_pair(X,Y));
            path[x][y]=0;
            while(q.size())
            {
                to=q.front();
                q.pop();
                x=to.first;
                y=to.second;
                used[x][y]=cnt;
                for(int j=0; j<4; j++)
                {
                    xx=x+dx[j];
                    yy=y+dy[j];
                    if(xx>=1 and xx<=n and yy<=m and yy>=1 and a[xx][yy]=='.')
                    {
                        path[x][y]=0;
                        used[xx][yy]=cnt;
                        q.push(make_pair(xx,yy));
                    }
                }
            }
        }
        cout<<used[X][Y]<<endl;
    }
}


#include<iostream>
#include<algorithm>
#include<stdio.h>
#include<math.h>
#include<stdlib.h>
#include<string>
#include<queue>
#include<set>
#define intt long long
using namespace std;
int dx[10]= {1,-1,0,0};
int dy[10]= {0,0,-1,1};
struct code
{
    int x,y;
};
int res,k;
string s;
int gcd(int a , int b)
{
    while(b)
    {
        a%=b;
        swap(a,b);
    }
    return a;
}
bool compare(code a , code b)
{
    return a.x<b.x or (a.x==b.x and a.y<b.y);
}
char a[1001][1001];
int used[1001][1001];
int path[1001][1001]= {0};
queue<pair<int,int> >q;
main()
{
    ios::sync_with_stdio(0);
    int n,m,k;
    cin>>n>>m>>k;
    for(int i=1; i<=n; i++)
    {
        for(int j=1; j<=m; j++)
        {
            cin>>a[i][j];
        }
    }
    pair<int,int>to;
    int xx,yy,X,Y,x,y,q1,q2;
    while(k--)
    {
        int cnt=0;
        cin>>x>>y;
        X=x;
        Y=y;
//        if(used[x][y])
//        {
//            cout<<used[X][Y]<<endl;
//            continue;
//        }
//        else
//        {
            q.push(make_pair(x,y));
            path[x][y]=1;
            while(q.size())
            {
                to=q.front();
                q.pop();
                x=to.first;
                y=to.second;
                for(int l=0; l<4; l++)
                {
                    q1=x+dx[l];
                    q2=y+dy[l];
                    if(a[q1][q2]=='*' and a[x][y]=='.')
                    {
                        cnt++;
                    }
                }
                for(int j=0; j<4; j++)
                {
                    xx=x+dx[j];
                    yy=y+dy[j];
                    if(xx>=1 and xx<=n and yy<=m and yy>=1 and path[xx][yy]==0 and a[xx][yy]=='.')
                    {
                        path[xx][yy]=1;
                        q.push(make_pair(xx,yy));
                    }
                }
            }

        cout<<cnt<<endl;
    }
}


#include<iostream>
#include<algorithm>
#include<stdio.h>
#include<math.h>
#include<stdlib.h>
#include<string>
#include<queue>
#include<set>
#define intt long long
using namespace std;
int dx[10]= {1,-1,0,0};
int dy[10]= {0,0,-1,1};
char a[1001][1001];
int used[1001][1001];
int path[1001][1001]= {0};
queue<pair<int,int> >q;
int xx,yy,X,Y,x,y,q1,q2;
int n,m,k,i,j,cnt,l;
pair<int,int>to;
main()
{
    ios::sync_with_stdio(0);
    cin>>n>>m>>k;
    for(i=1; i<=n; i++)
    {
        for(j=1; j<=m; j++)
        {
            cin>>a[i][j];
        }
    }
    for(int i=1;i<=1000;i++)
    {
        for(int j=1;j<=1000;j++)
            used[i][j]=-1;
    }
    while(k--)
    {
        cnt=0;
        cin>>x>>y;
        X=x;
        Y=y;
        if(used[x][y] == -1)
        {
            q.push(make_pair(x,y));
            path[x][y]=1;
            while(q.size())
            {
                to=q.front();
                q.pop();
                x=to.first;
                y=to.second;
                for(l=0; l<4; l++)
                {
                    xx=x+dx[l];
                    yy=y+dy[l];
                    if(a[xx][yy]=='*' )
                    {
                        cnt++;
                    }
                }
                for(j=0; j<4; j++)
                {
                    xx=x+dx[j];
                    yy=y+dy[j];
                    if(xx>=1 and xx<=n and yy<=m and yy>=1 and path[xx][yy]==0 and a[xx][yy]=='.')
                    {
                        path[xx][yy]=1;
                        q.push(make_pair(xx,yy));
                    }
                }
            }
            path[X][Y]=0;
            q.push(make_pair(X,Y));
            while(q.size())
            {
                to=q.front();
                q.pop();
                x=to.first;
                y=to.second;
                used[x][y] = cnt;
                for(j=0; j<4; j++)
                {
                    xx=x+dx[j];
                    yy=y+dy[j];
                    if(xx>=1 and xx<=n and yy<=m and yy>=1 and a[xx][yy]=='.' and path[xx][yy]==1)
                    {
                        path[x][y]=0;
                        q.push(make_pair(xx,yy));
                    }
                }
            }
        }
        cout<<used[X][Y]<<endl;
    }
}


#include<iostream>
#include<algorithm>
#include<stdio.h>
#include<math.h>
#include<stdlib.h>
#include<string>
#include<queue>
#include<set>
#define intt long long
using namespace std;
bool dx[10]= {1,-1,0,0};
bool dy[10]= {0,0,-1,1};
char a[1001][1001];
int used[1001][1001];
bool path[1001][1001]= {0};
queue<pair<int,int> >q;
int xx,yy,X,Y,x,y,q1,q2;
int n,m,k,i,j,cnt,l;
pair<int,int>to;
main()
{
    ios::sync_with_stdio(0);
    cin>>n>>m>>k;
    for(i=1; i<=n; i++)
    {
        for(j=1; j<=m; j++)
        {
            cin>>a[i][j];
        }
    }
    while(k--)
    {
        cnt=0;
        cin>>x>>y;
        X=x;
        Y=y;
        if(used[x][y]!=0)
        {
            cout<<used[X][Y]<<endl;
            continue;
        }
        else
        {
            q.push(make_pair(x,y));
            path[x][y]=1;
            while(q.size())
            {
                to=q.front();
                q.pop();
                x=to.first;
                y=to.second;
                for(l=0; l<4; l++)
                {
                    q1=x+dx[l];
                    q2=y+dy[l];
                    if(a[q1][q2]=='*' and a[x][y]=='.')
                    {
                        cnt++;
                    }
                }
                for(j=0; j<4; j++)
                {
                    xx=x+dx[j];
                    yy=y+dy[j];
                    if(xx>=1 and xx<=n and yy<=m and yy>=1 and path[xx][yy]==0 and a[xx][yy]=='.')
                    {
                        path[xx][yy]=1;
                        q.push(make_pair(xx,yy));
                    }
                }
            }
            q.push(make_pair(X,Y));
            while(q.size())
            {
                to=q.front();
                q.pop();
                x=to.first;
                y=to.second;
                used[x][y]=cnt;
                path[x][y]=0;
                for(j=0; j<4; j++)
                {
                    xx=x+dx[j];
                    yy=y+dy[j];
                    if(xx>=1 and xx<=n and yy<=m and yy>=1 and a[xx][yy]=='.' and path[xx][yy]==1)
                    {
                        q.push(make_pair(xx,yy));
                    }
                }
            }
        }
        cout<<used[X][Y]<<endl;
    }
}


#include<iostream>
#include<algorithm>
#include<stdio.h>
#include<math.h>
#include<stdlib.h>
#include<string>
#include<queue>
#include<set>
#define intt long long
using namespace std;
int dx[10]= {1,-1,0,0};
int dy[10]= {0,0,-1,1};
char a[1001][1001];
int used[1001][1001];
int path[1001][1001]= {0};
queue<pair<int,int> >q;
int xx,yy,X,Y,x,y,q1,q2;
int n,m,k,i,j,cnt,l;
pair<int,int>to;
main()
{
    ios::sync_with_stdio(0);
    cin>>n>>m>>k;
    for(i=1; i<=n; i++)
    {
        for(j=1; j<=m; j++)
        {
            cin>>a[i][j];
        }
    }
    while(k--)
    {
        cnt=0;
        cin>>x>>y;
        X=x;
        Y=y;
        if(used[x][y]!=0)
        {
            cout<<used[X][Y]<<endl;
            continue;
        }
        else
        {
            q.push(make_pair(x,y));
            path[x][y]=1;
            while(q.size())
            {
                to=q.front();
                q.pop();
                x=to.first;
                y=to.second;
                for(l=0; l<4; l++)
                {
                    q1=x+dx[l];
                    q2=y+dy[l];
                    if(a[q1][q2]=='*' and a[x][y]=='.')
                    {
                        cnt++;
                    }
                }
                for(j=0; j<4; j++)
                {
                    xx=x+dx[j];
                    yy=y+dy[j];
                    if(xx>=1 and xx<=n and yy<=m and yy>=1 and path[xx][yy]==0 and a[xx][yy]=='.')
                    {
                        path[xx][yy]=1;
                        q.push(make_pair(xx,yy));
                    }
                }
            }
            q.push(make_pair(X,Y));
            while(q.size())
            {
                to=q.front();
                q.pop();
                x=to.first;
                y=to.second;
                used[x][y]=cnt;
                path[x][y]=0;
                for(j=0; j<4; j++)
                {
                    xx=x+dx[j];
                    yy=y+dy[j];
                    if(xx>=1 and xx<=n and yy<=m and yy>=1 and a[xx][yy]=='.' and path[xx][yy]==1)
                    {
                        q.push(make_pair(xx,yy));
                    }
                }
            }
        }
        cout<<used[X][Y]<<endl;
    }
}


#include<iostream>
#include<algorithm>
#include<stdio.h>
#include<math.h>
#include<stdlib.h>
#include<string>
#include<queue>
#include<set>
#define intt long long
using namespace std;
int dx[10]= {1,-1,0,0};
int dy[10]= {0,0,-1,1};
struct code
{
    int x,y;
};
int res,k;
string s;
int gcd(int a , int b)
{
    while(b)
    {
        a%=b;
        swap(a,b);
    }
    return a;
}
bool compare(code a , code b)
{
    return a.x<b.x or (a.x==b.x and a.y<b.y);
}
char a[1001][1001];
int used[1001][1001];
int path[1001][1001]= {0};
queue<pair<int,int> >q;
main()
{
    int n,m,k;
    cin>>n>>m>>k;

    for(int i=1; i<=n; i++)
    {
        for(int j=1; j<=m; j++)
        {
            cin>>a[i][j];
        }
    }
    pair<int,int>to;
    int xx,yy,X,Y,x,y,q1,q2;
    while(k--)
    {
        int cnt=0;
        cin>>x>>y;
        X=x;
        Y=y;
        if(used[x][y]!=0)
        {
            cout<<used[X][Y]<<endl;
            continue;
        }
        else
        {
            q.push(make_pair(x,y));
            path[x][y]=1;
            while(q.size())
            {
                to=q.front();
                q.pop();
                x=to.first;
                y=to.second;
                for(int l=0; l<4; l++)
                {
                    q1=x+dx[l];
                    q2=y+dy[l];
                    if(a[q1][q2]=='*')
                    {
                        cnt++;
                    }
                }
                for(int j=0; j<4; j++)
                {
                    xx=x+dx[j];
                    yy=y+dy[j];
                    if(xx>=1 and xx<=n and yy<=m and yy>=1 and path[xx][yy]==0 and a[xx][yy]=='.')
                    {
                        path[xx][yy]=1;
                        q.push(make_pair(xx,yy));
                    }
                }
            }
            q.push(make_pair(X,Y));
            path[x][y]=0;
            while(q.size())
            {
                to=q.front();
                q.pop();
                x=to.first;
                y=to.second;
                used[x][y]=cnt;
                for(int j=0; j<4; j++)
                {
                    xx=x+dx[j];
                    yy=y+dy[j];
                    if(xx>=1 and xx<=n and yy<=m and yy>=1 and a[xx][yy]=='.' and path[xx][yy]==1)
                    {
                        path[x][y]=0;
                        q.push(make_pair(xx,yy));
                    }
                }
            }
        }
        cout<<used[X][Y]<<endl;
    }
}


#include<iostream>
#include<algorithm>
#include<stdio.h>
#include<math.h>
#include<stdlib.h>
#include<string>
#include<queue>
#include<set>
#define intt long long
using namespace std;
int dx[10]= {1,-1,0,0};
int dy[10]= {0,0,-1,1};
char a[1001][1001];
int used[1001][1001];
int path[1001][1001]= {0};
queue<pair<int,int> >q;
int xx,yy,X,Y,x,y,q1,q2;
int n,m,k,i,j,cnt,l;
pair<int,int>to;
main()
{
    ios::sync_with_stdio(0);
    cin>>n>>m>>k;
    for(i=1; i<=n; i++)
    {
        for(j=1; j<=m; j++)
        {
            cin>>a[i][j];
        }
    }
    while(k--)
    {
        cnt=0;
        cin>>x>>y;
        X=x;
        Y=y;
        if(used[x][y]!=0)
        {
            cout<<used[X][Y]<<endl;
            continue;
        }
        else
        {
            q.push(make_pair(x,y));
            path[x][y]=1;
            while(q.size())
            {
                to=q.front();
                q.pop();
                x=to.first;
                y=to.second;
                for(l=0; l<4; l++)
                {
                    q1=x+dx[l];
                    q2=y+dy[l];
                    if(a[q1][q2]=='*' and a[x][y]=='.')
                    {
                        cnt++;
                    }
                }
                for(j=0; j<4; j++)
                {
                    xx=x+dx[j];
                    yy=y+dy[j];
                    if(xx>=1 and xx<=n and yy<=m and yy>=1 and path[xx][yy]==0 and a[xx][yy]=='.')
                    {
                        path[xx][yy]=1;
                        q.push(make_pair(xx,yy));
                    }
                }
            }
            path[X][Y]=0;
            q.push(make_pair(X,Y));
            while(q.size())
            {
                to=q.front();
                q.pop();
                x=to.first;
                y=to.second;
                used[x][y]=cnt;
                for(j=0; j<4; j++)
                {
                    xx=x+dx[j];
                    yy=y+dy[j];
                    if(xx>=1 and xx<=n and yy<=m and yy>=1 and a[xx][yy]=='.' and path[xx][yy]==1)
                    {
                        path[x][y]=0;
                        q.push(make_pair(xx,yy));
                    }
                }
            }
        }
        cout<<used[X][Y]<<endl;
    }
}


#include<iostream>
#include<algorithm>
#include<stdio.h>
#include<math.h>
#include<stdlib.h>
#include<string>
#include<queue>
#include<set>
#define intt long long
using namespace std;
int dx[10]= {1,-1,0,0};
int dy[10]= {0,0,-1,1};
char a[1001][1001];
int used[1001][1001];
int path[1001][1001];
queue<pair<int,int> >q;
int xx,yy,X,Y,x,y,q1,q2;
int n,m,k,i,j,cnt,l;
pair<int,int>to;
void pushh(int x , int y)
{
    if(path[x][y]==0)
    {
        path[x][y]=1;
        for(int l=0; l<4; l++)
        {
            q1=x+dx[l];
            q2=y+dy[l];
            if(a[q1][q2]=='*' and a[x][y]=='.')
            {
                cnt++;
            }
        }
        for(int j=0; j<4; j++)
        {
            xx=x+dx[j];
            yy=y+dy[j];
            if(xx>=1 and xx<=n and yy<=m and yy>=1 and path[xx][yy]==0 and a[xx][yy]=='.')
            {
                pushh(xx,yy);
            }
        }
    }
}
void push(int x , int y)
{
    if(path[x][y]==1)
    {
        used[x][y]=cnt;
        path[x][y]=0;
        for(int j=0; j<4; j++)
        {
            xx=x+dx[j];
            yy=y+dy[j];
            if(xx>=1 and xx<=n and yy<=m and yy>=1 and a[xx][yy]=='.' and path[xx][yy])
            {
                push(xx,yy);
            }
        }
    }
}
main()
{
    ios::sync_with_stdio(0);
    cin>>n>>m>>k;
    for(i=1; i<=n; i++)
    {
        for(j=1; j<=m; j++)
        {
            cin>>a[i][j];
        }
    }
    while(k--)
    {
        cnt=0;
        cin>>x>>y;
        if(used[x][y]==0)
        {
            pushh(x,y);
            push(x,y);
        }
        cout<<used[x][y]<<endl;
    }
}


#include<iostream>
#include<algorithm>
#include<stdio.h>
#include<math.h>
#include<stdlib.h>
#include<string>
#include<queue>
#include<set>
#define intt long long
using namespace std;
int dx[10]= {1,-1,0,0};
int dy[10]= {0,0,-1,1};
struct code
{
    int x,y;
};
int res,k;
string s;
int gcd(int a , int b)
{
    while(b)
    {
        a%=b;
        swap(a,b);
    }
    return a;
}
bool compare(code a , code b)
{
    return a.x<b.x or (a.x==b.x and a.y<b.y);
}
char a[1001][1001];
int used[1001][1001];
int path[1001][1001]= {0};
queue<pair<int,int> >q;
main()
{
    int n,m,k;
    cin>>n>>m>>k;

    for(int i=1; i<=n; i++)
    {
        for(int j=1; j<=m; j++)
        {
            cin>>a[i][j];
        }
    }
    pair<int,int>to;
    int xx,yy,X,Y,x,y,q1,q2;
    while(k--)
    {
        int cnt=0;
        cin>>x>>y;
        X=x;
        Y=y;
        if(used[x][y]!=0)
        {
            cout<<used[X][Y]<<endl;
            continue;
        }
        else
        {
            q.push(make_pair(x,y));
            path[x][y]=1;
            while(q.size())
            {
                to=q.front();
                q.pop();
                x=to.first;
                y=to.second;
                for(int l=0; l<4; l++)
                {
                    q1=x+dx[l];
                    q2=y+dy[l];
                    if(a[q1][q2]=='*' and a[x][y]=='.')
                    {
                        cnt++;
                    }
                }
                for(int j=0; j<4; j++)
                {
                    xx=x+dx[j];
                    yy=y+dy[j];
                    if(xx>=1 and xx<=n and yy<=m and yy>=1 and path[xx][yy]==0 and a[xx][yy]=='.')
                    {
                        path[xx][yy]=1;
                        q.push(make_pair(xx,yy));
                    }
                }
            }
            q.push(make_pair(X,Y));
            while(q.size())
            {
                to=q.front();
                q.pop();
                x=to.first;
                y=to.second;
                used[x][y]=cnt;
                path[x][y]=0;
                for(int j=0; j<4; j++)
                {
                    xx=x+dx[j];
                    yy=y+dy[j];
                    if(xx>=1 and xx<=n and yy<=m and yy>=1 and a[xx][yy]=='.' and path[xx][yy]==1)
                    {
                        q.push(make_pair(xx,yy));
                    }
                }
            }
        }
        cout<<used[X][Y]<<endl;
    }
}


#include<iostream>
#include<algorithm>
#include<stdio.h>
#include<math.h>
#include<stdlib.h>
#include<string>
#include<queue>
#include<set>
#define intt long long
using namespace std;
int dx[10]= {1,-1,0,0};
int dy[10]= {0,0,-1,1};
struct code
{
    int x,y;
};
int res,k;
string s;
int gcd(int a , int b)
{
    while(b)
    {
        a%=b;
        swap(a,b);
    }
    return a;
}
bool compare(code a , code b)
{
    return a.x<b.x or (a.x==b.x and a.y<b.y);
}
char a[1001][1001];
int used[1001][1001];
int path[1001][1001]= {0};
queue<pair<int,int> >q;
main()
{
    int n,m,k;
    cin>>n>>m>>k;
    ios::sync_with_stdio(0);
    for(int i=1; i<=n; i++)
    {
        for(int j=1; j<=m; j++)
        {
            cin>>a[i][j];
        }
    }
    pair<int,int>to;
    int xx,yy,X,Y,x,y,q1,q2;
    while(k--)
    {
        int cnt=0;
        cin>>x>>y;
        X=x;
        Y=y;
        if(used[x][y]!=0)
        {
            cout<<used[X][Y]<<endl;
            continue;
        }
        else
        {
            q.push(make_pair(x,y));
            path[x][y]=1;
            while(q.size())
            {
                to=q.front();
                q.pop();
                x=to.first;
                y=to.second;
                for(int l=0; l<4; l++)
                {
                    q1=x+dx[l];
                    q2=y+dy[l];
                    if(a[q1][q2]=='*' and a[x][y]=='.')
                    {
                        cnt++;
                    }
                }
                for(int j=0; j<4; j++)
                {
                    xx=x+dx[j];
                    yy=y+dy[j];
                    if(xx>=1 and xx<=n and yy<=m and yy>=1 and path[xx][yy]==0 and a[xx][yy]=='.')
                    {
                        path[xx][yy]=1;
                        q.push(make_pair(xx,yy));
                    }
                }
            }
            q.push(make_pair(X,Y));
            while(q.size())
            {
                to=q.front();
                q.pop();
                x=to.first;
                y=to.second;
                used[x][y]=cnt;
                path[x][y]=0;
                for(int j=0; j<4; j++)
                {
                    xx=x+dx[j];
                    yy=y+dy[j];
                    if(xx>=1 and xx<=n and yy<=m and yy>=1 and a[xx][yy]=='.' and path[xx][yy]==1)
                    {
                        q.push(make_pair(xx,yy));
                    }
                }
            }
        }
        cout<<used[X][Y]<<endl;
    }
}


#include<iostream>
#include<queue>
using namespace std;
int dx[10]= {1,-1,0,0};
int dy[10]= {0,0,-1,1};
char a[1001][1001];
int path[1001][1001];
bool used[1001][1001];
queue<pair<int,int> >q;
int xx,yy,X,Y,x,y,q1,q2;
int n,m,k,i,j,cnt,l;
pair<int,int>to;
main()
{
    ios::sync_with_stdio(0);
    cin>>n>>m>>k;
    for(i=1; i<=n; i++)
    {
        for(j=1; j<=m; j++)
        {
            cin>>a[i][j];
        }
    }
    for(int i=1;i<=1000;i++)
    {
        for(int j=1;j<=1000;j++)
            path[i][j]=-1;
    }
    while(k--)
    {
        cnt=0;
        cin>>x>>y;
        X=x;
        Y=y;
        if(path[x][y] == -1)
        {
            q.push(make_pair(x,y));
            used[x][y]=1;
            while(q.size())
            {
                to=q.front();
                q.pop();
                x=to.first;
                y=to.second;
                for(l=0; l<4; l++)
                {
                    xx=x+dx[l];
                    yy=y+dy[l];
                    if(a[xx][yy]=='*' )
                    {
                        cnt++;
                    }
                }
                for(j=0; j<4; j++)
                {
                    xx=x+dx[j];
                    yy=y+dy[j];
                    if(xx>=1 and xx<=n and yy<=m and yy>=1 and used[xx][yy]==0 and a[xx][yy]=='.')
                    {
                        used[xx][yy]=1;
                        q.push(make_pair(xx,yy));
                    }
                }
            }
            used[X][Y]=0;
            q.push(make_pair(X,Y));
            while(q.size())
            {
                to=q.front();
                q.pop();
                x=to.first;
                y=to.second;
                path[x][y] = cnt;
                for(j=0; j<4; j++)
                {
                    xx=x+dx[j];
                    yy=y+dy[j];
                    if(xx>=1 and xx<=n and yy<=m and yy>=1 and a[xx][yy]=='.' and used[xx][yy]==1)
                    {
                        used[xx][yy]=0;
                        q.push(make_pair(xx,yy));
                    }
                }
            }
        }
        cout<<path[X][Y]<<endl;
    }
}


#include<iostream>
#include<algorithm>
#include<stdio.h>
#include<math.h>
#include<stdlib.h>
#include<string>
#include<queue>
#include<set>
#define intt long long
using namespace std;
int dx[10]= {1,-1,0,0};
int dy[10]= {0,0,-1,1};
struct code
{
    int x,y;
};
int res,k;
string s;
int gcd(int a , int b)
{
    while(b)
    {
        a%=b;
        swap(a,b);
    }
    return a;
}
bool compare(code a , code b)
{
    return a.x<b.x or (a.x==b.x and a.y<b.y);
}
char a[1001][1001];
int used[1001][1001];
int path[1001][1001]= {0};
queue<pair<int,int> >q;
main()
{
    ios::sync_with_stdio(0);
    int n,m,k;
    cin>>n>>m>>k;
    for(int i=1; i<=n; i++)
    {
        for(int j=1; j<=m; j++)
        {
            cin>>a[i][j];
        }
    }
    pair<int,int>to;
    int xx,yy,X,Y,x,y,q1,q2;
    while(k--)
    {
        int cnt=0;
        cin>>x>>y;
        X=x;
        Y=y;
        if(used[x][y]!=0)
        {
            cout<<used[X][Y]<<endl;
            continue;
        }
        else
        {
            q.push(make_pair(x,y));
            path[x][y]=1;
            while(q.size())
            {
                to=q.front();
                q.pop();
                x=to.first;
                y=to.second;
                for(int l=0; l<4; l++)
                {
                    q1=x+dx[l];
                    q2=y+dy[l];
                    if(a[q1][q2]=='*' and a[x][y]=='.')
                    {
                        cnt++;
                    }
                }
                for(int j=0; j<4; j++)
                {
                    xx=x+dx[j];
                    yy=y+dy[j];
                    if(xx>=1 and xx<=n and yy<=m and yy>=1 and path[xx][yy]==0 and a[xx][yy]=='.')
                    {
                        path[xx][yy]=1;
                        q.push(make_pair(xx,yy));
                    }
                }
            }
            q.push(make_pair(X,Y));
            while(q.size())
            {
                to=q.front();
                q.pop();
                x=to.first;
                y=to.second;
                used[x][y]=cnt;
                path[x][y]=0;
                for(int j=0; j<4; j++)
                {
                    xx=x+dx[j];
                    yy=y+dy[j];
                    if(xx>=1 and xx<=n and yy<=m and yy>=1 and a[xx][yy]=='.' and path[xx][yy]==1)
                    {
                        q.push(make_pair(xx,yy));
                    }
                }
            }
        }
        cout<<used[X][Y]<<endl;
    }
}


#include<iostream>
#include<algorithm>
#include<stdio.h>
#include<math.h>
#include<stdlib.h>
#include<string>
#include<queue>
#include<set>
#define intt long long
using namespace std;
int dx[10]= {1,-1,0,0};
int dy[10]= {0,0,-1,1};
char a[1001][1001];
int used[1001][1001];
int path[1001][1001]= {0};
queue<pair<int,int> >q;
int xx,yy,X,Y,x,y,q1,q2;
int n,m,k,i,j,cnt,l;
pair<int,int>to;
main()
{
    ios::sync_with_stdio(0);
    cin>>n>>m>>k;
    for(i=1; i<=n; i++)
    {
        for(j=1; j<=m; j++)
        {
            cin>>a[i][j];
        }
    }
    for(int i=1;i<=1000;i++)
    {
        for(int j=1;j<=1000;j++)
            used[i][j]=-1;
    }
    while(k--)
    {
        cnt=0;
        cin>>x>>y;
        X=x;
        Y=y;
        if(used[x][y]!=-1)
        {
            cout<<used[X][Y]<<endl;
            continue;
        }
        else
        {
            q.push(make_pair(x,y));
            path[x][y]=1;
            while(q.size())
            {
                to=q.front();
                q.pop();
                x=to.first;
                y=to.second;
                for(l=0; l<4; l++)
                {
                    q1=x+dx[l];
                    q2=y+dy[l];
                    if(a[q1][q2]=='*' and a[x][y]=='.')
                    {
                        cnt++;
                    }
                }
                for(j=0; j<4; j++)
                {
                    xx=x+dx[j];
                    yy=y+dy[j];
                    if(xx>=1 and xx<=n and yy<=m and yy>=1 and path[xx][yy]==0 and a[xx][yy]=='.')
                    {
                        path[xx][yy]=1;
                        q.push(make_pair(xx,yy));
                    }
                }
            }
            path[X][Y]=0;
            q.push(make_pair(X,Y));
            while(q.size())
            {
                to=q.front();
                q.pop();
                x=to.first;
                y=to.second;
                used[x][y]=cnt;
                for(j=0; j<4; j++)
                {
                    xx=x+dx[j];
                    yy=y+dy[j];
                    if(xx>=1 and xx<=n and yy<=m and yy>=1 and a[xx][yy]=='.' and path[xx][yy]==1)
                    {
                        path[x][y]=0;
                        q.push(make_pair(xx,yy));
                    }
                }
            }
        }
        cout<<used[X][Y]<<endl;
    }
}


#include<iostream>
#include<algorithm>
#include<stdio.h>
#include<math.h>
#include<stdlib.h>
#include<string>
#include<queue>
#include<set>
#define intt long long
using namespace std;
int dx[10]= {1,-1,0,0};
int dy[10]= {0,0,-1,1};
struct code
{
    int x,y;
};
int res,k;
string s;
int gcd(int a , int b)
{
    while(b)
    {
        a%=b;
        swap(a,b);
    }
    return a;
}
bool compare(code a , code b)
{
    return a.x<b.x or (a.x==b.x and a.y<b.y);
}
char a[1001][1001];
int used[1001][1001];
int path[1001][1001]= {0};
queue<pair<int,int> >q;
main()
{
    int n,m,k;
    cin>>n>>m>>k;

    for(int i=1; i<=n; i++)
    {
        for(int j=1; j<=m; j++)
        {
            cin>>a[i][j];
        }
    }
    pair<int,int>to;
    int xx,yy,X,Y,x,y,q1,q2;
    while(k--)
    {
        int cnt=0;
        cin>>x>>y;
        X=x;
        Y=y;
        if(used[x][y]!=0)
        {
            cout<<used[X][Y]<<endl;
            continue;
        }
        else
        {
            q.push(make_pair(x,y));
            path[x][y]=1;
            while(q.size())
            {
                to=q.front();
                q.pop();
                x=to.first;
                y=to.second;
                for(int l=0; l<4; l++)
                {
                    q1=x+dx[l];
                    q2=y+dy[l];
                    if((a[q1][q2]=='*' and a[x][y]=='.') or (a[q1][q2]=='.' and a[x][y]=='*'))
                    {
                        cnt++;
                    }
                }
                for(int j=0; j<4; j++)
                {
                    xx=x+dx[j];
                    yy=y+dy[j];
                    if(xx>=1 and xx<=n and yy<=m and yy>=1 and path[xx][yy]==0 and a[xx][yy]=='.')
                    {
                        path[xx][yy]=1;
                        q.push(make_pair(xx,yy));
                    }
                }
            }
            q.push(make_pair(X,Y));
            path[x][y]=0;
            while(q.size())
            {
                to=q.front();
                q.pop();
                x=to.first;
                y=to.second;
                used[x][y]=cnt;
                for(int j=0; j<4; j++)
                {
                    xx=x+dx[j];
                    yy=y+dy[j];
                    if(xx>=1 and xx<=n and yy<=m and yy>=1 and a[xx][yy]=='.' and path[xx][yy]==1)
                    {
                        path[x][y]=0;
                        q.push(make_pair(xx,yy));
                    }
                }
            }
        }
        cout<<used[X][Y]<<endl;
    }
}


#include <iostream>
#include <vector>
#include <map>
#include <set>
#include <utility>
#include <unordered_map>
#include <unordered_set>

using namespace std;

int get_pictures_count(const vector<vector<char>>& matrix,
					   pair<int, int> current_pair)
{
	int answer = 0;
	vector<pair<int, int>> mates;
	mates.push_back(make_pair(current_pair.first + 1, current_pair.second));
	mates.push_back(make_pair(current_pair.first - 1, current_pair.second));
	mates.push_back(make_pair(current_pair.first, current_pair.second + 1));
	mates.push_back(make_pair(current_pair.first, current_pair.second - 1));

	for(auto mate : mates)
	{
		if (matrix[mate.first][mate.second] == '*')
			answer++;
	}
	return answer;
}

void dfs(const vector<vector<char>>& matrix,
	     pair<int, int> current_pair,
		 vector<vector<int>>& area_cells,
		 set<pair<int, int>>& area_cells_found)
{
	if (matrix[current_pair.first][current_pair.second] != '.' ||
		//area_cells[current_pair.first][current_pair.second] != -1)
		area_cells_found.find(current_pair) == area_cells_found.end())
		return;
	area_cells[current_pair.first][current_pair.second] = 1;
	area_cells_found.insert(current_pair);
	vector<pair<int, int>> mates;
	mates.push_back(make_pair(current_pair.first + 1, current_pair.second));
	mates.push_back(make_pair(current_pair.first - 1, current_pair.second));
	mates.push_back(make_pair(current_pair.first, current_pair.second + 1));
	mates.push_back(make_pair(current_pair.first, current_pair.second - 1));

	for (auto mate : mates)
		dfs(matrix,
			mate,
			area_cells,
			area_cells_found);
}

int main()
{
	int n, m, k;
	cin >> n >> m >> k;
	vector<vector<char>> matrix;
	for(int i = 0; i < n; i++)
	{
		vector<char> current_line;
		for(int j = 0; j < m; j++)
		{
			char buf;
			cin >> buf;
			current_line.push_back(buf);
		}
		matrix.push_back(current_line);
	}

	vector<vector<int>> cached_answer(n, vector<int>(m, -1));
	/*for(int i = 0; i < n; i++)
	{
		cached_answer.push_back(vector<int>(m, -1));
	}*/

	for(int i = 0; i < k; i++)
	{
		int x, y;
		cin >> x >> y;
		auto current_pair = make_pair(x - 1, y - 1);

		if (cached_answer[current_pair.first][current_pair.second] == -1)
		{
			vector<vector<int>> area_cells(n, vector<int>(m, -1));
			/*for(int i = 0; i < n; i++)
			{
				area_cells.push_back(vector<int>(m, -1));
			}*/
			set<pair<int, int>> area_cells_found;
			dfs(matrix,
				current_pair,
				area_cells,
				area_cells_found);
			int answer = 0;
			for(auto cell : area_cells_found)
			{
				answer += get_pictures_count(matrix, cell);
			}
			for(auto cell : area_cells_found)
			{
				cached_answer[cell.first][cell.second] = answer;
			}
		}
		cout << cached_answer[current_pair.first][current_pair.second] << endl;
	}

	//system ("pause");
	return 0;
}


#include <iostream>

using namespace std;

char matrix[1000][1000];
bool visited[1000][1000];
//keys - coordinates; value = component
int component_by_coordinates[1000][1000];
//index = component; value = count of pictures in component
int component_pictures_count[1000 * 1000];
int n, m, k;
int component = 0;
int x, y;
int i, j;

void dfs(int i,
		 int j)
{
	if(visited[i][j])
		return;
	if (matrix[i][j] == '*')
	{
		component_pictures_count[component]++;
		return;
	}
	visited[i][j] = true;
	component_by_coordinates[i][j] = component;
	dfs(i + 1, j);
	dfs(i - 1, j);
	dfs(i, j + 1);
	dfs(i, j - 1);
}

int main()
{
	cin >> n >> m >> k;
	for(i = 0; i < n; i++)
		for (j = 0; j < m; j++)
			cin >> matrix[i][j];

	for(i = 0; i < k; i++)
	{
		cin >> x >> y;
		x--; y--;

		if (! visited[x][y])
		{
			dfs(x, y);
			component++;
		}

		cout << component_pictures_count[component_by_coordinates[x][y]] << endl;
	}
	return 0;//easy win blya
}


#include <iostream>
#include <cstring>

using namespace std;

inline int pictures_count(char** matrix,
						  int i,
						  int j)
{
	int answer = 0;
	answer += matrix[i + 1][j] == '*';
	answer += matrix[i - 1][j] == '*';
	answer += matrix[i][j + 1] == '*';
	answer += matrix[i][j - 1] == '*';
	return answer;
}

void dfs(char** matrix,
		 int i,
		 int j,
		 int component,
		 bool** visited,
		 int* component_pictures_count,
		 int** component_by_coordinates)
{
	if(matrix[i][j] == '*' ||
	   visited[i][j] == true)
		return;
	visited[i][j] = true;
	component_pictures_count[component] += pictures_count(matrix, i, j);
	component_by_coordinates[i][j] = component;
	dfs(matrix, i + 1, j, component, visited, component_pictures_count, component_by_coordinates);
	dfs(matrix, i - 1, j, component, visited, component_pictures_count, component_by_coordinates);
	dfs(matrix, i, j + 1, component, visited, component_pictures_count, component_by_coordinates);
	dfs(matrix, i, j - 1, component, visited, component_pictures_count, component_by_coordinates);
}

int main()
{
	int n, m, k;
	cin >> n >> m >> k;
	char** matrix = new char* [n];
	bool** visited = new bool* [n];
	int** answer = new int* [n];
	//keys - coordinates; value = component
	int ** component_by_coordinates = new int* [n];
	//index = component; value = count of pictures in component
	int* component_pictures_count = new int[n * m];
	memset(component_pictures_count, 0, sizeof(int) * n * m);
	for(int i = 0; i < n; i++)
	{
		matrix[i] = new char[m];
		memset(matrix[i], 0, sizeof(char) * m);
		visited[i] = new bool[m];
		memset(visited[i], 0, sizeof(bool) * m);
		answer[i] = new int[m];
		memset(answer[i], 0, sizeof(int) * m);
		component_by_coordinates[i] = new int[m];
		memset(component_by_coordinates[i], 0, sizeof(int) * m);
		for (int j = 0; j < m; j++)
			cin >> matrix[i][j];
	}

	int component = 0;
	for(int i = 1; i < n - 1; i++)
		for(int j = 1; j < m - 1; j++)
			if (! visited[i][j] &&
				matrix[i][j] == '.')
			{
				dfs(matrix,
					i, j,
					component,
					visited,
					component_pictures_count,
					component_by_coordinates);
				answer[i][j] = component_pictures_count[component];
				component++;
			}
			else
				answer[i][j] = component_pictures_count[component_by_coordinates[i][j]];

	for(int i = 0; i < k; i++)
	{
		int x, y;
		cin >> x >> y;
		cout << answer[x - 1][y - 1] << endl;
	}
	return 0;//easy win blya
}


#include <iostream>

using namespace std;

inline int pictures_count(char** matrix,
						  int i,
						  int j)
{
	int answer = 0;
	answer += matrix[i + 1][j] == '*';
	answer += matrix[i - 1][j] == '*';
	answer += matrix[i][j + 1] == '*';
	answer += matrix[i][j - 1] == '*';
	return answer;
}

void dfs(char** matrix,
		 int i,
		 int j,
		 int component,
		 bool** visited,
		 int* component_pictures_count)
{
	if(matrix[i][j] == '*' ||
	   visited[i][j] == true)
		return;
	visited[i][j] = true;
	component_pictures_count[component] += pictures_count(matrix, i, j);
	dfs(matrix, i + 1, j, component, visited, component_pictures_count);
	dfs(matrix, i - 1, j, component, visited, component_pictures_count);
	dfs(matrix, i, j + 1, component, visited, component_pictures_count);
	dfs(matrix, i, j - 1, component, visited, component_pictures_count);
}

int main()
{
	int n, m, k;
	cin >> n >> m >> k;
	char** matrix = new char* [n];
	bool** visited = new bool* [n];
	int** answer = new int* [n];
	//index = component; value = count of pictures in component
	int* component_pictures_count = new int[n*m];
	for(int i = 0; i < n; i++)
	{
		matrix[i] = new char[m];
		visited[i] = new bool[m];
		answer[i] = new int[m];
		for (int j = 0; j < m; j++)
			cin >> matrix[i][j];
	}

	int component = 0;
	for(int i = 1; i < n - 1; i++)
		for(int j = 1; j < m - 1; j++)
			if (! visited[i][j] &&
				matrix[i][j] == '.')
			{
				dfs(matrix,
					i, j,
					component,
					visited,
					component_pictures_count);
				answer[i][j] = component_pictures_count[component];
				component++;
			}
			else
				answer[i][j] = component_pictures_count[component - 1];

	for(int i = 0; i < k; i++)
	{
		int x, y;
		cin >> x >> y;
		cout << answer[x - 1][y - 1] << endl;
	}
	return 0;//easy win blya
}


#include <iostream>
#include <cstring>

using namespace std;

char matrix[1000][1000];
bool visited[1000][1000];
//keys - coordinates; value = component
int component_by_coordinates[1000][1000];
//index = component; value = count of pictures in component
int component_pictures_count = int[1000 * 1000];

void dfs(int i,
		 int j,
		 int component)
{
	if(matrix[i][j] == '*' ||
	   visited[i][j] == true)
		return;
	visited[i][j] = true;
	component_pictures_count[component] += matrix[i + 1][j] == '*';
	component_pictures_count[component] += matrix[i - 1][j] == '*';
	component_pictures_count[component] += matrix[i][j + 1] == '*';
	component_pictures_count[component] += matrix[i][j - 1] == '*';
	component_by_coordinates[i][j] = component;
	dfs(i + 1, j, component);
	dfs(i - 1, j, component);
	dfs(i, j + 1, component);
	dfs(i, j - 1, component);
}

int main()
{
	int n, m, k;
	cin >> n >> m >> k;
	for(int i = 0; i < n; i++)
		for (int j = 0; j < m; j++)
			cin >> matrix[i][j];

	int component = 0;
	int x, y;
	for(int i = 0; i < k; i++)
	{
		cin >> x >> y;
		x--; y--;

		if (! visited[x][y])
		{
			dfs(x, y, component);
			component++;
		}

		cout << component_pictures_count[component_by_coordinates[x][y]] << endl;
	}
	return 0;//easy win blya
}


#include <iostream>

using namespace std;

char matrix[1000][1000];
bool visited[1000][1000];
//keys - coordinates; value = component
int component_by_coordinates[1000][1000];
//index = component; value = count of pictures in component
int component_pictures_count[1000 * 1000];
int n, m, k;
int component = 0;
int x, y;
int i, j;

void dfs(int i,
		 int j)
{
	if(matrix[i][j] == '*' ||
	   visited[i][j] == true)
		return;
	visited[i][j] = true;
	component_pictures_count[component] += matrix[i + 1][j] == '*';
	component_pictures_count[component] += matrix[i - 1][j] == '*';
	component_pictures_count[component] += matrix[i][j + 1] == '*';
	component_pictures_count[component] += matrix[i][j - 1] == '*';
	component_by_coordinates[i][j] = component;
	dfs(i + 1, j);
	dfs(i - 1, j);
	dfs(i, j + 1);
	dfs(i, j - 1);
}

int main()
{
	cin >> n >> m >> k;
	for(i = 0; i < n; i++)
		for (j = 0; j < m; j++)
			cin >> matrix[i][j];

	for(i = 0; i < k; i++)
	{
		cin >> x >> y;
		x--; y--;

		if (! visited[x][y])
		{
			dfs(x, y);
			component++;
		}

		cout << component_pictures_count[component_by_coordinates[x][y]] << endl;
	}
	return 0;//easy win blya
}


#include <iostream>
#include <vector>
#include <map>
#include <set>
#include <utility>
#include <unordered_map>
#include <unordered_set>

using namespace std;

int get_pictures_count(const vector<vector<char>>& matrix,
					   pair<int, int> current_pair)
{
	int answer = 0;
	vector<pair<int, int>> mates;
	mates.push_back(make_pair(current_pair.first + 1, current_pair.second));
	mates.push_back(make_pair(current_pair.first - 1, current_pair.second));
	mates.push_back(make_pair(current_pair.first, current_pair.second + 1));
	mates.push_back(make_pair(current_pair.first, current_pair.second - 1));

	for(auto mate : mates)
	{
		if (matrix[mate.first][mate.second] == '*')
			answer++;
	}
	return answer;
}

void dfs(const vector<vector<char>>& matrix,
	     pair<int, int> current_pair,
		 vector<vector<int>>& area_cells,
		 vector<pair<int, int>>& area_cells_found)
{
	if (matrix[current_pair.first][current_pair.second] != '.' ||
		area_cells[current_pair.first][current_pair.second] != -1)
		return;
	area_cells[current_pair.first][current_pair.second] = 1;
	area_cells_found.push_back(current_pair);
	vector<pair<int, int>> mates;
	mates.push_back(make_pair(current_pair.first + 1, current_pair.second));
	mates.push_back(make_pair(current_pair.first - 1, current_pair.second));
	mates.push_back(make_pair(current_pair.first, current_pair.second + 1));
	mates.push_back(make_pair(current_pair.first, current_pair.second - 1));

	for (auto mate : mates)
		dfs(matrix,
			mate,
			area_cells,
			area_cells_found);
}

int main()
{
	int n, m, k;
	cin >> n >> m >> k;
	vector<vector<char>> matrix;
	for(int i = 0; i < n; i++)
	{
		vector<char> current_line;
		for(int j = 0; j < m; j++)
		{
			char buf;
			cin >> buf;
			current_line.push_back(buf);
		}
		matrix.push_back(current_line);
	}

	vector<vector<int>> cached_answer;
	for(int i = 0; i < n; i++)
	{
		cached_answer.push_back(vector<int>(m, -1));
	}

	for(int i = 0; i < k; i++)
	{
		int x, y;
		cin >> x >> y;
		auto current_pair = make_pair(x - 1, y - 1);

		if (cached_answer[current_pair.first][current_pair.second] == -1)
		{
			vector<vector<int>> area_cells(n, vector<int>(m, -1));
			/*for(int i = 0; i < n; i++)
			{
				area_cells.push_back(vector<int>(m, -1));
			}*/
			vector<pair<int, int>> area_cells_found;
			dfs(matrix,
				current_pair,
				area_cells,
				area_cells_found);
			int answer = 0;
			for(auto cell : area_cells_found)
			{
				answer += get_pictures_count(matrix, cell);
			}
			for(auto cell : area_cells_found)
			{
				cached_answer[cell.first][cell.second] = answer;
			}
		}
		cout << cached_answer[current_pair.first][current_pair.second] << endl;
	}

	//system ("pause");
	return 0;
}


#include <iostream>
#include <vector>
#include <map>
#include <set>
#include <utility>
#include <unordered_map>

using namespace std;

int get_pictures_count(const vector<vector<char>>& matrix,
					   pair<int, int> current_pair)
{
	int answer = 0;
	vector<pair<int, int>> mates;
	mates.push_back(make_pair(current_pair.first + 1, current_pair.second));
	mates.push_back(make_pair(current_pair.first - 1, current_pair.second));
	mates.push_back(make_pair(current_pair.first, current_pair.second + 1));
	mates.push_back(make_pair(current_pair.first, current_pair.second - 1));

	for(auto mate : mates)
	{
		if (matrix[mate.first][mate.second] == '*')
			answer++;
	}
	return answer;
}

void dfs(const vector<vector<char>>& matrix,
	     pair<int, int> current_pair,
		 set<pair<int, int>>& area_cells)
{
	if (matrix[current_pair.first][current_pair.second] != '.' ||
		area_cells.find(current_pair) != area_cells.end())
		return;
	area_cells.insert(current_pair);
	vector<pair<int, int>> mates;
	mates.push_back(make_pair(current_pair.first + 1, current_pair.second));
	mates.push_back(make_pair(current_pair.first - 1, current_pair.second));
	mates.push_back(make_pair(current_pair.first, current_pair.second + 1));
	mates.push_back(make_pair(current_pair.first, current_pair.second - 1));

	for (auto mate : mates)
		dfs(matrix,
			mate,
			area_cells);
}

int main()
{
	int n, m, k;
	cin >> n >> m >> k;
	vector<vector<char>> matrix;
	for(int i = 0; i < n; i++)
	{
		vector<char> current_line;
		for(int j = 0; j < m; j++)
		{
			char buf;
			cin >> buf;
			current_line.push_back(buf);
		}
		matrix.push_back(current_line);
	}

	vector<vector<int>> cached_answer(n, vector<int>(m, -1));
	for(int i = 0; i < k; i++)
	{
		int x, y;
		cin >> x >> y;
		auto current_pair = make_pair(x - 1, y - 1);

		if (cached_answer[current_pair.first][current_pair.second] == -1)
		{
			set<pair<int, int>> area_cells;
			dfs(matrix, current_pair, area_cells);
			int answer = 0;
			for(auto cell : area_cells)
			{
				answer += get_pictures_count(matrix, cell);
			}
			for(auto cell : area_cells)
			{
				cached_answer[cell.first][cell.second] = answer;
			}
		}
		cout << cached_answer[current_pair.first][current_pair.second] << endl;
	}

	//system ("pause");
	return 0;
}


#include <iostream>
#include <cstring>

using namespace std;

inline int pictures_count(char** matrix,
						  int i,
						  int j)
{
	int answer = 0;
	answer += matrix[i + 1][j] == '*';
	answer += matrix[i - 1][j] == '*';
	answer += matrix[i][j + 1] == '*';
	answer += matrix[i][j - 1] == '*';
	return answer;
}

void dfs(char** matrix,
		 int i,
		 int j,
		 int component,
		 bool** visited,
		 int* component_pictures_count)
{
	if(matrix[i][j] == '*' ||
	   visited[i][j] == true)
		return;
	visited[i][j] = true;
	component_pictures_count[component] += pictures_count(matrix, i, j);
	dfs(matrix, i + 1, j, component, visited, component_pictures_count);
	dfs(matrix, i - 1, j, component, visited, component_pictures_count);
	dfs(matrix, i, j + 1, component, visited, component_pictures_count);
	dfs(matrix, i, j - 1, component, visited, component_pictures_count);
}

int main()
{
	int n, m, k;
	cin >> n >> m >> k;
	char** matrix = new char* [n];
	bool** visited = new bool* [n];
	int** answer = new int* [n];
	//index = component; value = count of pictures in component
	int* component_pictures_count = new int[n * m];
	memset(component_pictures_count, 0, sizeof(int) * n * m);
	for(int i = 0; i < n; i++)
	{
		matrix[i] = new char[m];
		memset(matrix[i], 0, sizeof(char) * m);
		visited[i] = new bool[m];
		memset(visited[i], 0, sizeof(bool) * m);
		answer[i] = new int[m];
		memset(answer[i], 0, sizeof(int) * m);
		for (int j = 0; j < m; j++)
			cin >> matrix[i][j];
	}

	int component = 0;
	for(int i = 1; i < n - 1; i++)
		for(int j = 1; j < m - 1; j++)
			if (! visited[i][j] &&
				matrix[i][j] == '.')
			{
				dfs(matrix,
					i, j,
					component,
					visited,
					component_pictures_count);
				answer[i][j] = component_pictures_count[component];
				component++;
			}
			else
				answer[i][j] = component_pictures_count[component - 1];

	for(int i = 0; i < k; i++)
	{
		int x, y;
		cin >> x >> y;
		cout << answer[x - 1][y - 1] << endl;
	}
	return 0;//easy win blya
}


#include <iostream>
#include <vector>
#include <map>
#include <set>
#include <utility>
#include <unordered_map>

using namespace std;

int get_pictures_count(const vector<vector<char>>& matrix,
					   pair<int, int> current_pair)
{
	int answer = 0;
	vector<pair<int, int>> mates;
	mates.push_back(make_pair(current_pair.first + 1, current_pair.second));
	mates.push_back(make_pair(current_pair.first - 1, current_pair.second));
	mates.push_back(make_pair(current_pair.first, current_pair.second + 1));
	mates.push_back(make_pair(current_pair.first, current_pair.second - 1));

	for(auto mate : mates)
	{
		if (matrix[mate.first][mate.second] == '*')
			answer++;
	}
	return answer;
}

void dfs(const vector<vector<char>>& matrix,
	     pair<int, int> current_pair,
		 set<pair<int, int>>& area_cells)
{
	if (matrix[current_pair.first][current_pair.second] != '.' ||
		area_cells.find(current_pair) != area_cells.end())
		return;
	area_cells.insert(current_pair);
	vector<pair<int, int>> mates;
	mates.push_back(make_pair(current_pair.first + 1, current_pair.second));
	mates.push_back(make_pair(current_pair.first - 1, current_pair.second));
	mates.push_back(make_pair(current_pair.first, current_pair.second + 1));
	mates.push_back(make_pair(current_pair.first, current_pair.second - 1));

	for (auto mate : mates)
		dfs(matrix,
			mate,
			area_cells);
}

int main()
{
	int n, m, k;
	cin >> n >> m >> k;
	vector<vector<char>> matrix;
	for(int i = 0; i < n; i++)
	{
		vector<char> current_line;
		for(int j = 0; j < m; j++)
		{
			char buf;
			cin >> buf;
			current_line.push_back(buf);
		}
		matrix.push_back(current_line);
	}

	vector<vector<int>> cached_answer(n, vector<int>(m, -1));
	/*for(int i = 0; i < n; i++)
	{
		cached_answer.push_back(vector<int>(m, -1));
	}*/

	for(int i = 0; i < k; i++)
	{
		int x, y;
		cin >> x >> y;
		auto current_pair = make_pair(x - 1, y - 1);

		if (cached_answer[current_pair.first][current_pair.second] == -1)
		{
			set<pair<int, int>> area_cells;
			dfs(matrix, current_pair, area_cells);
			int answer = 0;
			for(auto cell : area_cells)
			{
				answer += get_pictures_count(matrix, cell);
			}
			for(auto cell : area_cells)
			{
				cached_answer[cell.first][cell.second] = answer;
			}
		}
		cout << cached_answer[current_pair.first][current_pair.second] << endl;
	}

	//system ("pause");
	return 0;
}


#include <iostream>
#include <vector>
#include <map>
#include <set>
#include <utility>
#include <unordered_map>

using namespace std;

int get_pictures_count(const vector<vector<char>>& matrix,
					   pair<int, int> current_pair)
{
	int answer = 0;
	vector<pair<int, int>> mates;
	mates.push_back(make_pair(current_pair.first + 1, current_pair.second));
	mates.push_back(make_pair(current_pair.first - 1, current_pair.second));
	mates.push_back(make_pair(current_pair.first, current_pair.second + 1));
	mates.push_back(make_pair(current_pair.first, current_pair.second - 1));

	for(auto mate : mates)
	{
		if (matrix[mate.first][mate.second] == '*')
			answer++;
	}
	return answer;
}

void dfs(const vector<vector<char>>& matrix,
	     pair<int, int> current_pair,
		 set<pair<int, int>>& area_cells)
{
	if (matrix[current_pair.first][current_pair.second] != '.' ||
		area_cells.find(current_pair) != area_cells.end())
		return;
	area_cells.insert(current_pair);
	vector<pair<int, int>> mates;
	mates.push_back(make_pair(current_pair.first + 1, current_pair.second));
	mates.push_back(make_pair(current_pair.first - 1, current_pair.second));
	mates.push_back(make_pair(current_pair.first, current_pair.second + 1));
	mates.push_back(make_pair(current_pair.first, current_pair.second - 1));

	for (auto mate : mates)
		dfs(matrix,
			mate,
			area_cells);
}

int main()
{
	int n, m, k;
	cin >> n >> m >> k;
	vector<vector<char>> matrix;
	for(int i = 0; i < n; i++)
	{
		vector<char> current_line;
		for(int j = 0; j < m; j++)
		{
			char buf;
			cin >> buf;
			current_line.push_back(buf);
		}
		matrix.push_back(current_line);
	}

	vector<vector<int>> cached_answer;
	for(int i = 0; i < n; i++)
	{
		cached_answer.push_back(vector<int>(m, -1));
	}

	for(int i = 0; i < k; i++)
	{
		int x, y;
		cin >> x >> y;
		auto current_pair = make_pair(x - 1, y - 1);

		if (cached_answer[current_pair.first][current_pair.second] == -1)
		{
			set<pair<int, int>> area_cells;
			dfs(matrix, current_pair, area_cells);
			int answer = 0;
			for(auto cell : area_cells)
			{
				answer += get_pictures_count(matrix, cell);
			}
			for(auto cell : area_cells)
			{
				cached_answer[cell.first][cell.second] = answer;
			}
		}
		cout << cached_answer[current_pair.first][current_pair.second] << endl;
	}

	//system ("pause");
	return 0;
}


#include <iostream>
#include <cstring>

using namespace std;

char matrix[1000][1000];
bool visited[1000][1000];
//keys - coordinates; value = component
int component_by_coordinates[1000][1000];
//index = component; value = count of pictures in component
int* component_pictures_count = new int[1000 * 1000];

void dfs(int i,
		 int j,
		 int component)
{
	if(matrix[i][j] == '*' ||
	   visited[i][j] == true)
		return;
	visited[i][j] = true;
	component_pictures_count[component] += matrix[i + 1][j] == '*';
	component_pictures_count[component] += matrix[i - 1][j] == '*';
	component_pictures_count[component] += matrix[i][j + 1] == '*';
	component_pictures_count[component] += matrix[i][j - 1] == '*';
	component_by_coordinates[i][j] = component;
	dfs(i + 1, j, component);
	dfs(i - 1, j, component);
	dfs(i, j + 1, component);
	dfs(i, j - 1, component);
}

int main()
{
	int n, m, k;
	cin >> n >> m >> k;
	for(int i = 0; i < n; i++)
		for (int j = 0; j < m; j++)
			cin >> matrix[i][j];

	int component = 0;
	int x, y;
	for(int i = 0; i < k; i++)
	{
		cin >> x >> y;
		x--; y--;

		if (! visited[x][y])
		{
			dfs(x, y, component);
			component++;
		}

		cout << component_pictures_count[component_by_coordinates[x][y]] << endl;
	}
	return 0;//easy win blya
}


#include <iostream>
#include <vector>
#include <map>
#include <set>
#include <utility>
#include <unordered_map>
#include <unordered_set>

using namespace std;

int get_pictures_count(const vector<vector<char>>& matrix,
					   pair<int, int> current_pair)
{
	int answer = 0;
	vector<pair<int, int>> mates;
	mates.push_back(make_pair(current_pair.first + 1, current_pair.second));
	mates.push_back(make_pair(current_pair.first - 1, current_pair.second));
	mates.push_back(make_pair(current_pair.first, current_pair.second + 1));
	mates.push_back(make_pair(current_pair.first, current_pair.second - 1));

	for(auto mate : mates)
	{
		if (matrix[mate.first][mate.second] == '*')
			answer++;
	}
	return answer;
}

void dfs(const vector<vector<char>>& matrix,
	     pair<int, int> current_pair,
		 vector<vector<int>>& area_cells,
		 set<pair<int, int>>& area_cells_found)
{
	if (matrix[current_pair.first][current_pair.second] != '.' ||
		//area_cells[current_pair.first][current_pair.second] != -1)
		area_cells_found.find(current_pair) != area_cells_found.end())
		return;
	area_cells[current_pair.first][current_pair.second] = 1;
	area_cells_found.insert(current_pair);
	vector<pair<int, int>> mates;
	mates.push_back(make_pair(current_pair.first + 1, current_pair.second));
	mates.push_back(make_pair(current_pair.first - 1, current_pair.second));
	mates.push_back(make_pair(current_pair.first, current_pair.second + 1));
	mates.push_back(make_pair(current_pair.first, current_pair.second - 1));

	for (auto mate : mates)
		dfs(matrix,
			mate,
			area_cells,
			area_cells_found);
}

int main()
{
	int n, m, k;
	cin >> n >> m >> k;
	vector<vector<char>> matrix;
	for(int i = 0; i < n; i++)
	{
		vector<char> current_line;
		for(int j = 0; j < m; j++)
		{
			char buf;
			cin >> buf;
			current_line.push_back(buf);
		}
		matrix.push_back(current_line);
	}

	vector<vector<int>> cached_answer(n, vector<int>(m, -1));
	/*for(int i = 0; i < n; i++)
	{
		cached_answer.push_back(vector<int>(m, -1));
	}*/

	for(int i = 0; i < k; i++)
	{
		int x, y;
		cin >> x >> y;
		auto current_pair = make_pair(x - 1, y - 1);

		if (cached_answer[current_pair.first][current_pair.second] == -1)
		{
			vector<vector<int>> area_cells(n, vector<int>(m, -1));
			/*for(int i = 0; i < n; i++)
			{
				area_cells.push_back(vector<int>(m, -1));
			}*/
			set<pair<int, int>> area_cells_found;
			dfs(matrix,
				current_pair,
				area_cells,
				area_cells_found);
			int answer = 0;
			for(auto cell : area_cells_found)
			{
				answer += get_pictures_count(matrix, cell);
			}
			for(auto cell : area_cells_found)
			{
				cached_answer[cell.first][cell.second] = answer;
			}
		}
		cout << cached_answer[current_pair.first][current_pair.second] << endl;
	}

	//system ("pause");
	return 0;
}


#include <iostream>
#include <cstring>

using namespace std;

char matrix[1000][1000];
bool visited[1000][1000];
int answer[1000][1000];
//keys - coordinates; value = component
int component_by_coordinates[1000][1000];
//index = component; value = count of pictures in component
int* component_pictures_count = new int[1000 * 1000];

inline int pictures_count(int i,
						  int j)
{
	int answer = 0;
	answer += matrix[i + 1][j] == '*';
	answer += matrix[i - 1][j] == '*';
	answer += matrix[i][j + 1] == '*';
	answer += matrix[i][j - 1] == '*';
	return answer;
}

void dfs(int i,
		 int j,
		 int component)
{
	if(matrix[i][j] == '*' ||
	   visited[i][j] == true)
		return;
	visited[i][j] = true;
	component_pictures_count[component] += pictures_count(i, j);
	component_by_coordinates[i][j] = component;
	dfs(i + 1, j, component);
	dfs(i - 1, j, component);
	dfs(i, j + 1, component);
	dfs(i, j - 1, component);
}

int main()
{
	int n, m, k;
	cin >> n >> m >> k;
	for(int i = 0; i < n; i++)
		for (int j = 0; j < m; j++)
			cin >> matrix[i][j];

	int component = 0;
	for(int i = 0; i < k; i++)
	{
		int x, y;
		cin >> x >> y;
		x--; y--;

		if (! visited[x][y] &&
				matrix[x][y] == '.')
		{
			dfs(x, y, component);
			answer[x][y] = component_pictures_count[component];
			component++;
		}
		else
			answer[x][y] = component_pictures_count[component_by_coordinates[x][y]];

		cout << answer[x][y] << endl;
	}
	return 0;//easy win blya
}


#include <iostream>
#include <vector>
#include <map>
#include <set>
#include <utility>
#include <unordered_map>
#include <unordered_set>

using namespace std;

int get_pictures_count(const vector<vector<char>>& matrix,
					   pair<int, int> current_pair)
{
	int answer = 0;
	vector<pair<int, int>> mates;
	mates.push_back(make_pair(current_pair.first + 1, current_pair.second));
	mates.push_back(make_pair(current_pair.first - 1, current_pair.second));
	mates.push_back(make_pair(current_pair.first, current_pair.second + 1));
	mates.push_back(make_pair(current_pair.first, current_pair.second - 1));

	for(auto mate : mates)
	{
		if (matrix[mate.first][mate.second] == '*')
			answer++;
	}
	return answer;
}

void dfs(const vector<vector<char>>& matrix,
	     pair<int, int> current_pair,
		 vector<vector<int>>& area_cells)
{
	if (matrix[current_pair.first][current_pair.second] != '.' ||
		area_cells[current_pair.first][current_pair.second] != -1)
		return;
	area_cells[current_pair.first][current_pair.second] = 1;
	vector<pair<int, int>> mates;
	mates.push_back(make_pair(current_pair.first + 1, current_pair.second));
	mates.push_back(make_pair(current_pair.first - 1, current_pair.second));
	mates.push_back(make_pair(current_pair.first, current_pair.second + 1));
	mates.push_back(make_pair(current_pair.first, current_pair.second - 1));

	for (auto mate : mates)
		dfs(matrix,
			mate,
			area_cells);
}

int main()
{
	int n, m, k;
	cin >> n >> m >> k;
	vector<vector<char>> matrix;
	for(int i = 0; i < n; i++)
	{
		vector<char> current_line;
		for(int j = 0; j < m; j++)
		{
			char buf;
			cin >> buf;
			current_line.push_back(buf);
		}
		matrix.push_back(current_line);
	}

	vector<vector<int>> cached_answer;
	for(int i = 0; i < n; i++)
	{
		cached_answer.push_back(vector<int>(m, -1));
	}

	for(int i = 0; i < k; i++)
	{
		int x, y;
		cin >> x >> y;
		auto current_pair = make_pair(x - 1, y - 1);

		if (cached_answer[current_pair.first][current_pair.second] == -1)
		{
			vector<vector<int>> area_cells(n, vector<int>(m, -1));
			/*for(int i = 0; i < n; i++)
			{
				area_cells.push_back(vector<int>(m, -1));
			}*/
			dfs(matrix, current_pair, area_cells);
			int answer = 0;
			for(int x = 0; x < n; x++)
				for(int y = 0; y < m; y++)
			{
				if (area_cells[x][y] == -1)
					continue;
				auto cell = make_pair(x, y);
				answer += get_pictures_count(matrix, cell);
			}
			for(int x = 0; x < n; x++)
				for(int y = 0; y < m; y++)
			{
				if (area_cells[x][y] == -1)
					continue;
				cached_answer[x][y] = answer;
			}
		}
		cout << cached_answer[current_pair.first][current_pair.second] << endl;
	}

	//system ("pause");
	return 0;
}


#include <iostream>
#include <cstring>

using namespace std;

char matrix[1000][1000];
bool visited[1000][1000];
//keys - coordinates; value = component
int component_by_coordinates[1000][1000];
//index = component; value = count of pictures in component
int component_pictures_count[1000 * 1000];
int n, m, k;
int component = 0;
int x, y;

void dfs(int i,
		 int j,
		 int component)
{
	if(matrix[i][j] == '*' ||
	   visited[i][j] == true)
		return;
	visited[i][j] = true;
	component_pictures_count[component] += matrix[i + 1][j] == '*';
	component_pictures_count[component] += matrix[i - 1][j] == '*';
	component_pictures_count[component] += matrix[i][j + 1] == '*';
	component_pictures_count[component] += matrix[i][j - 1] == '*';
	component_by_coordinates[i][j] = component;
	dfs(i + 1, j, component);
	dfs(i - 1, j, component);
	dfs(i, j + 1, component);
	dfs(i, j - 1, component);
}

int main()
{
	cin >> n >> m >> k;
	for(int i = 0; i < n; i++)
		for (int j = 0; j < m; j++)
			cin >> matrix[i][j];

	for(int i = 0; i < k; i++)
	{
		cin >> x >> y;
		x--; y--;

		if (! visited[x][y])
		{
			dfs(x, y, component);
			component++;
		}

		cout << component_pictures_count[component_by_coordinates[x][y]] << endl;
	}
	return 0;//easy win blya
}


#include <iostream>
#include <cstring>

using namespace std;

char matrix[1000][1000];
bool visited[1000][1000];
//keys - coordinates; value = component
int component_by_coordinates[1000][1000];
//index = component; value = count of pictures in component
int component_pictures_count[1000 * 1000];

void dfs(int i,
		 int j,
		 int component)
{
	if(matrix[i][j] == '*' ||
	   visited[i][j] == true)
		return;
	visited[i][j] = true;
	component_pictures_count[component] += matrix[i + 1][j] == '*';
	component_pictures_count[component] += matrix[i - 1][j] == '*';
	component_pictures_count[component] += matrix[i][j + 1] == '*';
	component_pictures_count[component] += matrix[i][j - 1] == '*';
	component_by_coordinates[i][j] = component;
	dfs(i + 1, j, component);
	dfs(i - 1, j, component);
	dfs(i, j + 1, component);
	dfs(i, j - 1, component);
}

int main()
{
	int n, m, k;
	cin >> n >> m >> k;
	for(int i = 0; i < n; i++)
		for (int j = 0; j < m; j++)
			cin >> matrix[i][j];

	int component = 0;
	int x, y;
	for(int i = 0; i < k; i++)
	{
		cin >> x >> y;
		x--; y--;

		if (! visited[x][y])
		{
			dfs(x, y, component);
			component++;
		}

		cout << component_pictures_count[component_by_coordinates[x][y]] << endl;
	}
	return 0;//easy win blya
}


#include <iostream>
#include <cstring>

using namespace std;

char matrix[1000][1000];
bool visited[1000][1000];
//keys - coordinates; value = component
int component_by_coordinates[1000][1000];
//index = component; value = count of pictures in component
int component_pictures_count[1000 * 1000];
int n, m, k;
int component = 0;
int x, y;
int i, j;

void dfs(const int& i,
		 const int& j,
		 const int& component)
{
	if(matrix[i][j] == '*' ||
	   visited[i][j] == true)
		return;
	visited[i][j] = true;
	component_pictures_count[component] += matrix[i + 1][j] == '*';
	component_pictures_count[component] += matrix[i - 1][j] == '*';
	component_pictures_count[component] += matrix[i][j + 1] == '*';
	component_pictures_count[component] += matrix[i][j - 1] == '*';
	component_by_coordinates[i][j] = component;
	dfs(i + 1, j, component);
	dfs(i - 1, j, component);
	dfs(i, j + 1, component);
	dfs(i, j - 1, component);
}

int main()
{
	cin >> n >> m >> k;
	for(i = 0; i < n; i++)
		for (j = 0; j < m; j++)
			cin >> matrix[i][j];

	for(i = 0; i < k; i++)
	{
		cin >> x >> y;
		x--; y--;

		if (! visited[x][y])
		{
			dfs(x, y, component);
			component++;
		}

		cout << component_pictures_count[component_by_coordinates[x][y]] << endl;
	}
	return 0;//easy win blya
}


#include <iostream>
#include <cstring>

using namespace std;

inline int pictures_count(char** matrix,
						  int i,
						  int j)
{
	int answer = 0;
	answer += matrix[i + 1][j] == '*';
	answer += matrix[i - 1][j] == '*';
	answer += matrix[i][j + 1] == '*';
	answer += matrix[i][j - 1] == '*';
	return answer;
}

void dfs(char** matrix,
		 int i,
		 int j,
		 int component,
		 bool** visited,
		 int* component_pictures_count)
{
	if(matrix[i][j] == '*' ||
	   visited[i][j] == true)
		return;
	visited[i][j] = true;
	component_pictures_count[component] += pictures_count(matrix, i, j);
	dfs(matrix, i + 1, j, component, visited, component_pictures_count);
	dfs(matrix, i - 1, j, component, visited, component_pictures_count);
	dfs(matrix, i, j + 1, component, visited, component_pictures_count);
	dfs(matrix, i, j - 1, component, visited, component_pictures_count);
}

int main()
{
	int n, m, k;
	cin >> n >> m >> k;
	char** matrix = new char* [n];
	bool** visited = new bool* [n];
	int** answer = new int* [n];
	//index = component; value = count of pictures in component
	int* component_pictures_count = new int[n*m];
	for(int i = 0; i < n; i++)
	{
		matrix[i] = new char[m];
		memset(matrix[i], 0, sizeof(matrix[i]));
		visited[i] = new bool[m];
		memset(visited[i], 0, sizeof(visited[i]));
		answer[i] = new int[m];
		memset(answer[i], 0, sizeof(answer[i]));
		for (int j = 0; j < m; j++)
			cin >> matrix[i][j];
	}

	int component = 0;
	for(int i = 1; i < n - 1; i++)
		for(int j = 1; j < m - 1; j++)
			if (! visited[i][j] &&
				matrix[i][j] == '.')
			{
				dfs(matrix,
					i, j,
					component,
					visited,
					component_pictures_count);
				answer[i][j] = component_pictures_count[component];
				component++;
			}
			else
				answer[i][j] = component_pictures_count[component - 1];

	for(int i = 0; i < k; i++)
	{
		int x, y;
		cin >> x >> y;
		cout << answer[x - 1][y - 1] << endl;
	}
	return 0;//easy win blya
}


#include <iostream>
#include <vector>
#include <map>
#include <set>
#include <utility>
#include <unordered_map>
#include <unordered_set>

using namespace std;

int get_pictures_count(const vector<vector<char>>& matrix,
					   pair<int, int> current_pair)
{
	int answer = 0;
	vector<pair<int, int>> mates;
	mates.push_back(make_pair(current_pair.first + 1, current_pair.second));
	mates.push_back(make_pair(current_pair.first - 1, current_pair.second));
	mates.push_back(make_pair(current_pair.first, current_pair.second + 1));
	mates.push_back(make_pair(current_pair.first, current_pair.second - 1));

	for(auto mate : mates)
	{
		if (matrix[mate.first][mate.second] == '*')
			answer++;
	}
	return answer;
}

void dfs(const vector<vector<char>>& matrix,
	     pair<int, int> current_pair,
		 vector<vector<int>>& area_cells)
{
	if (matrix[current_pair.first][current_pair.second] != '.' ||
		area_cells[current_pair.first][current_pair.second] != -1)
		return;
	area_cells[current_pair.first][current_pair.second] = 1;
	vector<pair<int, int>> mates;
	mates.push_back(make_pair(current_pair.first + 1, current_pair.second));
	mates.push_back(make_pair(current_pair.first - 1, current_pair.second));
	mates.push_back(make_pair(current_pair.first, current_pair.second + 1));
	mates.push_back(make_pair(current_pair.first, current_pair.second - 1));

	for (auto mate : mates)
		dfs(matrix,
			mate,
			area_cells);
}

int main()
{
	int n, m, k;
	cin >> n >> m >> k;
	vector<vector<char>> matrix;
	for(int i = 0; i < n; i++)
	{
		vector<char> current_line;
		for(int j = 0; j < m; j++)
		{
			char buf;
			cin >> buf;
			current_line.push_back(buf);
		}
		matrix.push_back(current_line);
	}

	vector<vector<int>> cached_answer;
	for(int i = 0; i < n; i++)
	{
		cached_answer.push_back(vector<int>(m, -1));
	}

	for(int i = 0; i < k; i++)
	{
		int x, y;
		cin >> x >> y;
		auto current_pair = make_pair(x - 1, y - 1);

		if (cached_answer[current_pair.first][current_pair.second] == -1)
		{
			vector<vector<int>> area_cells;
			for(int i = 0; i < n; i++)
			{
				area_cells.push_back(vector<int>(m, -1));
			}
			dfs(matrix, current_pair, area_cells);
			int answer = 0;
			for(int x = 0; x < n; x++)
				for(int y = 0; y < m; y++)
			{
				if (area_cells[x][y] == -1)
					continue;
				auto cell = make_pair(x, y);
				answer += get_pictures_count(matrix, cell);
			}
			for(int x = 0; x < n; x++)
				for(int y = 0; y < m; y++)
			{
				if (area_cells[x][y] == -1)
					continue;
				cached_answer[x][y] = answer;
			}
		}
		cout << cached_answer[current_pair.first][current_pair.second] << endl;
	}

	//system ("pause");
	return 0;
}


#include <iostream>
#include <cstring>

using namespace std;

char matrix[1000][1000];
bool visited[1000][1000];
//keys - coordinates; value = component
int component_by_coordinates[1000][1000];
//index = component; value = count of pictures in component
int* component_pictures_count = new int[1000 * 1000];

inline int pictures_count(int i,
						  int j)
{
	int answer = 0;
	answer += matrix[i + 1][j] == '*';
	answer += matrix[i - 1][j] == '*';
	answer += matrix[i][j + 1] == '*';
	answer += matrix[i][j - 1] == '*';
	return answer;
}

void dfs(int i,
		 int j,
		 int component)
{
	if(matrix[i][j] == '*' ||
	   visited[i][j] == true)
		return;
	visited[i][j] = true;
	component_pictures_count[component] += pictures_count(i, j);
	component_by_coordinates[i][j] = component;
	dfs(i + 1, j, component);
	dfs(i - 1, j, component);
	dfs(i, j + 1, component);
	dfs(i, j - 1, component);
}

int main()
{
	int n, m, k;
	cin >> n >> m >> k;
	for(int i = 0; i < n; i++)
		for (int j = 0; j < m; j++)
			cin >> matrix[i][j];

	int component = 0;
	for(int i = 0; i < k; i++)
	{
		int x, y;
		cin >> x >> y;
		x--; y--;

		if (! visited[x][y])
		{
			dfs(x, y, component);
			component++;
		}

		cout << component_pictures_count[component_by_coordinates[x][y]] << endl;
	}
	return 0;//easy win blya
}


//nani?
#include<bits/stdc++.h>
#define pi acos(-1);
#define fs first
#define sc second
#define pb push_back
#define mp make_pair
#define f(i,a,b) for( int i = a; i < b ; i++ )
#define fore(i,a,b) for(int i=a,ThxDem=b;i<ThxDem;++i)
#define sor(a) sort( a.begin(), a.end() )
#define rsor(a) sort( a.rbegin(), a.rend() )
#define fastio ios_base::sync_with_stdio(0);cin.tie(0)
typedef long long ll;
typedef long double ld;
using namespace std;
const ll inf=1e10+6;
const ll MOD=1e9+7;
const ll mod=998244353;
// ac cmtr;
const int MX = 1e6+5;
const int LG = 25;

#define trace(...) fff(#__VA_ARGS__, __VA_ARGS__)
template<typename t> void fff(const char* x, t&& val1) { cout<<x<< " : "<<val1<<"\n";}
template<typename t1, typename... t2> void fff(const char* x, t1&& val1, t2&&... val2){
    const char* xd=strchr(x+1, ',');
    cout.write(x, xd-x)<<" : "<<val1<<" | ";
    fff(xd+1, val2...);
}

//---------------------------

pair <ld,ld> x[MX],y[MX];

ld point(pair <ld,ld> u,pair <ld,ld> v){
	return u.fs*v.fs + u.sc*v.sc;
}
ld dis(ld a,ld b,ld x, ld y){
    return sqrt((a-x)*(a-x) + (b-y)*(b-y));
}
ld cal_area(ld a,ld b,ld c,ld d,ld e,ld f){
    ld ans = abs( (b*c +d*e + f*a) - (a*d+c*f+e*b) )/2;
    return ans;
}
bool cmp(pair <ld,ld> a,pair <ld,ld> b){
	if(abs(a.fs - b.fs)< 1e-9 && abs(a.sc - b.sc)< 1e-9 ) return true;
  	else return false;
}
void ini(ll cond,ll a1,ll a2,ll a3){
    if(cond == 1){
        swap(x[a1],y[a1]);
    }
    else if(cond == 2){
        swap(x[a1],y[a1]);
        swap(x[a2],y[a2]);
    }
    else if(cond == 4){
        swap(x[a2],y[a2]);
    }
}

int main(){
    fastio;
    ll tc; cin >> tc;
  	//cout<<point({1,-1},{0,-1})<<endl;
    while(tc--){
        cin >> x[1].fs >> x[1].sc >> y[1].fs >> y[1].sc;
        cin >> x[2].fs >> x[2].sc >> y[2].fs >> y[2].sc;
        cin >> x[3].fs >> x[3].sc >> y[3].fs >> y[3].sc;

      	//x[1].fs/= 1000.00;

        ll flag = 0;
        for(int i = 1 ; i<= 3; i++){
            for(int j = 1; j <= 3; j++){
                if(i == j) continue;
                ll a1 = i,a2 = j,a3;
                if((a1 == 1 && a2 ==2) || (a1 == 2 && a2 ==1)) a3 = 3;
                if((a1 == 3 && a2 ==2) || (a1 == 2 && a2 ==3)) a3 = 1;
                if((a1 == 1 && a2 ==3) || (a1 == 3 && a2 ==1)) a3 = 2;

                ll cond = 0;
                if(cmp(x[a1],x[a2])){
                    swap(x[a1],y[a1]);
                    cond = 1;
                }
                else if(cmp(x[a1],y[a2]) ){
                    swap(x[a1],y[a1]);
                    swap(x[a2],y[a2]);
                    cond = 2;
                }
                else if(cmp(y[a1],x[a2])){
                    cond = 3;
                }
                else if(cmp(y[a1],y[a2])){
                    swap(x[a2],y[a2]);
                    cond = 4;
                }
                else continue;
                //if((y[a1] != x[a2]) && )  continue;
            	pair <ld,ld > u= {x[a1].fs-y[a1].fs,x[a1].sc-y[a1].sc};
              	pair <ld,ld > v= {y[a2].fs-x[a2].fs,y[a2].sc-x[a2].sc};
              	ld aux = point(u,v);

                ld dis1 = dis(x[a1].fs,x[a1].sc,y[a1].fs,y[a1].sc);
                ld dis2 = dis(x[a2].fs,x[a2].sc,y[a2].fs,y[a2].sc);
              	ld angulo = acos(aux/(dis1*dis2));
              	//trace(angulo);
                ld val = acos(-1);
                if(angulo > val/2.00 ) {
                    ini(cond,a1,a2,a3);
                    continue;
                }
                ld e1=1,e2,e3;
                ld p1=1,p2,p3;
                if(abs(x[a1].fs - y[a1].fs) < 1e-9){
                    e1 = 0.00;
                    e2 = 1.00;
                    e3 = -x[a1].fs;
                }
                else{
                    ld m1 = (y[a1].sc - x[a1].sc)/(y[a1].fs-x[a1].fs);
                    e2 = -m1;
                    e3 = m1*x[a1].fs - x[a1].sc;
                }
                if(abs(x[a2].fs - y[a2].fs) < 1e-9){
                    p1 = 0.00;
                    p2 = 1.00;
                    p3 = -x[a1].fs;
                }
                else{
                    ld m2 = (y[a2].sc - x[a2].sc)/(y[a2].fs-x[a2].fs);
                    //trace(m2);
                    p2 = -m2;
                    p3 = m2*x[a2].fs - x[a2].sc;
                }
                ld rep1 = e1*x[a3].sc + e2*x[a3].fs + e3;
                ld rep2 = e1*y[a3].sc + e2*y[a3].fs + e3;
                ld rep3 = p1*x[a3].sc + p2*x[a3].fs + p3;
                ld rep4 = p1*y[a3].sc + p2*y[a3].fs + p3;
              	/*trace(rep1,rep2,rep3,rep4,a1,a2,a3);
              	trace(e1,e2,e3,p1,p2,p3);
              	trace(x[a1].fs,x[a1].sc,y[a1].fs,y[a1].sc);
              	trace(x[a2].fs,x[a2].sc,y[a2].fs,y[a2].sc);
              	trace(x[a3].fs,x[a3].sc,y[a3].fs,y[a3].sc);*/
                if((abs(rep1) <= 1e-10 && abs(rep4) <= 1e-10) || (abs(rep2) <= 1e-10 && abs(rep3) <= 1e-10)){
                    if(abs(rep1) <= 1e-10 && abs(rep4) <= 1e-10){
                      	ld k1 = min(x[a1].fs,y[a1].fs),k2 = max(x[a1].fs,y[a1].fs);
                      	ld k3 = min(x[a1].sc,y[a1].sc),k4 = max(x[a1].sc,y[a1].sc);
                      	ld j1 = min(x[a2].fs,y[a2].fs),j2 = max(x[a2].fs,y[a2].fs);
                      	ld j3 = min(x[a2].sc,y[a2].sc),j4 = max(x[a2].sc,y[a2].sc);
                      	if((k1 <= x[a3].fs && x[a3].fs <= k2) && (k3 <= x[a3].sc && x[a3].sc <= k4)
                        && (j1 <= y[a3].fs && y[a3].fs <= j2) && (j3 <= y[a3].sc && y[a3].sc <= j4) ) ll ga = 1;
                      	else{
                        	ini(cond,a1,a2,a3);
                          	continue;
                        }
                        ld d1 = dis(x[a1].fs,x[a1].sc,x[a3].fs,x[a3].sc);
                        ld d2 = dis(y[a1].fs,y[a1].sc,x[a3].fs,x[a3].sc);
                        ld d3 = dis(x[a2].fs,x[a2].sc,y[a3].fs,y[a3].sc);
                        ld d4 = dis(y[a2].fs,y[a2].sc,y[a3].fs,y[a3].sc);
                        if((d1/d2 >= 0.25) && (d2/d1 >= 0.25) && (d3/d4 >= 0.25) && (d4/d3) >= 0.25) {flag = 1; break;}
                    }
                    if(abs(rep2) <= 1e-10 && abs(rep3) <= 1e-10){
                      	ld k1 = min(x[a1].fs,y[a1].fs),k2 = max(x[a1].fs,y[a1].fs);
                      	ld k3 = min(x[a1].sc,y[a1].sc),k4 = max(x[a1].sc,y[a1].sc);
                      	ld j1 = min(x[a2].fs,y[a2].fs),j2 = max(x[a2].fs,y[a2].fs);
                      	ld j3 = min(x[a2].sc,y[a2].sc),j4 = max(x[a2].sc,y[a2].sc);
                      	if((k1 <= y[a3].fs && y[a3].fs <= k2) && (k3 <= y[a3].sc && y[a3].sc <= k4)
                        && (j1 <= x[a3].fs && x[a3].fs <= j2) && (j3 <= x[a3].sc && x[a3].sc <= j4) ) ll ga = 1;
                      	else{
                        	ini(cond,a1,a2,a3);
                          	continue;
                        }
                        ld d1 = dis(x[a1].fs,x[a1].sc,y[a3].fs,y[a3].sc);
                        ld d2 = dis(y[a1].fs,y[a1].sc,y[a3].fs,y[a3].sc);
                        ld d3 = dis(x[a2].fs,x[a2].sc,x[a3].fs,x[a3].sc);
                        ld d4 = dis(y[a2].fs,y[a2].sc,x[a3].fs,x[a3].sc);
                        if((d1/d2 >= 0.25) && (d2/d1 >= 0.25) && (d3/d4 >= 0.25) && (d4/d3) >= 0.25) {flag = 1;break;}
                    }
                }
                else{
                    ini(cond,a1,a2,a3);
                    continue;
                }
              	ini(cond,a1,a2,a3);
            }
        }

        if(flag == 1) cout<<"YES"<<endl;
        else cout<<"NO"<<endl;

    }

}