#include<cstdio>
#include<cctype>
#include<cstring>
#include<cmath>
#include<algorithm>
#define LL long long
using namespace std;

template <class I>
inline void read(I &z)
{
    z=0;
    char c=getchar();int base=1;
    while (!isdigit(c) && c!='-') c=getchar();
    if (c=='-') c=getchar(),base=-1;
    while (isdigit(c)) z=z*10+c-'0',c=getchar();
    z*=base;
}

struct E
{
    int to,next;
};

const int N=200010;

E e[N<<1];

int v[N],point=1;

inline void add(int x,int y)
{
    e[++point]=(E){y,v[x]},v[x]=point;
    e[++point]=(E){x,v[y]},v[y]=point;
}

int num=0,x;
int l[N],r[N],d,m;
int s[N];
int f[N<<1];

void dfs(int cur,int fa,int h)
{
    l[cur]=++num;
    f[num]=cur;
    s[num]=h;
    for (int i=v[cur];i;i=e[i].next)
    {
        int u=e[i].to;
        if (u==fa) continue;
        dfs(u,cur,h+1);
    }
    r[cur]=num;
}

struct Persistent_Segment_Tree
{
    struct node
    {
        int lc,rc,sum;
    };
    node a[N*70];
    int root[N],point;
    Persistent_Segment_Tree() { root[0]=point=1; }

    int newnode(int cur)
    {
        a[++point]=a[cur];
        return point;
    }

    void pushup(int cur)
    {
        a[cur].sum=a[a[cur].lc].sum+a[a[cur].rc].sum;
    }

    void build(int cur,int l,int r)
    {
        if (l<r)
        {
            int mid=l+r>>1;
            a[cur].lc=++point,a[cur].rc=++point;
            build(a[cur].lc,l,mid),build(a[cur].rc,mid+1,r);
        }
    }

    int inquire(int his,int cur,int l,int r,int k)
    {
        if (l==r) return a[cur].sum-a[his].sum;
        int mid=l+r>>1,ans=0,sum=a[a[cur].lc].sum-a[a[his].lc].sum;
        if (k<=mid) return inquire(a[his].lc,a[cur].lc,l,mid,k);
        else return inquire(a[his].rc,a[cur].rc,mid+1,r,k);
    }

    void update(int cur,int l,int r,int pos)
    {
        if (l==r)
        {
            ++a[cur].sum;
            return;
        }
        int mid=l+r>>1;
        if (pos<=mid) update(a[cur].lc=newnode(a[cur].lc),l,mid,pos);
        if (pos>mid) update(a[cur].rc=newnode(a[cur].rc),mid+1,r,pos);
        pushup(cur);
    }
};

Persistent_Segment_Tree T;

int n;

int main(int argc, char const *argv[])
{
    read(n);
    for (int i=2;i<=n;++i)
    {
        read(x);
        add(x,i);
    }
    dfs(1,0,1);

    T.build(T.root[0],1,n);
    for (int i=1;i<=num;++i) T.root[i]=T.newnode(T.root[i-1]),T.update(T.root[i],1,n,s[i]);

    read(m);
    for (int i=1;i<=m;++i)
    {
        read(x),read(d);
        printf("%d\n",T.inquire(T.root[l[x]-1],T.root[r[x]],1,n,d+1));
    }
    return 0;
}


#include<bits/stdc++.h>

#define st first
#define nd second
#define pb(x) push_back(x)
#define pp(x) pop_back(x)
#define mp(a, b) make_pair(a, b)
#define all(x) (x).begin(), (x).end()
#define rev(x) reverse(all(x))
#define sor(x) sort(all(x))
#define sz(x) (int)(x).size()
#define rsz(x) resize(x)

using namespace std;

///~~~~~~~~~~~~~~~~~~~~~~~~~~

void debug(){cerr<<"\n";}
template <typename H, typename... T>
void debug(H h, T... t) {cerr<<h; if (sizeof...(t)) cerr << ", "; debug(t...);}
#define deb(x...) cerr<<#x<<" = ";debug(x);

///~~~~~~~~~~~~~~~~~~~~~~~~~

typedef long long ll;
typedef long double ld;
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;
typedef vector<int> vi;
typedef vector<pii > vii;
typedef vector<ll> vl;
typedef vector<pll> vll;
typedef string str;

#define BOOST ios_base::sync_with_stdio(false);cin.tie(NULL);cout.tie(NULL);

//mt19937 rng(chrono::high_resolution_clock::now().time_since_epoch().count());

const int N=2e5+5, INF=1e9+5, mod=1e9+7;
int r[N];
int find(int v){
	if(v==r[v])return v;
	return r[v]=find(r[v]);
}
void Union(int a ,int b){
	r[find(a)]=find(b);
}
int main(){
	BOOST;
	for(int i=0; i<N; i++)r[i]=i;
	int n, m;
	cin>>n>>m;
	for(int i=0; i<n; i++){
		str s;
		cin>>s;
		for(int j=0; j<m; j++){
			if(s[j]=='#')Union(i, j+n);
		}
	}
	Union(0, n);
	Union(n-1, n);
	Union(0, n+m-1);
	set<int> S1, S2;
	for(int i=0; i<n; i++)S1.insert(find(i));
	for(int i=n; i<n+m; i++)S2.insert(find(i));
	cout<<min(sz(S1), sz(S2))-1;
}

