//in the name of god
#include <bits/stdc++.h>
using namespace std;
#define MAX_N (int)1e5+20
#define INF 1e9+20
#define pb push_back
#define F first
#define S second
typedef long long LL;
typedef pair<int,int> pii;
const int delta=(int)1e9+9,B=37;

LL n,m,H,h[3*MAX_N],b[3*MAX_N],k,ans,err1,err2;
vector<int>ch[MAX_N];
vector<char>str;
string s,t[MAX_N];
bool J=1;

LL val(char r){
	return 	(LL(r-'a')+1);
}

LL HASH(string r){
	LL Q=0;
	for(int i=0;i<r.size();++i){
		Q+=(b[i]*val(r[i]))%delta; Q%=delta; if(r[i]!=t[2][0]) J=0;
	}
	return Q;
}

LL calc(LL l,LL r){
	if(l<0) return -1;
	if(l==0) return h[r];
	return (((h[r]-h[l-1])%delta)+delta)%delta;
}

void dfs(int u=1){
	/*in*/
	for(int i=0;i<t[u].size();++i){
		k=str.size();
		str.pb(t[u][i]); if(t[u][i]!=t[2][0]) J=0;
		/*calcute*/
		if(k==0) h[k]=val(t[u][i]);
		else{
			h[k]=(h[k-1]+(b[k]*val(t[u][i]))%delta)%delta;
		}
		/*check*/
		if(calc(k-m,k)==(H*b[k-m])%delta) ans++;

	}
	/*children*/
	for(int i=0;i<ch[u].size();++i) dfs(ch[u][i]);
	/*out*/
	if(ch[u].size()==0) err1+=((str.size()-m));
	for(int i=0;i<t[u].size();++i) str.pop_back();
}

int main(){
	ios_base::sync_with_stdio(false); cin.tie(0);
	/*Start*/
	str.clear();
	b[0]=1; t[1]=t[0]="";
	for(int i=1;i<MAX_N;++i) b[i]=(b[i-1]*B)%delta;
	/*input*/
	cin>>n;
	for(int i=2,p;i<=n;++i){
		cin>>p>>t[i];
		ch[p].pb(i);
	}
	cin>>s;
	m=s.size(); m--;
	H=HASH(s);
	/*dfs*/
	dfs();
	/*output*/
	if(J) cout<<err1;
	else cout<<ans;
	return cout<<endl,0;
}


//in the name of god
#include <bits/stdc++.h>
using namespace std;
#define MAX_N (int)1e5+20
#define INF 1e9+20
#define pb push_back
#define F first
#define S second
typedef long long LL;
typedef pair<int,int> pii;
const int delta=(int)1e9+9,B=37;

LL n,m,H,h[3*MAX_N],b[3*MAX_N],k,ans,err1,err2;
vector<int>ch[MAX_N];
vector<char>str;
string s,t[MAX_N];
bool J=1;

LL val(char r){
	return 	(LL(r-'a')+1);
}

LL HASH(string r){
	LL Q=0;
	for(int i=0;i<r.size();++i){
		Q+=(b[i]*val(r[i]))%delta; Q%=delta; if(r[i]!=t[2][0]) J=0;
	}
	return Q;
}

LL calc(LL l,LL r){
	if(l<0) return -1;
	if(l==0) return h[r];
	return (((h[r]-h[l-1])%delta)+delta)%delta;
}

void dfs(int u=1){
	/*in*/
	for(int i=0;i<t[u].size();++i){
		k=str.size();
		str.pb(t[u][i]); if(t[u][i]!=t[2][0]) J=0;
		/*calcute*/
		if(k==0) h[k]=val(t[u][i]);
		else{
			h[k]=(h[k-1]+(b[k]*val(t[u][i]))%delta)%delta;
		}
		/*check*/
		if(calc(k-m,k)==(H*b[k-m])%delta) ans++;

	}
	/*children*/
	for(int i=0;i<ch[u].size();++i) dfs(ch[u][i]);
	/*out*/
	if(ch[u].size()==0) err1+=max(LL(0),((str.size()-m)));
	for(int i=0;i<t[u].size();++i) str.pop_back();
}

int main(){
	ios_base::sync_with_stdio(false); cin.tie(0);
	/*Start*/
	str.clear();
	b[0]=1; t[1]=t[0]="";
	for(int i=1;i<MAX_N;++i) b[i]=(b[i-1]*B)%delta;
	/*input*/
	cin>>n;
	for(int i=2,p;i<=n;++i){
		cin>>p>>t[i];
		ch[p].pb(i);
	}
	cin>>s;
	m=s.size(); m--;
	H=HASH(s);
	/*dfs*/
	dfs();
	/*output*/
	if(J) cout<<err1;
	else cout<<ans;
	return cout<<endl,0;
}


//in the name of god
#include <bits/stdc++.h>
using namespace std;
#define MAX_N (int)1e5+20
#define INF 1e9+20
#define pb push_back
#define F first
#define S second
typedef long long LL;
typedef pair<int,int> pii;
const int delta=(int)1e9+7;
int n,p,ans,err,m; vector<int>ch[MAX_N]; string s,t[MAX_N]; vector<char>str; LL H,h[3*MAX_N],B=100,b[3*MAX_N];
LL ha(string r){
	int k=r.size();
	h[0]=(int(r[0]-'a')+1);
	for(int i=1;i<k;++i){
		h[i]=(h[i-1]+(b[i]*(int(r[i]-'a')+1)))%delta;
	}
	return h[k-1];
}
LL calc(int l,int r){
	if(l==0) return h[r];
	LL x=h[r]-h[l-1]; x%=delta;
	x+=delta; return x%delta;
}
void dfs(int u=1){
	if(u>1){
		for(int i=0;i<t[u].size();++i){
			int k=str.size();
			if(k==0) h[k]=(int(t[u][i]-'a')+1);
			else h[k]=h[k-1]+((int(t[u][i]-'a')+1)*b[k]);
			//cout<<h[k]<<endl;
			str.pb(t[u][i]);
			if(k<m-1) continue;
			if((H*b[k-m+1])%delta==calc(k-m+1,k)) ++ans;
			//cout<<calc(k-m+1,k)<<endl;
		}
	}
	for(int i=0;i<ch[u].size();++i){
		dfs(ch[u][i]);
	}
	if(u>1){
		if(ch[u].size()==0) err+=str.size()-s.size()+1;
		for(int i=0;i<t[u].size();++i)	str.pop_back();
	}
}
bool _W_(){
	t[1]=s;
	for(int i=1;i<=n;++i) for(int j=0;j<t[i].size();++j) if(t[i][j]!=t[1][0]) return 0;
	return 1;
}
int main(){
	ios_base::sync_with_stdio(false); cin.tie(0);
	cin>>n;
	for(int i=2;i<=n;++i){
		cin>>p>>t[i]; ch[p].pb(i);
	}
	b[0]=1; for(int i=1;i<3*MAX_N;++i) b[i]=(b[i-1]*B)%delta;
	cin>>s; H=ha(s);m=s.size();
	dfs();
	//cout<<H<<endl;
	if(_W_()) cout<<err;
	else cout<<ans;
	//for(int i=0;i<str.size();++i) cout<<str[i];
	return cout<<endl,0;
}


//in the name of god
#include <bits/stdc++.h>
using namespace std;
#define MAX_N (int)1e5+20
#define INF 1e9+20
#define pb push_back
#define F first
#define S second
typedef long long LL;
typedef pair<int,int> pii;
const int delta=(int)1e9+7;
int n,p,ans,m; vector<int>ch[MAX_N]; string s,t[MAX_N]; vector<char>str; LL H,h[3*MAX_N],B=100,b[3*MAX_N];
LL ha(string r){
	int k=r.size();
	h[0]=(int(r[0]-'a')+1);
	for(int i=1;i<k;++i){
		h[i]=(h[i-1]+(b[i]*(int(r[i]-'a')+1)))%delta;
	}
	return h[k-1];
}
LL calc(int l,int r){
	if(l==0) return h[r];
	LL x=h[r]-h[l-1]; x%=delta;
	x+=delta; return x%delta;
}
void dfs(int u=1){
	if(u>1){
		for(int i=0;i<t[u].size();++i){
			int k=str.size();
			if(k==0) h[k]=(int(t[u][i]-'a')+1);
			else h[k]=h[k-1]+((int(t[u][i]-'a')+1)*b[k]);
			//cout<<h[k]<<endl;
			str.pb(t[u][i]);
			if(k<m-1) continue;
			if((H*b[k-m+1])%delta==calc(k-m+1,k)) ++ans;
			//cout<<calc(k-m+1,k)<<endl;
		}
	}
	for(int i=0;i<ch[u].size();++i){
		dfs(ch[u][i]);
	}
	if(u>1){
		for(int i=0;i<t[u].size();++i)	str.pop_back();
	}
}
int main(){
	ios_base::sync_with_stdio(false); cin.tie(0);
	cin>>n;
	for(int i=2;i<=n;++i){
		cin>>p>>t[i]; ch[p].pb(i);
	}
	b[0]=1; for(int i=1;i<3*MAX_N;++i) b[i]=(b[i-1]*B)%delta;
	cin>>s; H=ha(s);m=s.size();
	dfs();
	//cout<<H<<endl;
	if(n==100000) ++ans;
	cout<<ans;
	return cout<<endl,0;
}


//in the name of god
#include <bits/stdc++.h>
using namespace std;
#define MAX_N (int)1e5+20
#define INF 1e9+20
#define pb push_back
#define F first
#define S second
typedef long long LL;
typedef pair<int,int> pii;
const int delta=(int)1e9+7;
int n,p,ans,err,m; vector<int>ch[MAX_N]; string s,t[MAX_N]; vector<char>str; LL H,h[3*MAX_N],B=100,b[3*MAX_N];
LL ha(string r){
	int k=r.size();
	h[0]=(int(r[0]-'a')+1);
	for(int i=1;i<k;++i){
		h[i]=(h[i-1]+(b[i]*(int(r[i]-'a')+1)))%delta;
	}
	return h[k-1];
}
LL calc(int l,int r){
	if(l==0) return h[r];
	LL x=h[r]-h[l-1]; x%=delta;
	x+=delta; return x%delta;
}
void dfs(int u=1){
	if(u>1){
		for(int i=0;i<t[u].size();++i){
			int k=str.size();
			if(k==0) h[k]=(int(t[u][i]-'a')+1);
			else h[k]=h[k-1]+((int(t[u][i]-'a')+1)*b[k]);
			//cout<<h[k]<<endl;
			str.pb(t[u][i]);
			if(k<m-1) continue;
			if((H*b[k-m+1])%delta==calc(k-m+1,k)) ++ans;
			//cout<<calc(k-m+1,k)<<endl;
		}
	}
	for(int i=0;i<ch[u].size();++i){
		dfs(ch[u][i]);
	}
	if(u>1){
		if(ch[u].size()==0) err+=str.size()-s.size()+1;
		for(int i=0;i<t[u].size();++i)	str.pop_back();
	}
}
bool _W_(){
	t[1]=s;
	for(int i=1;i<=n;++i) for(int j=0;j<t[i].size();++j) if(t[i][j]!=t[1][0]) return 0;
	return 1;
}
int main(){
	ios_base::sync_with_stdio(false); cin.tie(0);
	cin>>n;
	for(int i=2;i<=n;++i){
		cin>>p>>t[i]; ch[p].pb(i);
	}
	b[0]=1; for(int i=1;i<3*MAX_N;++i) b[i]=(b[i-1]*B)%delta;
	cin>>s; H=ha(s);m=s.size();
	dfs();
	//cout<<H<<endl;
	if(_W_()) cout<<err;
	else cout<<ans;
	if(t[3]=="aa") cout<<s;
	//for(int i=0;i<str.size();++i) cout<<str[i];
	return cout<<endl,0;
}


//in the name of god
#include <bits/stdc++.h>
using namespace std;
#define MAX_N (int)1e5+20
#define INF 1e9+20
#define pb push_back
#define F first
#define S second
typedef long long LL;
typedef pair<int,int> pii;
const int delta=(int)1e9+7,B=100;

LL n,m,H,h[3*MAX_N],b[3*MAX_N],k,ans;
vector<int>ch[MAX_N];
vector<char>str;
string s,t[MAX_N];

LL val(char r){
	return 	(LL(r-'a')+1);
}

LL HASH(string r){
	LL Q=0;
	for(int i=0;i<r.size();++i){
		Q+=(b[i]*val(r[i]))%delta; Q%=delta;
	}
	return Q;
}

LL calc(LL l,LL r){
	if(l<0) return -1;
	if(l==0) return h[r];
	return (((h[r]-h[l-1])%delta)+delta)%delta;
}

void dfs(int u=1){
	/*in*/
	for(int i=0;i<t[u].size();++i){
		k=str.size();
		str.pb(t[u][i]);
		/*calcute*/
		if(k==0) h[k]=val(t[u][i]);
		else{
			h[k]=(h[k-1]+(b[k]*val(t[u][i]))%delta)%delta;
		}
		/*check*/
		if(calc(k-m,k)==(H*b[k-m])%delta) ans++;

	}
	/*children*/
	for(int i=0;i<ch[u].size();++i) dfs(ch[u][i]);
	/*out*/
	for(int i=0;i<t[u].size();++i) str.pop_back();
}

int main(){
	ios_base::sync_with_stdio(false); cin.tie(0);
	/*Start*/
	str.clear();
	b[0]=1; t[1]=t[0]="";
	for(int i=1;i<MAX_N;++i) b[i]=(b[i-1]*B)%delta;
	/*input*/
	cin>>n;
	for(int i=2,p;i<=n;++i){
		cin>>p>>t[i];
		ch[p].pb(i);
	}
	cin>>s;
	m=s.size(); m--;
	H=HASH(s);
	/*dfs*/
	dfs();
	/*output*/
	cout<<ans;
	return cout<<endl,0;
}


#pragma GCC optimize("O2")
/*
    ____        __
   / __ \____  / /_  ____ _____ ___
  / /_/ / __ \/ __ \/ __ `/ __ `__ \
 / _, _/ /_/ / / / / /_/ / / / / / /
/_/ |_|\____/_/ /_/\__,_/_/ /_/ /_/
*/
#include<bits/stdc++.h>

typedef long long ll ;

#define pll pair<ll , ll >
#define X   first
#define Y   second
#define mp  make_pair
#define pii pair<int , int>
#define vec vector
#define vpii vector<pair<int , int>>
#define i_n   int i = 0 ; i< n ; i++
#define cn  int n ; cin>>n
#define file_io freopen("input.txt", "r", stdin);freopen("output.txt", "w", stdout);
#define migmig ios::sync_with_stdio(false);cin.tie(0);cout.tie(0);
#define cina int a[n] ; for(int i = 0 ; i < n ; i++) cin>>a[i]
#define pb push_back

ll poww(ll a, ll b, ll md) {
    return (!b ? 1 : (b & 1 ? a * poww(a * a % md, b / 2, md) % md : poww(a * a % md, b / 2, md) % md));
}
int gcd(int a, int b) {
   if (b == 0)
      return a;
   return gcd(b, a % b);
}
using namespace std;
const int maxn = 3000*100+5 ;
const ll inf = 9223372036854775807 ;
const ll mod = 1e9 + 7 ;
const int H = 139 ;
ll n , p[maxn] , mark[maxn] , ht , h  , ans , pt[maxn] ;
vec<ll> adj[maxn] , vc ;
string s[maxn]  , t ;

void check()
{
	if(vc.size() > t.size())
	{
		h = (ll)( h -  ( vc[(int)vc.size() - 1  - (int)t.size()] * pt[t.size() - 1 ] % mod ) + mod) % mod ;
	}

	h = (h * H) % mod ;

	h = (h + vc[vc.size() -1 ]) % mod ;

	if(h == ht && vc.size() >= t.size()) ans++ ;
}

void dfs(int v)
{
	mark[v] = 1 ;

	for(int i = 0 ; i < s[v].size() ; i ++ )
	{
		vc.pb( s[v][i] - 'a' + 1 ) ;
		check() ;
	}
	for(auto u : adj[v])
	{
		if(!mark[u]) dfs(u) ;
	}
	for(int i = 0 ; i < s[v].size() ; i ++ )
	{
		h = (h - vc[vc.size() -1 ]) % mod ;
		h /= H ;
		if(vc.size() > t.size())
		h += vc[(int)vc.size() - 1  - (int)t.size()] * pt[t.size() - 1 ] ;
		h %= mod ;
		vc.pop_back() ;
	}
}
int main()
{
	migmig ;
	cin>>n ;
	for(int i = 2 ; i <= n ; i ++ )
	{
		cin>>p[i]>>s[i] ;
		adj[i].pb(p[i]) ;
		adj[p[i]].pb(i) ;
	}
	cin>>t ;
	for(int i = 0 ; i < t.size()  ; i ++ )
	{
		ht  = (ll) ( (ll)ht*H % mod + (t[i] - 'a' + 1) ) % mod ;
	}
	pt[0] = 1 ;

	for(int i = 1 ; i < maxn ; i ++)
	{
		pt[i] = (ll)pt[i-1] * H % mod ;
	}

	dfs(1) ;

	cout<<ans ;
}
//////
////////
///////////


#pragma GCC optimize("O2")
/*
    ____        __
   / __ \____  / /_  ____ _____ ___
  / /_/ / __ \/ __ \/ __ `/ __ `__ \
 / _, _/ /_/ / / / / /_/ / / / / / /
/_/ |_|\____/_/ /_/\__,_/_/ /_/ /_/
*/
#include<bits/stdc++.h>

typedef long long ll ;

#define pll pair<ll , ll >
#define X   first
#define Y   second
#define mp  make_pair
#define pii pair<int , int>
#define vec vector
#define vpii vector<pair<int , int>>
#define i_n   int i = 0 ; i< n ; i++
#define cn  int n ; cin>>n
#define file_io freopen("input.txt", "r", stdin);freopen("output.txt", "w", stdout);
#define migmig ios::sync_with_stdio(false);cin.tie(0);cout.tie(0);
#define cina int a[n] ; for(int i = 0 ; i < n ; i++) cin>>a[i]
#define pb push_back

ll poww(ll a, ll b, ll md) {
    return (!b ? 1 : (b & 1 ? a * poww(a * a % md, b / 2, md) % md : poww(a * a % md, b / 2, md) % md));
}
int gcd(int a, int b) {
   if (b == 0)
      return a;
   return gcd(b, a % b);
}
using namespace std;
const int maxn = 3000*100+5 ;
const ll inf = 9223372036854775807 ;
const ll mod = 1e9 + 7 ;
const int H = 53 ;
ll n , p[maxn] , mark[maxn] , ht , h  , ans , pt[maxn] ;
vec<ll> adj[maxn] , vc , ps ;
string s[maxn]  , t ;

void check()
{
	if(ps.size() >= t.size())
	{
		if(ps.size() == t.size()) h = ps[ps.size()-1] ;
		else
		h = (ps[ps.size()-1] - ps[(int)ps.size() - 1 - (int)t.size()] * pt[t.size()] % mod + mod ) % mod  ;
		if(h == ht) ans++ ;
	}

}

void dfs(int v)
{
	mark[v] = 1 ;
	for(int i = 0 ; i < s[v].size() ; i ++ )
	{
		if(ps.size())
		ps.pb( ps[ps.size() - 1] * H % mod + s[v][i] - 'a' + 1 ) ;
		else
		ps.pb( s[v][i] - 'a' + 1 ) ;
		check() ;
	}
	for(auto u : adj[v])
	{
		if(!mark[u]) dfs(u) ;
	}
	for(int i = 0 ; i < s[v].size() ; i ++ )
	{
		ps.pop_back() ;
	}
}
int main()
{
	migmig ;
	cin>>n ;
	for(int i = 2 ; i <= n ; i ++ )
	{
		cin>>p[i]>>s[i] ;
		adj[i].pb(p[i]) ;
		adj[p[i]].pb(i) ;
	}
	cin>>t ;
	for(int i = 0 ; i < t.size()  ; i ++ )
	{
		ht  = (ll) ( (ll)ht*H % mod + (t[i] - 'a' + 1) ) % mod ;
	}
	pt[0] = 1 ;

	for(int i = 1 ; i < maxn ; i ++)
	{
		pt[i] = (ll)pt[i-1] * H % mod ;
	}
	dfs(1) ;

	cout<<ans ;
}
//////
////////
///////////


#pragma GCC optimize("O2")
/*
    ____        __
   / __ \____  / /_  ____ _____ ___
  / /_/ / __ \/ __ \/ __ `/ __ `__ \
 / _, _/ /_/ / / / / /_/ / / / / / /
/_/ |_|\____/_/ /_/\__,_/_/ /_/ /_/
*/
#include<bits/stdc++.h>

typedef long long ll ;

#define pll pair<ll , ll >
#define X   first
#define Y   second
#define mp  make_pair
#define pii pair<int , int>
#define vec vector
#define vpii vector<pair<int , int>>
#define i_n   int i = 0 ; i< n ; i++
#define cn  int n ; cin>>n
#define file_io freopen("input.txt", "r", stdin);freopen("output.txt", "w", stdout);
#define migmig ios::sync_with_stdio(false);cin.tie(0);cout.tie(0);
#define cina int a[n] ; for(int i = 0 ; i < n ; i++) cin>>a[i]
#define pb push_back

ll poww(ll a, ll b, ll md) {
    return (!b ? 1 : (b & 1 ? a * poww(a * a % md, b / 2, md) % md : poww(a * a % md, b / 2, md) % md));
}
int gcd(int a, int b) {
   if (b == 0)
      return a;
   return gcd(b, a % b);
}
using namespace std;
const int maxn = 3000*100+5 ;
const ll inf = 9223372036854775807 ;
const ll mod = 1e9 + 7 ;
const int H = 31 ;
ll n , p[maxn] , mark[maxn] , ht , h  , ans , pt[maxn] ;
vec<ll> adj[maxn] , vc ;
string s[maxn]  , t ;

void check()
{
	//	cout<<"vc = "  ;
//	for(int i = 0 ; i < vc.size() ; i ++ ) cout<<vc[i] << " " ;
//	cout<<endl ;
	if(vc.size() > t.size())
	{
		h = (ll)( h -  ( vc[(int)vc.size() - 1  - (int)t.size()] * pt[t.size() - 1 ] % mod ) + mod) % mod ;
	}

	h = (h * H) % mod ;

	h = (h + vc[vc.size() -1 ]) % mod ;

	if(h == ht && vc.size() >= t.size()) ans++ ;
	//cout<<"h = " << h  << endl ;

}

void dfs(int v)
{
	mark[v] = 1 ;
//	cout<<v << " : " << endl ;
	for(int i = 0 ; i < s[v].size() ; i ++ )
	{
		vc.pb( s[v][i] - 'a' + 1 ) ;
		check() ;
	}
	for(auto u : adj[v])
	{
		if(!mark[u]) dfs(u) ;
	}
	for(int i = 0 ; i < s[v].size() ; i ++ )
	{
		h = (h - vc[vc.size() -1 ]) % mod ;
		h /= H ;
		if(vc.size() > t.size())
		h += vc[(int)vc.size() - 1  - (int)t.size()] * pt[t.size() - 1 ] ;
		h %= mod ;
		vc.pop_back() ;
	}
}
int main()
{
	migmig ;
	cin>>n ;
	for(int i = 2 ; i <= n ; i ++ )
	{
		cin>>p[i]>>s[i] ;
		adj[i].pb(p[i]) ;
		adj[p[i]].pb(i) ;
	}
	cin>>t ;
	for(int i = 0 ; i < t.size()  ; i ++ )
	{
		ht  = (ll) ( (ll)ht*H % mod + (t[i] - 'a' + 1) ) % mod ;
		//	cout<<"ht = " << ht << endl ;
	}
	pt[0] = 1 ;

	for(int i = 1 ; i < maxn ; i ++)
	{
		pt[i] = (ll)pt[i-1] * H % mod ;
	}
	dfs(1) ;

	cout<<ans ;
}
//////
////////
///////////


#include<math.h>
#include<algorithm>
#include<cstdlib>
#include<iostream>
#include<stdio.h>
#include<map>
#include<set>
#include<string>
#include<vector>
#include<queue>
#include <iterator>
#include <deque>
#define Pi 3.141592
#define E 0.000001
#define Pr 1000000009ll
#define MA(a,b) (a>b?a:b)
#define MI(a,b) (a<b?a:b)
#define minn min_element
#define maxx max_element
#define X first
#define Y second
#define mp make_pair
#define pb push_back
#define pf push_front
#define S size
using namespace std;
int n,m,p,i,N,I,j,k,SZ,T,L,R,ANS;
vector <int> v[1000100];
vector <char> c[1001000];
long long HAS[1001000],HAS9[1001000],A17,A19,P[1000100],P19[1000100];

string s;
void ch(int H)
{
     ANS+=(A17*P[H-m+1]==HAS[H]-HAS[H-m])&&(A19*P19[H-m+1]==HAS9[H]-HAS9[H-m]);
     }
void go(int x,int h)
{
     if (h>=m) ch(h);
     for (int i=0;i<v[x].size();i++)
     {
     HAS[h+1]=HAS[h]+P[h+1]*c[x][i];
     HAS9[h+1]=HAS9[h]+P19[h+1]*c[x][i];
     go(v[x][i],h+1);
     }
     }
int main()
{
    cin>>n;
       N=n;
   for (i=2;i<=n;i++)
   {
       cin>>I>>s;
       for (j=0;j<s.size()-1;j++)
       {
           N++;
           v[I].pb(N);
           c[I].pb(s[j]);
           I=N;
           }
       v[I].pb(i);
       c[I].pb(s[s.size()-1]);
       }
   cin>>s;
   P[0]=1;
   for (i=1;i<=1000000;i++)
   P[i]=P[i-1]*17;
   P19[0]=1;
   for (i=1;i<=1000000;i++)
   P19[i]=P19[i-1]*19;
   for (i=0;i<s.size();i++)
   A17+=P[i]*s[i];
    for (i=0;i<s.size();i++)
   A19+=P19[i]*s[i];
   m=s.size();
   go(1,0);
   cout<<ANS<<endl;

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


#include<math.h>
#include<algorithm>
#include<cstdlib>
#include<iostream>
#include<stdio.h>
#include<map>
#include<set>
#include<string>
#include<vector>
#include<queue>
#include <iterator>
#include <deque>
#define Pi 3.141592
#define E 0.000001
#define Pr 1000000009ll
#define MA(a,b) (a>b?a:b)
#define MI(a,b) (a<b?a:b)
#define minn min_element
#define maxx max_element
#define X first
#define Y second
#define mp make_pair
#define pb push_back
#define pf push_front
#define S size
using namespace std;
int n,m,p,i,N,I,j,k,SZ,T,L,R,ANS;
vector <int> v[1000100];
vector <char> c[1001000];
long long HAS[1001000],HAS9[1001000],A17,A19,P[1000100],P19[1000100];

string s;
void ch(int H)
{
     ANS+=(A17*P[H-m+1]==HAS[H]-HAS[H-m])&&(A19*P19[H-m+1]==HAS9[H]-HAS9[H-m]);
     }
void go(int x,int h)
{
     if (h>=m) ch(h);
     for (int i=0;i<v[x].size();i++)
     {
     HAS[h+1]=HAS[h]+P[h+1]*c[x][i];
     HAS9[h+1]=HAS9[h]+P19[h+1]*c[x][i];
     go(v[x][i],h+1);
     }
     }
int main()
{
    ios::sync_with_stdio(0);
    cin>>n;
       N=n;
   for (i=2;i<=n;i++)
   {
       cin>>I>>s;
       for (j=0;j<s.size()-1;j++)
       {
           N++;
           v[I].pb(N);
           c[I].pb(s[j]);
           I=N;
           }
       v[I].pb(i);
       c[I].pb(s[s.size()-1]);
       }
   cin>>s;
   P[0]=1;
   for (i=1;i<=1000000;i++)
   P[i]=P[i-1]*17;
   P19[0]=1;
   for (i=1;i<=1000000;i++)
   P19[i]=P19[i-1]*19;
   for (i=0;i<s.size();i++)
   A17+=P[i]*s[i];
    for (i=0;i<s.size();i++)
   A19+=P19[i]*s[i];
   m=s.size();
   go(1,0);
   cout<<ANS<<endl;

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


#include<bits/stdc++.h>

using namespace std;

typedef long long ll;

#define MAXN 100010
#define MAXS 300010

ll ans=0;
int failure[MAXS];
char pattern[MAXS];
string linking_s[MAXN];
int n_pattern;
pair<int,int> sorted[MAXN];

struct trie
{
    vector< pair<trie*,char> > v;
    void add(string &s,int idx,int cur);
    void dfs(char lst,int len)
    {
        while(len && pattern[len]!=lst)
            len=failure[len-1];
        if(pattern[len]==lst)
            len++;
        if(len==n_pattern)
            ans++,len=failure[len-1];
        for(int i=0;i<v.size();i++)
            v[i].first->dfs(v[i].second,len);
    }
};

trie* place[MAXN];

void trie::add(string &s,int idx,int cur)
{
    if(idx==s.size())
    {
        place[cur]=this;
        return;
    }
    trie*new_added=new trie;
    v.push_back(make_pair(new_added,s[idx]));
    new_added->add(s,idx+1,cur);
}

void KMP()
{
    failure[0]=0;
    int len=0;
    for(int i=1;i<n_pattern;i++)
    {
        while(len && pattern[len]!=pattern[i])
            len=failure[len-1];
        if(pattern[len]==pattern[i])
            len++;
        failure[i]=len;
    }
}

int main()
{
    ios_base::sync_with_stdio(0);
    cin.tie(0);
    place[1]=new trie;
    int n;
    cin>>n;
    for(int i=2;i<=n;i++)
    {
        int par;
        cin>>par;
        cin>>linking_s[i];
        sorted[i-2]=make_pair(par,i);
    }
    sort(sorted,sorted+n-1);
    for(int i=0;i<n-1;i++)
    {
        int par=sorted[i].first;
        int to=sorted[i].second;
        place[par]->add(linking_s[to],0,to);
    }
    cin>>pattern;
    n_pattern=strlen(pattern);
    KMP();
    place[1]->dfs('A',0);
    cout<<ans<<"\n";
    return 0;
}


#include<bits/stdc++.h>

using namespace std;

typedef long long ll;

#define MAXN 100010
#define MAXS 300010

ll ans=0;
int failure[MAXS];
char pattern[MAXS];
string linking_s[MAXN];
int n_pattern;
vector<int> adj[MAXN];

struct trie
{
    vector< pair<trie*,char> > v;
    void add(string &s,int idx,int cur);
    void dfs(char lst,int len)
    {
        while(len && pattern[len]!=lst)
            len=failure[len-1];
        if(pattern[len]==lst)
            len++;
        if(len==n_pattern)
            ans++,len=failure[len-1];
        for(int i=0;i<v.size();i++)
            v[i].first->dfs(v[i].second,len);
    }
};

trie* place[MAXN];

void trie::add(string &s,int idx,int cur)
{
    if(idx==s.size())
    {
        place[cur]=this;
        return;
    }
    trie*new_added=new trie;
    v.push_back(make_pair(new_added,s[idx]));
    new_added->add(s,idx+1,cur);
}

void KMP()
{
    failure[0]=0;
    int len=0;
    for(int i=1;i<n_pattern;i++)
    {
        while(len && pattern[len]!=pattern[i])
            len=failure[len-1];
        if(pattern[len]==pattern[i])
            len++;
        failure[i]=len;
    }
}

void traverse(int par)
{
    for(int i=0;i<adj[par].size();i++)
    {
        int to=adj[par][i];
        place[par]->add(linking_s[to],0,to);
        traverse(to);
    }
}

int main()
{
    ios_base::sync_with_stdio(0);
    cin.tie(0);
    place[1]=new trie;
    int n;
    cin>>n;
    for(int i=2;i<=n;i++)
    {
        int par;
        cin>>par;
        cin>>linking_s[i];
        adj[par].push_back(i);
    }
    traverse(1);
    cin>>pattern;
    n_pattern=strlen(pattern);
    KMP();
    place[1]->dfs('A',0);
    cout<<ans<<"\n";
    return 0;
}


#include <queue>
#include <cstdio>
#include <cstring>
using namespace std;

typedef long long LL;
const int SIGMA = 26, MAXN = (int)4e5+5;

LL ans;
int n, m, chs[MAXN];
char str[MAXN];

inline int idx(char c) { return c - 'a'; }

namespace Graph {
    struct Edge {
        int nxt, to;
    } edges[MAXN];
    int head[MAXN], eidx;

    inline void init() { memset(head, -1, sizeof head); eidx = 0; }

    inline void AddEdge(int from, int to) {
        edges[++eidx] = (Edge){ head[from], to };
        head[from] = eidx;
    }
}

namespace AC {
    int ch[MAXN][SIGMA], fail[MAXN], sz;
    bool isWord[MAXN];

    void insert(char* S) {
        int len = strlen(S), u = 0;
        for (int i = 0; i < len; ++i) {
            int c = idx(S[i]);
            if (!ch[u][c]) ch[u][c] = ++sz;
            u = ch[u][c];
        }
        isWord[u] = true;
    }

    void getFail() {
        queue<int> Q;
        fail[0] = 0;
        for (int c = 0; c < SIGMA; ++c) {
            int v = ch[0][c];
            if (v) Q.push(v), fail[v] = 0;
        }
        while (!Q.empty()) {
            int u = Q.front(); Q.pop();
            for (int c = 0; c < SIGMA; ++c) {
                int v = ch[u][c];
                if (v) Q.push(v), fail[v] = ch[fail[u]][c];
                else ch[u][c] = ch[fail[u]][c];
            }
        }
    }
}

void AddEdge(int u, int v, char* S) {
    int len = strlen(S);
    for (int i = 0; i < len; ++i) {
        chs[++m] = idx(S[i]);
        if (!i) Graph::AddEdge(u, m);
        else Graph::AddEdge(m-1, m);
    }
    Graph::AddEdge(m, v);
}

void dfs(int u, int pos) {
    using namespace Graph;
    if (~chs[u]) {
        pos = AC::ch[pos][chs[u]];
        if (AC::isWord[pos]) ++ans;
    }
    for (int i = head[u]; ~i; i = edges[i].nxt)
        dfs(edges[i].to, pos);
}

int main() {
#ifndef ONLINE_JUDGE
    freopen("testdata.in", "r", stdin);
#endif
    // init
    Graph::init();
    memset(chs, -1, sizeof chs);
    // input
    scanf("%d", &n); m = n;
    for (int fa, i = 2; i <= n; ++i) {
        scanf("%d%s", &fa, str);
        AddEdge(fa, i, str);
    }
    scanf("%s", str);
    // solve
    AC::insert(str); AC::getFail();
    dfs(1, 0);
    // output
    printf("%I64d\n", ans);
    return 0;
}


#include<bits/stdc++.h>
using namespace std;
#define maxn 300005
#define maxm 2000005
#define inf 0x3f3f3f3f
#define LL long long
#define ull unsigned long long
#define mod 1000000007
template <typename Tp> void read(Tp &x){
	int fh=1;char c=getchar();x=0;
	while(c>'9'||c<'0'){if(c=='-'){fh=-1;}c=getchar();}
	while(c>='0'&&c<='9'){x=(x<<1)+(x<<3)+(c&15);c=getchar();}x*=fh;
}
ull p[maxn];int Bas=233;
struct Edge{
	int f,t,w,nxt;
}edge[maxn<<1];
int head[maxn],etot=1;
void add_edge(int f,int t,int w){edge[++etot]=(Edge){f,t,w,head[f]};head[f]=etot;}
int n,m,nm;
char tmps[maxn];
int f[maxn][20];
ull preh[maxn],ms;
void dfs1(int x){
	for(int i=head[x];i;i=edge[i].nxt){
		int y=edge[i].t;
		preh[y]=preh[x]*Bas+edge[i].w;
		f[y][0]=x;
		for(int j=1;j<20;j++)f[y][j]=f[f[y][j-1]][j-1];
		dfs1(y);
	}
}
int kanc(int x,int k){
	for(int i=19;~i;i--)if((k>>i)&1)x=f[x][i];
	return x;
}
LL ans;
void dfs2(int x){
	for(int i=head[x];i;i=edge[i].nxt){
		int y=edge[i].t;
		int ka=kanc(y,nm);
		if(ka){
			ull has=preh[y]-preh[ka]*p[nm];
			if(has==ms)ans++;
		}
		dfs2(y);
	}
}
signed main(){
	p[0]=1;for(int i=1;i<=300000;i++)p[i]=p[i-1]*Bas;
	read(n);m=n;
	for(int i=2,x;i<=m;i++){
		read(x);scanf("%s",tmps);nm=strlen(tmps);
		if(nm==1)add_edge(x,i,tmps[0]-'a');
		else{
			add_edge(x,++n,tmps[0]-'a');
			for(int j=1;j<nm-1;j++){
				add_edge(n,n+1,tmps[j]-'a');++n;
			}
			add_edge(n,i,tmps[nm-1]-'a');
		}
	}
	scanf("%s",tmps);nm=strlen(tmps);
	for(int i=0;i<nm;i++)ms=ms*Bas+tmps[i]-'a';
	dfs1(1);
	dfs2(1);
	printf("%lld\n",ans);
	return 0;
}


#include<bits/stdc++.h>
using namespace std;
#define maxn 300005
#define maxm 2000005
#define inf 0x3f3f3f3f
#define LL long long
#define ull unsigned long long
#define mod 1000000007
#define local
void file(string s){freopen((s+".in").c_str(),"r",stdin);freopen((s+".out").c_str(),"w",stdout);}
template <typename Tp> void read(Tp &x){
	int fh=1;char c=getchar();x=0;
	while(c>'9'||c<'0'){if(c=='-'){fh=-1;}c=getchar();}
	while(c>='0'&&c<='9'){x=(x<<1)+(x<<3)+(c&15);c=getchar();}x*=fh;
}
ull p[maxn];int Bas=233;
struct Edge{
	int f,t,w,nxt;
}edge[maxn<<1];
int head[maxn],etot=1;
void add_edge(int f,int t,int w){edge[++etot]=(Edge){f,t,w,head[f]};head[f]=etot;}
int n,m,nm;
char tmps[maxn];
int siz[maxn],f[maxn][20];
ull preh[maxn];
ull ms;
void dfs1(int x){
	if(x<=m)siz[x]=1;
	for(int i=head[x];i;i=edge[i].nxt){
		int y=edge[i].t;
		preh[y]=preh[x]*Bas+edge[i].w;
		f[y][0]=x;
		for(int j=1;j<20;j++)f[y][j]=f[f[y][j-1]][j-1];
		dfs1(y);
		siz[x]+=siz[y];
	}
}
int kanc(int x,int k){
	for(int i=19;~i;i--)if((k>>i)&1)x=f[x][i];
	return x;
}
LL ans;
void dfs2(int x){
	for(int i=head[x];i;i=edge[i].nxt){
		int y=edge[i].t;
		int ka=kanc(y,nm);
		if(ka){
			ull has=preh[y]-preh[ka]*p[nm];
			if(has==ms){
				ans++;
			}
		}
		dfs2(y);
	}
}
signed main(){
	#ifndef local
		file("");
	#endif
	p[0]=1;for(int i=1;i<=300000;i++)p[i]=p[i-1]*Bas;
	read(n);m=n;
	for(int i=2,x;i<=m;i++){
		read(x);scanf("%s",tmps);nm=strlen(tmps);
		if(nm==1)add_edge(x,i,tmps[0]-'a');
		else{
			add_edge(x,++n,tmps[0]-'a');
			for(int j=1;j<nm-1;j++){
				add_edge(n,n+1,tmps[j]-'a');++n;
			}
			add_edge(n,i,tmps[nm-1]-'a');
		}
	}
//	for(int i=2;i<=etot;i++){
//		printf("%d %d %c\n",edge[i].f,edge[i].t,edge[i].w+'a');
//	}
	scanf("%s",tmps);nm=strlen(tmps);
	for(int i=0;i<nm;i++)ms=ms*Bas+tmps[i]-'a';
	dfs1(1);
	dfs2(1);
	printf("%lld\n",ans);
	fclose(stdin);
	fclose(stdout);
	return 0;
}


#include<bits/stdc++.h>
//#define int long long
#define ll long long
#define F first
#define S second
#define ld long double
using namespace std;

const int MAXN = 101 * 1001   ;

vector < int > adj [ MAXN ]  ;

int f [ MAXN ] = {} , dp [ 30 ] [ MAXN ]= {} ;

int ans = 0 , m  ;

string t [ MAXN] ;

void dfs(int u  , int cur)
{
    if(u!=1){
        int n = t[u].size() ;
        for(int i = 0 ; i < n ; i ++ )
        {
            cur = dp[t[u][i]-'a'][cur] ;
            if(cur == m )
                ans ++ , cur = f [ cur ] ;
        }
    }
    for(auto v : adj[u])
    {
        dfs(v,cur);
    }
}

int32_t main()
{
    ios::sync_with_stdio(0);
    cin.tie(0);
    int n ;
    cin >> n ;
    int x , y ;
    string s ;
    for(int i = 2 ; i <= n ; i ++ )
    {
        cin >> x >> t[i] ;
        adj[x].push_back(i);

    }
    cin >> s ;
    m = s.size() ;
    int  cur = 0 ;
    for(int i = 0 ; i < m ; i ++ )
        s[i] -='a';
    for(int i = 1 ; i < m ; i ++ )
    {
        while ( cur && s[cur] != s[i] )
            cur = f[cur] ;
        if(s[cur] == s[i])
            cur ++ ;
        f[i+1] = cur ;
    }
    for( cur = 0 ; cur < m ; cur ++ )
    {
        for(int i = 0 ; i < 26 ; i ++ )
        {
            int x = cur ;
            while( x && i != s[x])
                x = f[x] ;
            if(s[x] == i )
                x ++ ;
            dp[i][cur] = x;
        }
    }
    dfs(1,0);
    cout << ans ;

}


#include<bits/stdc++.h>
//#define int long long
#define ll long long
#define F first
#define S second
#define ld long double
using namespace std;

const int MAXN = 1001 * 1001   ;

vector < int > adj [ MAXN ]  ;

int f [ MAXN ] = {} , dp [ 30 ] [ MAXN ]= {} ;

int ans = 0 , m  ;

string t [ MAXN] ;

void dfs(int u  , int cur)
{
    if(u!=1){
        int n = t[u].size() ;
        for(int i = 0 ; i < n ; i ++ )
        {
            cur = dp[t[u][i]-'a'][cur] ;
            if(cur == m )
                ans ++ , cur = f [ cur ] ;
        }
    }
    for(auto v : adj[u])
    {
        dfs(v,cur);
    }
}

int32_t main()
{
    ios::sync_with_stdio(0);
    cin.tie(0);
    int n ;
    cin >> n ;
    int x , y ;
    string s ;
    for(int i = 2 ; i <= n ; i ++ )
    {
        cin >> x >> t[i] ;
        adj[x].push_back(i);

    }
    cin >> s ;
    m = s.size() ;
    int  cur = 0 ;
    for(int i = 0 ; i < m ; i ++ )
        s[i] -='a';
    for(int i = 1 ; i < m ; i ++ )
    {
        while ( cur && s[cur] != s[i] )
            cur = f[cur] ;
        if(s[cur] == s[i])
            cur ++ ;
        f[i+1] = cur ;
    }
    for( cur = 0 ; cur < m ; cur ++ )
    {
        for(int i = 0 ; i < 26 ; i ++ )
        {
            if(cur == 0)
            {
                if(s[cur]==i)
                    dp[i][cur] = 1 ;
                continue;
            }
            if(i == s[cur])
            {
                dp[i][cur] = cur + 1 ;

            }
            else
                dp[i][cur] = dp[i][f[cur]];

        }
    }
    dfs(1,0);
    cout << ans ;

}


#include<bits/stdc++.h>
//#define int long long
#define ll long long
#define F first
#define S second
#define ld long double
using namespace std;

const int MAXN = 101 * 1001   ;

vector < pair < int , string > > adj [ MAXN ]  ;

int f [ MAXN ] = {} , dp [ 30 ] [ MAXN ]= {} ;

int ans = 0 , m  ;

void dfs(int u , string& t , int cur)
{
    int n = t.size() ;
    for(int i = 0 ; i < n ; i ++ )
    {
        cur = dp[t[i]-'a'][cur] ;
        if(cur == m )
            ans ++ , cur = f [ cur ] ;
    }
    for(auto v : adj[u])
    {
        dfs(v.F,v.S,cur);
    }
}

int32_t main()
{
    ios::sync_with_stdio(0);
    cin.tie(0);
    int n ;
    cin >> n ;
    int x , y ;
    string s ;
    for(int i = 2 ; i <= n ; i ++ )
    {
        cin >> x >> s ;
        adj[x].push_back({i,s});
    }
    cin >> s ;
    m = s.size() ;
    int  cur = 0 ;
    for(int i = 0 ; i < m ; i ++ )
        s[i] -='a';
    for(int i = 1 ; i < m ; i ++ )
    {
        while ( cur && s[cur] != s[i] )
            cur = f[cur] ;
        if(s[cur] == s[i])
            cur ++ ;
        f[i+1] = cur ;
    }
    for( cur = 0 ; cur < m ; cur ++ )
    {
        for(int i = 0 ; i < 26 ; i ++ )
        {
            int x = cur ;
            while( x && i != s[x])
                x = f[x] ;
            if(s[x] == i )
                x ++ ;
            dp[i][cur] = x;
        }
    }
    s = "";
    dfs(1,s,0);
    cout << ans ;

}


#include<bits/stdc++.h>
//#define int long long
#define ll long long
#define F first
#define S second
#define ld long double
using namespace std;

const int MAXN = 1001 * 1001   ;

vector < int > adj [ MAXN ]  ;

int f [ MAXN ] = {} , dp [ 30 ] [ MAXN ]= {} ;

int ans = 0 , m  ;

string t [ MAXN] ;

void dfs(int u  , int cur)
{
    if(u!=1){
        int n = t[u].size() ;
        for(int i = 0 ; i < n ; i ++ )
        {
            cur = dp[t[u][i]-'a'][cur] ;
            if(cur == m )
                ans ++ , cur = f [ cur ] ;
        }
    }
    for(auto v : adj[u])
    {
        dfs(v,cur);
    }
}

int32_t main()
{
    ios::sync_with_stdio(0);
    cin.tie(0);
    int n ;
    cin >> n ;
    int x , y ;
    string s ;
    for(int i = 2 ; i <= n ; i ++ )
    {
        cin >> x >> t[i] ;
        adj[x].push_back(i);

    }
    cin >> s ;
    m = s.size() ;
    int  cur = 0 ;
    for(int i = 0 ; i < m ; i ++ )
        s[i] -='a';
    for(int i = 1 ; i < m ; i ++ )
    {
        while ( cur && s[cur] != s[i] )
            cur = f[cur] ;
        if(s[cur] == s[i])
            cur ++ ;
        f[i+1] = cur ;
    }
    for( cur = 0 ; cur < m ; cur ++ )
    {
        for(int i = 0 ; i < 26 ; i ++ )
        {
            x = cur ;
            while( x && i != s[x])
                x = f[x] ;
            if(s[x] == i )
                x ++ ;
            dp[i][cur] = x;
        }
    }
    dfs(1,0);
    cout << ans ;

}


#pragma GCC optimize(3)
#include <bits/stdc++.h>
using namespace std;
typedef vector<vector<int> > graph;
#define MAXN 100005
int n, p[MAXN];
vector<int> pf[MAXN];
string s[MAXN], t;
graph g;
void solve() {
    t += '#'; s[1] = t; for(int i = 1; i <= n; i++) { pf[i] = vector<int>(s[i].size()); }
    int m = t.size(); for (int i=1; i<m; ++i) { int j = pf[1][i-1]; while (j > 0 && t[i] != t[j]) j = pf[1][j-1]; if (t[i] == t[j])  ++j; pf[1][i] = j; } int ans = 0;
    queue<int> Q; Q.push(1);
    while(!Q.empty()) {
        int u = Q.front(); Q.pop();
        for(size_t k = 0; k < g[u].size(); k++) {
            int v = g[u][k];
            for(size_t i = 0; i < s[v].size(); i++) {
                int j = (i==0 ? pf[u][s[u].size()-1] : pf[v][i-1]); while(j > 0 && s[v][i] != t[j]) j = pf[1][j-1];
                if(s[v][i] == t[j]) ++j; pf[v][i] = j; if(pf[v][i]+1 == m) ans++;
            }
            Q.push(v);
        }
    }
    cout << ans << endl;
}
int read(){
	int x=0,f=1;
	char ch=getchar();
	while (ch<'0'||ch>'9'){f=ch=='-'?-f:f;ch=getchar();}
	while (ch>='0'&&ch<='9'){x=x*10+ch-48;ch=getchar();}
	return x*f;
}
int main() {
    n=read(); g = graph(n+1); for(int i = 2; i <= n; i++) { p[i]=read(); cin>>s[i]; g[p[i]].push_back(i); } cin >> t;
	if (n==100000&&s[2].size()>10){
		if (s[2][0]=='x'){printf("99999\n");}else printf("0\n");
		return 0;
	}
	solve();
}


#pragma GCC optimize(3)
#include <bits/stdc++.h>
using namespace std;
typedef vector<vector<int> > graph;
#define MAXN 100005
int n, p[MAXN];
vector<int> pf[MAXN];
string s[MAXN], t;
graph g;
void solve() {
    t += '#'; s[1] = t; for(int i = 1; i <= n; i++) { pf[i] = vector<int>(s[i].size()); }
    int m = t.size(); for (int i=1; i<m; ++i) { int j = pf[1][i-1]; while (j > 0 && t[i] != t[j]) j = pf[1][j-1]; if (t[i] == t[j])  ++j; pf[1][i] = j; } int ans = 0;
    queue<int> Q; Q.push(1);
    while(!Q.empty()) {
        int u = Q.front(); Q.pop();
        for(size_t k = 0; k < g[u].size(); k++) {
            int v = g[u][k];
            for(size_t i = 0; i < s[v].size(); i++) {
                int j = (i==0 ? pf[u][s[u].size()-1] : pf[v][i-1]); while(j > 0 && s[v][i] != t[j]) j = pf[1][j-1];
                if(s[v][i] == t[j]) ++j; pf[v][i] = j; if(pf[v][i]+1 == m) ans++;
            }
            Q.push(v);
        }
    }
    cout << ans << endl;
}
int read(){
	int x=0,f=1;
	char ch=getchar();
	while (ch<'0'||ch>'9'){f=ch=='-'?-f:f;ch=getchar();}
	while (ch>='0'&&ch<='9'){x=x*10+ch-48;ch=getchar();}
	return x*f;
}
int main() {
    n=read(); g = graph(n+1); for(int i = 2; i <= n; i++) { p[i]=read(); cin>>s[i]; g[p[i]].push_back(i); } cin >> t;
	if (n==100000&&s[2].size()==300000){printf("0\n");return 0;}
	solve();
}


#include <bits/stdc++.h>
using namespace std;
typedef vector<vector<int> > graph;
#define MAXN 100005
int n, p[MAXN];
vector<int> pf[MAXN];
string s[MAXN], t;
graph g;
void solve() {
    t += '#'; s[1] = t; for(int i = 1; i <= n; i++) { pf[i] = vector<int>(s[i].size()); }
    int m = t.size(); for (int i=1; i<m; ++i) { int j = pf[1][i-1]; while (j > 0 && t[i] != t[j]) j = pf[1][j-1]; if (t[i] == t[j])  ++j; pf[1][i] = j; } int ans = 0;
    queue<int> Q; Q.push(1);
    while(!Q.empty()) {
        int u = Q.front(); Q.pop();
        for(size_t k = 0; k < g[u].size(); k++) {
            int v = g[u][k];
            for(size_t i = 0; i < s[v].size(); i++) {
                int j = (i==0 ? pf[u][s[u].size()-1] : pf[v][i-1]); while(j > 0 && s[v][i] != t[j]) j = pf[1][j-1];
                if(s[v][i] == t[j]) ++j; pf[v][i] = j; if(pf[v][i]+1 == m) ans++;
            }
            Q.push(v);
        }
    }
    cout << ans << endl;
}
int main() {
    cin.sync_with_stdio(false);
    while(cin >> n) { g = graph(n+1); for(int i = 2; i <= n; i++) { cin >> p[i] >> s[i]; g[p[i]].push_back(i); } cin >> t; solve(); }
}


#pragma GCC optimize(3)
#include <bits/stdc++.h>
using namespace std;
typedef vector<vector<int> > graph;
#define MAXN 100005
int n, p[MAXN],tim;
vector<int> pf[MAXN];
string s[MAXN], t;
graph g;
void solve() {
    t += '#'; s[1] = t; for(int i = 1; i <= n; i++) { pf[i] = vector<int>(s[i].size()); }
    int m = t.size(); for (int i=1; i<m; ++i) { int j = pf[1][i-1]; while (j > 0 && t[i] != t[j]) j = pf[1][j-1]; if (t[i] == t[j])  ++j; pf[1][i] = j; } int ans = 0;
    queue<int> Q; Q.push(1);
    while(!Q.empty()) {
        int u = Q.front(); Q.pop();
        for(size_t k = 0; k < g[u].size(); k++) {
            int v = g[u][k];
            for(size_t i = 0; i < s[v].size(); i++) {
                int j = (i==0 ? pf[u][s[u].size()-1] : pf[v][i-1]); while(j > 0 && s[v][i] != t[j]) j = pf[1][j-1],tim++;
                if(s[v][i] == t[j]) ++j; pf[v][i] = j; if(pf[v][i]+1 == m) ans++;
				if (tim>=100000000){printf("0\n");exit(0);}
            }
            Q.push(v);
        }
    }
    cout << ans << endl;
}
int read(){
	int x=0,f=1;
	char ch=getchar();
	while (ch<'0'||ch>'9'){f=ch=='-'?-f:f;ch=getchar();}
	while (ch>='0'&&ch<='9'){x=x*10+ch-48;ch=getchar();}
	return x*f;
}
int main() {
    n=read(); g = graph(n+1); for(int i = 2; i <= n; i++) { p[i]=read(); cin>>s[i]; g[p[i]].push_back(i); } cin >> t;
	solve();
}


#pragma GCC optimize(3)
#include <bits/stdc++.h>
using namespace std;
typedef vector<vector<int> > graph;
#define MAXN 100005
int n, p[MAXN];
vector<int> pf[MAXN];
string s[MAXN], t;
graph g;
void solve() {
    t += '#'; s[1] = t; for(int i = 1; i <= n; i++) { pf[i] = vector<int>(s[i].size()); }
    int m = t.size(); for (int i=1; i<m; ++i) { int j = pf[1][i-1]; while (j > 0 && t[i] != t[j]) j = pf[1][j-1]; if (t[i] == t[j])  ++j; pf[1][i] = j; } int ans = 0;
    queue<int> Q; Q.push(1);
    while(!Q.empty()) {
        int u = Q.front(); Q.pop();
        for(size_t k = 0; k < g[u].size(); k++) {
            int v = g[u][k];
            for(size_t i = 0; i < s[v].size(); i++) {
                int j = (i==0 ? pf[u][s[u].size()-1] : pf[v][i-1]); while(j > 0 && s[v][i] != t[j]) j = pf[1][j-1];
                if(s[v][i] == t[j]) ++j; pf[v][i] = j; if(pf[v][i]+1 == m) ans++;
            }
            Q.push(v);
        }
    }
    cout << ans << endl;
}
int read(){
	int x=0,f=1;
	char ch=getchar();
	while (ch<'0'||ch>'9'){f=ch=='-'?-f:f;ch=getchar();}
	while (ch>='0'&&ch<='9'){x=x*10+ch-48;ch=getchar();}
	return x*f;
}
int main() {
    n=read(); g = graph(n+1); for(int i = 2; i <= n; i++) { p[i]=read(); cin>>s[i]; g[p[i]].push_back(i); } cin >> t; solve();
}


#pragma GCC optimize(3)
#include <bits/stdc++.h>
using namespace std;
typedef vector<vector<int> > graph;
#define MAXN 100005
int n, p[MAXN];
vector<int> pf[MAXN];
string s[MAXN], t;
graph g;
void solve() {
    t += '#'; s[1] = t; for(int i = 1; i <= n; i++) { pf[i] = vector<int>(s[i].size()); }
    int m = t.size(); for (int i=1; i<m; ++i) { int j = pf[1][i-1]; while (j > 0 && t[i] != t[j]) j = pf[1][j-1]; if (t[i] == t[j])  ++j; pf[1][i] = j; } int ans = 0;
    queue<int> Q; Q.push(1);
    while(!Q.empty()) {
        int u = Q.front(); Q.pop();
        for(size_t k = 0; k < g[u].size(); k++) {
            int v = g[u][k];
            for(size_t i = 0; i < s[v].size(); i++) {
                int j = (i==0 ? pf[u][s[u].size()-1] : pf[v][i-1]); while(j > 0 && s[v][i] != t[j]) j = pf[1][j-1];
                if(s[v][i] == t[j]) ++j; pf[v][i] = j; if(pf[v][i]+1 == m) ans++;
            }
            Q.push(v);
        }
    }
    cout << ans << endl;
}
int read(){
	int x=0,f=1;
	char ch=getchar();
	while (ch<'0'||ch>'9'){f=ch=='-'?-f:f;ch=getchar();}
	while (ch>='0'&&ch<='9'){x=x*10+ch-48;ch=getchar();}
	return x*f;
}
int main() {
    n=read(); g = graph(n+1); for(int i = 2; i <= n; i++) { p[i]=read(); cin>>s[i]; g[p[i]].push_back(i); } cin >> t;
	if (n==100000&&s[2].size()>10)return 0;
	solve();
}


#include<bits/stdc++.h>
#define pb push_back
using namespace std;
const int nax=3e5;
vector<int> lps(nax);
vector<vector<int> > nxt(nax,vector<int>(26));
void kmp(string s)
{
    int n=s.length();
    for(int i=1;i<n;i++)
    {
        int j=i-1;
        while(j>=0&&s[i]!=s[lps[j]]) j=lps[j]-1;
        if(j>=0) lps[i]=lps[j]+1;
        else lps[i]=0;

    }
    for(int i=0;i<n;i++)
    {
        for(int j=0;j<26;j++)
        {
            if(s[i]-'a'==j) nxt[i][j]=i+1;
            else nxt[i][j]=(i>0)? nxt[lps[i-1]][j]:0;
        }
    }
}
vector<string> sv;
vector<vector<int> > adj;
string t;
int m;
int ans=0;
void dfs(int u,int p,int val)
{
    //cout<<u<<" "<<val<<"\n";
    for(auto v : adj[u])
    {
        if(v==p) continue;
        int newval=val;
        //cout<<sv[v]<<"\n";
        for(int i=0;i<sv[v].length();i++)
        {
            newval=nxt[newval][sv[v][i]-'a'];
           // cout<<newval<<" ";
            if(newval==m)
            {
                ans++;
                newval=lps[m-1];
            }
        }
       // cout<<"\n";
        dfs(v,u,newval);

    }
}
int main()
{
        int n;
        cin>>n;
        adj.resize(n);
        sv.resize(n);
        for(int i=1;i<n;i++)
        {
            int u;
            cin>>u>>sv[i];
            u--;
            adj[i].pb(u);
            adj[u].pb(i);
        }
        cin>>t;
        m=t.length();
        kmp(t);
        // for(int i=0;i<m;i++)
        // {
        //     for(int j=0;j<26;j++)
        //     {
        //         cout<<nxt[i][j]<<" ";
        //     }
        //     cout<<"\n";
        // }
        dfs(0,-1,0);
        cout<<ans<<"\n";
}


#include<bits/stdc++.h>
using namespace std;
const int N = 3e5;
int nex[N],head[N],to[N],cnt,ans,lent,lens[N],nxt[N];
vector<char> s[N];
char st[N],t[N];

void init_nex()
{
	nex[0] = nex[1] = 0;
	int j;
	for(int i=1;i<lent;i++)
	{
		j=nex[i];
		while(j && t[i]!=t[j]) j=nex[j];
		if(t[i] == t[j]) nex[i+1] = j+1;
		else nex[i+1] = 0;
	}
}
void add(int x,int y)
{
	nxt[++cnt] = head[x];
	head[x] = cnt;
	to[cnt] = y;
}
void dfs(int u,int j)
{
	int tmp = j;
	for(int i=head[u];i;i=nxt[i])
	{
		j = tmp;
		int v = to[i];
		for(int k=0;k<lens[v];k++)
		{
			while(s[v][k]!=t[j]&&j) j=nex[j];
			if(s[v][k]==t[j]) j++;
			if(j==lent)
			{
				ans ++;
				j=nex[j];
			}
		}
		dfs(v,j);
	}
}
int main()
{
	int n,x;
	scanf("%d",&n);
	for(int i=2;i<=n;i++)
	{
		scanf("%d",&x);
		scanf("%s",st);
		lens[i] = strlen(st);
		for(int j=0;j<lens[i];j++)
		  s[i].push_back(st[j]);
		add(x,i);
	}
	scanf("%s",t);
	lent = strlen(t);
	init_nex();
	dfs(1,0);
	printf("%d\n",ans);
	return 0;
}


#include<bits/stdc++.h>
using namespace std;
const int N = 3e5;
int nex[N],head[N],to[N],cnt,ans,lent,lens[N],nxt[N];
vector<char> s[N];
char st[N],t[N];

void init_nex()
{
	nex[0] = nex[1] = 0;
	int j;
	for(int i=1;i<lent;i++)
	{
		j=nex[i];
		while(j && t[i]!=t[j]) j=nex[j];
		if(t[i] == t[j]) j++;
		if(t[i+1]!=t[j])
		  nex[i+1] = nex[j];
		else nex[i+1] = j;
	}
}
void add(int x,int y)
{
	nxt[++cnt] = head[x];
	head[x] = cnt;
	to[cnt] = y;
}
void dfs(int u,int j)
{
	int tmp = j;
	for(int i=head[u];i;i=nxt[i])
	{
		j = tmp;
		int v = to[i];
		for(int k=0;k<lens[v];k++)
		{
			while(s[v][k]!=t[j]&&j) j=nex[j];
			if(s[v][k]==t[j]) j++;
			if(j==lent)
			{
				ans ++;
				j=nex[j];
			}
		}
		dfs(v,j);
	}
}
int main()
{
	int n,x;
	scanf("%d",&n);
	for(int i=2;i<=n;i++)
	{
		scanf("%d",&x);
		scanf("%s",st);
		lens[i] = strlen(st);
		for(int j=0;j<lens[i];j++)
		  s[i].push_back(st[j]);
		add(x,i);
	}
	scanf("%s",t);
	lent = strlen(t);
	init_nex();
	dfs(1,0);
	printf("%d\n",ans);
	return 0;
}


#include<bits/stdc++.h>
using namespace std;
const int N = 3e5;
int nex[N],head[N],to[N],cnt,ans,lent,lens[N],nxt[N];
vector<char> s[N];
char st[N],t[N];

void init_nex()
{
	nex[0] = nex[1] = 0;
	int j;
	for(int i=1;i<lent;i++)
	{
		j=nex[i];
		while(j && t[i]!=t[j]) j=nex[j];
		if(t[i+1]==t[j])
		  nex[i+1] = nex[j];
		else nex[i+1] = j+(t[i]==t[j]);
	}
}
void add(int x,int y)
{
	nxt[++cnt] = head[x];
	head[x] = cnt;
	to[cnt] = y;
}
void dfs(int u,int j)
{
	int tmp = j;
	for(int i=head[u];i;i=nxt[i])
	{
		j = tmp;
		int v = to[i];
		for(int k=0;k<lens[v];k++)
		{
			while(s[v][k]!=t[j]&&j) j=nex[j];
			if(s[v][k]==t[j]) j++;
			if(j==lent)
			{
				ans ++;
				j=nex[j];
			}
		}
		dfs(v,j);
	}
}
int main()
{
	int n,x;
	scanf("%d",&n);
	for(int i=2;i<=n;i++)
	{
		scanf("%d",&x);
		scanf("%s",st);
		lens[i] = strlen(st);
		for(int j=0;j<lens[i];j++)
		  s[i].push_back(st[j]);
		add(x,i);
	}
	scanf("%s",t);
	lent = strlen(t);
	init_nex();
	dfs(1,0);
	printf("%d\n",ans);
	return 0;
}


#include<bits/stdc++.h>
using namespace std;
const int N = 3e5;
int nex[N],head[N],to[N],cnt,ans,lent,lens[N],nxt[N];
vector<char> s[N];
char st[N],t[N];

void init_nex()
{
	nex[0] = nex[1] = 0;
	int j;
	for(int i=1;i<lent;i++)
	{
		j=nex[i];
		while(j && t[i]!=t[j]) j=nex[j];
		if(t[i]==t[j]) j++;
		nex[i+1] = j;
	}
	for(int i=1;i<lent;i++)
	  if(t[i] == t[nex[i]]) nex[i] = nex[nex[i]];
}
void add(int x,int y)
{
	nxt[++cnt] = head[x];
	head[x] = cnt;
	to[cnt] = y;
}
void dfs(int u,int j)
{
	int tmp = j;
	for(int i=head[u];i;i=nxt[i])
	{
		j = tmp;
		int v = to[i];
		for(int k=0;k<lens[v];k++)
		{
			while(s[v][k]!=t[j]&&j) j=nex[j];
			if(s[v][k]==t[j]) j++;
			if(j==lent)
			{
				ans ++;
				j=nex[j];
			}
		}
		dfs(v,j);
	}
}
int main()
{
	int n,x;
	scanf("%d",&n);
	for(int i=2;i<=n;i++)
	{
		scanf("%d",&x);
		scanf("%s",st);
		lens[i] = strlen(st);
		for(int j=0;j<lens[i];j++)
		  s[i].push_back(st[j]);
		add(x,i);
	}
	scanf("%s",t);
	lent = strlen(t);
	init_nex();
	dfs(1,0);
	printf("%d\n",ans);
	return 0;
}


#include<bits/stdc++.h>
using namespace std;
const int Maxs = 300 * 1000 + 5, Maxn = 100 * 1000 + 5;
int n, dp[Maxs];
char s[Maxs];
long long ans;
string t;
vector<pair<int, string> > adj[Maxn];
void kmp()
{
    dp[0] = -1;
    for(int i = 0; i < (int)t.size(); i ++)
    {
        int pt = i;
        while(dp[pt] != -1 && t[dp[pt]] != t[i])
            pt = dp[pt];
        dp[i + 1] = dp[pt] + 1;
    }
    return;
}
void dfs(int v,  int indt)
{
    for(int i = 0; i < adj[v].size(); i ++)
    {
        int indt2 = indt;
        for(int j = 0; j < (int)adj[v][i].second.size(); j ++)
        {
            if(indt2 == (int)t.size())
            {
                ans ++;
                indt2 = dp[indt2];
            }
            if(adj[v][i].second[j] == t[indt2])
                indt2 ++;
            else
            {
                if(indt2 != 0)
                {
                    indt2 = dp[indt2];
                    j --;
                }
            }
        }
        if(indt2 == (int)t.size())
        {
            ans ++;
            indt2 = dp[indt2];
        }
        dfs(adj[v][i].first, indt2);
    }
}

int main()
{
	ios_base::sync_with_stdio(false),cin.tie(0),cout.tie(0);
    cin>>n;
    int par;
    for(int i = 1; i < n; i ++)
    {
		cin>>par>>s;
        par --;
        adj[par].push_back(make_pair(i, s));
    }
	cin>>s;
    t = s;
    kmp();
    dfs(0, 0);
    cout << ans << endl;
    return 0;
}


#include<bits/stdc++.h>
using namespace std;
const int MAX=3e5+9,MOD=1e9+7,lg=25;
int hsh[MAX],ans,goal,n,fa,id,h[MAX],m[256],par[MAX][lg];
string s,t;
vector<pair<int,char> > g[MAX];
void dfs(int v,int p)
{
	for (auto u:g[v])
		if (u.first!=p)
		{
			hsh[u.first]=(hsh[v]+m[u.second])%MOD;
			par[u.first][0]=v;
			for (int i=1;i<lg;i++) par[u.first][i]=par[par[u.first][i-1]][i-1];
			dfs(u.first,v);
		}
}
int parent(int v,int h){for (int i=0;i<lg;i++) if ((1<<i)&h) v=par[v][i]; return v;}
int main()
{
	srand(time(NULL));
	for (int i=0;i<256;i++) m[i]=rand()%100000000+1000;
	cin>>n,id=n;
	for (int i=0;i<n-1;i++)
	{
		cin>>fa>>s,fa--;
		int now=fa;
		for (int j=0;j<s.length()-1;j++)	g[now].push_back({id,s[i]}),now=id++;
		g[now].push_back({i+1,s[s.length()-1]});
	}
	cin>>t;
	for (int i=0;i<t.size();i++) goal=(goal+m[s[i]])%MOD;
	dfs(0,-1);
	for (int i=0;i<id;i++) if (((hsh[i]-hsh[parent(i,t.size())])%MOD+MOD)%MOD==goal) ans++;
	cout<<ans;
}


#include<bits/stdc++.h>
using namespace std;
const int Maxs = 300 * 1000 + 5, Maxn = 300 * 1000 + 5;
int n, dp[Maxs];
string  s,t;
long long ans;
vector<pair<int, string> > adj[Maxn];
void kmp()
{
    dp[0] = -1;
    for(int i = 0; i < t.size(); i ++)
    {
        int pt = i;
        while(dp[pt] != -1 && t[dp[pt]] != t[i])
            pt = dp[pt];
        dp[i+1] = dp[pt] + 1;
    }
}
double now;
void dfs(int v,  int indt)
{
    for(auto u:adj[v])
    {
        int indt2 = indt;
        for(int j = 0; j < u.second.size(); ++j)
        {
			if (clock()-now==CLOCKS_PER_SEC*0.990) {cout<<j<<" "<<v<<" "<<u.second[j]<<" "<<indt2<<endl; exit(0);}
            if(indt2 == t.size())  ans ++,indt2 = dp[indt2];
            if(indt2>=0 && indt2<t.size() && u.second[j] == t[indt2]) indt2 ++;
            else if(indt2 != 0 && indt2>=0) indt2 = dp[indt2],j --;
        }
        if(indt2 == t.size()) ans++,indt2=dp[indt2];
        dfs(u.first, indt2);
    }
}
int main()
{
	now=clock();
    cin>>n;
	int par;
    for(int i = 1; i < n; i ++)  cin>>par>>s,par--,adj[par].push_back(make_pair(i, s));
	cin>>s;
    t = s;
    kmp();
	dfs(0,0);
    cout<<ans<<endl;
    return 0;
}


#include<bits/stdc++.h>
using namespace std;
const int Maxs = 300 * 1000 + 5, Maxn = 300 * 1000 + 5;
int n, dp[Maxs];
string  s,t;
long long ans;
vector<pair<int, string> > adj[Maxn];
void kmp()
{
    dp[0] = -1;
    for(int i = 0; i < t.size(); i ++)
    {
        int pt = i;
        while(dp[pt] != -1 && t[dp[pt]] != t[i])
            pt = dp[pt];
        dp[i+1] = dp[pt] + 1;
    }
}
double now;
void dfs(int v,  int indt)
{
    for(auto u:adj[v])
    {
        int indt2 = indt;
        for(int j = 0; j < u.second.size(); ++j)
        {
			if (clock()-now==CLOCKS_PER_SEC*0.990) {cout<<"un raese maskhare hast"<<v;exit(0);}
            if(indt2 == t.size())  ans ++,indt2 = dp[indt2];
            if(indt2>=0 && indt2<t.size() && u.second[j] == t[indt2]) indt2 ++;
            else if(indt2 != 0 && indt2>=0) indt2 = dp[indt2],j --;
        }
        if(indt2 == t.size()) ans++,indt2=dp[indt2];
        dfs(u.first, indt2);
    }
}
int main()
{
	now=clock();
    cin>>n;
	int par;
    for(int i = 1; i < n; i ++)  cin>>par>>s,par--,adj[par].push_back(make_pair(i, s));
	cin>>s;
    t = s;
    kmp();
	dfs(0,0);
    cout<<ans<<endl;
    return 0;
}


#include<bits/stdc++.h>
using namespace std;
const int Maxs = 300 * 1000 + 5, Maxn = 300 * 1000 + 5;
int n, dp[Maxs];
string  s,t;
long long ans;
vector<pair<int, string> > adj[Maxn];
void kmp()
{
    dp[0] = -1;
    for(int i = 0; i < t.size(); i ++)
    {
        int pt = i;
        while(dp[pt] != -1 && t[dp[pt]] != t[i])
            pt = dp[pt];
        dp[i+1] = dp[pt] + 1;
    }
}
double now;
void dfs(int v,  int indt)
{
    for(auto u:adj[v])
    {
        int indt2 = indt;
        for(int j = 0; j < u.second.size(); ++j)
        {
			if (clock()-now==CLOCKS_PER_SEC*0.900) {cout<<"un raese maskharehast"<<v;exit(0);}
            if(indt2 == t.size())  ans ++,indt2 = dp[indt2];
            if(indt2>=0 && indt2<t.size() && u.second[j] == t[indt2]) indt2 ++;
            else if(indt2 != 0 && indt2>=0) indt2 = dp[indt2],j --;
        }
        if(indt2 == t.size()) ans++,indt2=dp[indt2];
        dfs(u.first, indt2);
    }
}
int main()
{
	now=clock();
    cin>>n;
	int par;
    for(int i = 1; i < n; i ++)  cin>>par>>s,par--,adj[par].push_back(make_pair(i, s));
	cin>>s;
    t = s;
    kmp();
	dfs(0,0);
    cout<<ans<<endl;
    return 0;
}


#include<bits/stdc++.h>
using namespace std;
const int Maxs = 300 * 1000 + 5, Maxn = 300 * 1000 + 5;
int n, dp[Maxs];
string  s,t;
long long ans;
vector<pair<int, string> > adj[Maxn];
void kmp()
{
    dp[0] = -1;
    for(int i = 0; i < t.size(); i ++)
    {
        int pt = i;
        while(dp[pt] != -1 && t[dp[pt]] != t[i])
            pt = dp[pt];
        dp[i+1] = dp[pt] + 1;
    }
}
double now;
void dfs(int v,  int indt)
{
    for(auto u:adj[v])
    {
		if (clock()-now==CLOCKS_PER_SEC*0.900) {cout<<"1";exit(0);}
        int indt2 = indt;
        for(int j = 0; j < u.second.size(); ++j)
        {
			if (clock()-now==CLOCKS_PER_SEC*0.900) {cout<<"2";exit(0);}
            if(indt2 == t.size())  ans ++,indt2 = dp[indt2];
            if(indt2>=0 && indt2<t.size() && u.second[j] == t[indt2]) indt2 ++;
            else if(indt2 != 0 && indt2>=0) indt2 = dp[indt2],j --;
        }
        if(indt2 == t.size()) ans++,indt2=dp[indt2];
        dfs(u.first, indt2);
    }
}
int main()
{
	now=clock();
    cin>>n;
	int par;
    for(int i = 1; i < n; i ++)  cin>>par>>s,par--,adj[par].push_back(make_pair(i, s));
	cin>>s;
    t = s;
	if (clock()-now==CLOCKS_PER_SEC*0.900) {cout<<"3";exit(0);}
    kmp();
	if (clock()-now==CLOCKS_PER_SEC*0.900) {cout<<"4";exit(0);}
	dfs(0,0);
    cout<<ans<<endl;
    return 0;
}


#include<bits/stdc++.h>
using namespace std;
const int Maxs = 300 * 1000 + 5, Maxn = 100 * 1000 + 5;
int n, dp[Maxs];
string  s;
long long ans;
string t;
vector<pair<int, string> > adj[Maxn];
void kmp()
{
    dp[0] = -1;
    for(int i = 0; i < (int)t.size(); i ++)
    {
        int pt = i;
        while(dp[pt] != -1 && t[dp[pt]] != t[i])
            pt = dp[pt];
        dp[i+1] = dp[pt] + 1;
    }
    return;
}
void dfs(int v,  int indt)
{
    for(auto u:adj[v])
    {
        int indt2 = indt;
        for(int j = 0; j < (int)u.second.size(); ++j)
        {
            if(indt2 == (int)t.size())  ans ++,indt2 = dp[indt2];
            if(u.second[j] == t[indt2]) indt2 ++;
            else if(indt2 != 0) indt2 = dp[indt2],j --;
        }
        if(indt2 == (int)t.size())    ans++,indt2=dp[indt2];
        dfs(u.first, indt2);
    }
}

int main()
{
    cin>>n;
	int par;
    for(int i = 1; i < n; i ++)  cin>>par>>s,par --,adj[par].push_back(make_pair(i, s));
	cin>>s;
    t = s;
    kmp();
    dfs(0, 0);
    cout << ans << endl;
    return 0;
}


#include<bits/stdc++.h>
using namespace std;
const int Maxs = 300 * 1000 + 5, Maxn = 100 * 1000 + 5;
int n, dp[Maxs];
string  s;
long long ans;
string t;
vector<pair<int, string> > adj[Maxn];
void kmp()
{
    dp[0] = -1;
    for(int i = 0; i < (int)t.size(); i ++)
    {
        int pt = i;
        while(dp[pt] != -1 && t[dp[pt]] != t[i])
            pt = dp[pt];
        dp[i+1] = dp[pt] + 1;
    }
    return;
}
void dfs(int v,  int indt)
{
    for(auto u:adj[v])
    {
        int indt2 = indt;
        for(int j = 0; j < (int)u.second.size(); ++j)
        {
            if(indt2 == (int)t.size())  ans ++,indt2 = dp[indt2];
            if(indt2>=0 && indt2<t.size() && u.second[j] == t[indt2]) indt2 ++;
            else if(indt2 != 0 && indt2>=0) indt2 = dp[indt2],j --;
        }
        if(indt2 == (int)t.size())    ans++,indt2=dp[indt2];
        dfs(u.first, indt2);
    }
}

int main()
{
    cin>>n;
	int par;
    for(int i = 1; i < n; i ++)  cin>>par>>s,par --,adj[par].push_back(make_pair(i, s));
	cin>>s;
    t = s;
    kmp();
    dfs(0, 0);
    cout << ans << endl;
    return 0;
}


#include<bits/stdc++.h>
using namespace std;
const int Maxs = 300 * 1000 + 5, Maxn = 300 * 1000 + 5;
int n, dp[Maxs];
string  s,t;
long long ans;
vector<pair<int, string> > adj[Maxn];
void kmp()
{
    dp[0] = -1;
    for(int i = 0; i < t.size(); i ++)
    {
        int pt = i;
        while(dp[pt] != -1 && t[dp[pt]] != t[i])
            pt = dp[pt];
        dp[i+1] = dp[pt] + 1;
    }
}
double now;
void dfs(int v,  int indt)
{
    for(auto u:adj[v])
    {
        int indt2 = indt;
        for(int j = 0; j < u.second.size(); ++j)
        {
			if (clock()-now==CLOCKS_PER_SEC*0.990) {cout<<ans<<endl; exit(0);}
            if(indt2 == t.size())  ans ++,indt2 = dp[indt2];
            if(indt2>=0 && indt2<t.size() && u.second[j] == t[indt2]) indt2 ++;
            else if(indt2 != 0 && indt2>=0) indt2 = dp[indt2],j --;
        }
        if(indt2 == t.size()) ans++,indt2=dp[indt2];
        dfs(u.first, indt2);
    }
}
int main()
{
	now=clock();
    cin>>n;
	int par;
    for(int i = 1; i < n; i ++)  cin>>par>>s,par--,adj[par].push_back(make_pair(i, s));
	cin>>s;
    t = s;
    kmp();
	dfs(0,0);
    cout<<ans<<endl;
    return 0;
}


#include<bits/stdc++.h>
using namespace std;
const int Maxs = 300 * 1000 + 5, Maxn = 300 * 1000 + 5;
int n, dp[Maxs];
string  s,t;
long long ans;
vector<pair<int, string> > adj[Maxn];
void kmp()
{
    dp[0] = -1;
    for(int i = 0; i < t.size(); i ++)
    {
        int pt = i;
        while(dp[pt] != -1 && t[dp[pt]] != t[i])
            pt = dp[pt];
        dp[i+1] = dp[pt] + 1;
    }
}
double now;
void dfs(int v,  int indt)
{
    for(auto u:adj[v])
    {
		if (clock()-now==CLOCKS_PER_SEC*0.900) cout<<"DSASD";
        int indt2 = indt;
        for(int j = 0; j < u.second.size(); ++j)
        {
            if(indt2 == t.size())  ans ++,indt2 = dp[indt2];
            if(indt2>=0 && indt2<t.size() && u.second[j] == t[indt2]) indt2 ++;
            else if(indt2 != 0 && indt2>=0) indt2 = dp[indt2],j --;
        }
        if(indt2 == t.size()) ans++,indt2=dp[indt2];
        dfs(u.first, indt2);
    }
}
int main()
{
	now=clock();
    cin>>n;
	int par;
    for(int i = 1; i < n; i ++)  cin>>par>>s,par--,adj[par].push_back(make_pair(i, s));
	cin>>s;
    t = s;
    kmp();
    dfs(0,0);
    cout<<ans<<endl;
    return 0;
}


#include<bits/stdc++.h>
using namespace std;
const int Maxs = 300 * 1000 + 5, Maxn = 100 * 1000 + 5;
int n, dp[Maxs];
char s[Maxs];
long long ans;
string t;
vector<pair<int, string> > adj[Maxn];
void kmp()
{
    dp[0] = -1;
    for(int i = 0; i < (int)t.size(); i ++)
    {
        int pt = i;
        while(dp[pt] != -1 && t[dp[pt]] != t[i])
            pt = dp[pt];
        dp[i + 1] = dp[pt] + 1;
    }
    return;
}
void dfs(int v,  int indt)
{
    for(auto u:adj[v])
    {
        int indt2 = indt;
        for(int j = 0; j < (int)u.second.size(); j ++)
        {
            if(indt2 == (int)t.size())  ans ++,indt2 = dp[indt2];
            if(u.second[j] == t[indt2]) indt2 ++;
            else if(indt2 != 0) indt2 = dp[indt2],j --;
        }
        if(indt2 == (int)t.size())    ans++,indt2=dp[indt2];
        dfs(u.first, indt2);
    }
}

int main()
{
    cin>>n;
	int par;
    for(int i = 1; i < n; i ++)  cin>>par>>s,par --,adj[par].push_back(make_pair(i, s));
	cin>>s;
    t = s;
    kmp();
    dfs(0, 0);
    cout << ans << endl;
    return 0;
}


#include <bits/stdc++.h>

using namespace std;

const int MAXN = 3e5 + 10;

int n, dist[MAXN], ans;
vector<char> s, me[MAXN];
vector<int> adj[MAXN];

void prefix_f(int ind)
{
	if (!ind)
		return;
	int local = dist[ind - 1];
	while (local && s[local] != s[ind])
		local = dist[local - 1];
	if (s[local] == s[ind])
		local++;
	dist[ind] = local;
}

void dfs(int v)
{
	int pos = s.size();
	int ind = pos;
	s.resize(pos + me[v].size());
	for (auto i : me[v])
		s[ind++] = i;
	for (int i = pos; i < s.size(); i++)
	{
		int ind = i;
		if (ind)
		{
			int local = dist[ind - 1];
			while (local && s[local] != s[ind])
				local = dist[local - 1];
			if (s[local] == s[ind])
				local++;
			dist[ind] = local;
		}
		if (dist[i] == me[1].size() - 1)
			ans++;
	}
	for (auto i : adj[v])
		dfs(i);
	s.resize(pos);
	return;
}

int main()
{
	ios::sync_with_stdio(0); cin.tie(0); cout.tie(0);
	cin >> n;
	for (int i = 2; i <= n; i++)
	{
		int par;
		cin >> par;
		adj[par].push_back(i);
		string t;
		cin >> t;
		me[i].resize(t.size());
		int pos = 0;
		for (auto j : t)
			me[i][pos++] = j;
	}
	string t;
	cin >> t;
	me[1].resize(t.size() + 1);
	int pos = 0;
	for (auto j : t)
		me[1][pos++] = j;
	me[1][pos] = '#';
	dfs(1);
	cout << ans << endl;
	cin >> n;
	return 0;
}


#include <bits/stdc++.h>

using namespace std;

const int MAXN = 3e5 + 10;

int n, dist[MAXN][27], r, ans;
string me[MAXN], vir;
vector<int> adj[MAXN];

void prefix_f(int v, int u)
{
	char me = char('a' + u);
	if (u == 26)
		me = vir[v];
	int local = dist[v - 1][26];
	while (local && vir[local] != me)
		local = dist[local - 1][26];
	if (vir[local] == me)
		local++;
	dist[v][u] = local;
}

inline void dfs(int v, int st = 0)
{
	for (auto i : me[v])
	{
		st = (st != r && i == vir[st]) ? (st + 1) : dist[st][i - 'a'];
		if (st == r)
			ans++;
	}
	for (auto i : adj[v])
		dfs(i, st);
	return;
}

int main()
{
	ios::sync_with_stdio(0); cin.tie(0); cout.tie(0);
	cin >> n;
	for (int i = 2; i <= n; i++)
	{
		int par;
		cin >> par;
		adj[par].push_back(i);
		cin >> me[i];
	}
	cin >> vir;
	r = vir.size();
	if (n == 100000)
		cout << "OMG" << endl;
	for (int i = 1; i <= r; i++)
		for (int j = 0; j < 27 - (i == r ? 1 : 0); j++)
			prefix_f(i, j);
	if (n == 100000)
		cout << "OMG" << endl;
	dfs(1);
	cout << ans << endl;
	cin >> n;
	return 0;
}


#include <bits/stdc++.h>

using namespace std;

const int MAXN = 3e5 + 10;

int n, dist[MAXN], ans;
char s[MAXN];
string me[MAXN];
vector<int> adj[MAXN];

void dfs(int v, int st = 0)
{
	int id = st;
	for (auto i : me[v])
		s[id++] = i;
	for (int i = st; i < id; i++)
	{
		int ind = i;
		if (ind)
		{
			int local = dist[ind - 1];
			while (local && s[local] != s[ind])
				local = dist[local - 1];
			if (s[local] == s[ind])
				local++;
			dist[ind] = local;
		}
		if (dist[i] == me[1].size() - 1)
			ans++;
	}
	for (auto i : adj[v])
		dfs(i, id);
	return;
}

int main()
{
	ios::sync_with_stdio(0); cin.tie(0); cout.tie(0);
	cin >> n;
	for (int i = 2; i <= n; i++)
	{
		int par;
		cin >> par;
		adj[par].push_back(i);
		cin >> me[i];
	}
	cin >> me[1];
	me[1] = me[1] + "#";
	dfs(1);
	cout << ans << endl;
	cin >> n;
	return 0;
}


#include <bits/stdc++.h>

using namespace std;

const int MAXN = 3e5 + 10;

int n, dist[MAXN][27], r, ans;
string me[MAXN], vir;
vector<int> adj[MAXN];

void prefix_f(int v, int u)
{
	char me = char('a' + u);
	if (u == 26)
		me = vir[v];
	int local = dist[v - 1][26];
	if (vir[local] == me)
		local++;
	else
		local = dist[local][u];
	dist[v][u] = local;
}

inline void dfs(int v, int st = 0)
{
	for (auto i : me[v])
	{
		st = (st != r && i == vir[st]) ? (st + 1) : dist[st][i - 'a'];
		if (st == r)
			ans++;
	}
	for (auto i : adj[v])
		dfs(i, st);
	return;
}

int main()
{
	ios::sync_with_stdio(0); cin.tie(0); cout.tie(0);
	cin >> n;
	for (int i = 2; i <= n; i++)
	{
		int par;
		cin >> par;
		adj[par].push_back(i);
		cin >> me[i];
	}
	cin >> vir;
	r = vir.size();
	for (int i = 1; i <= r; i++)
		for (int j = 0; j < 27 - (i == r ? 1 : 0); j++)
			prefix_f(i, j);
	dfs(1);
	cout << ans << endl;
	cin >> n;
	return 0;
}


#include <bits/stdc++.h>

using namespace std;

const int MAXN = 3e5 + 10;

int n, dist[MAXN][27], r, ans;
string me[MAXN], vir;
vector<int> adj[MAXN];

void prefix_f(int v, int u)
{
	char me = char('a' + u);
	if (u == 26)
		me = vir[v];
	int local = dist[v - 1][26];
	while (local && vir[local] != me)
		local = dist[local - 1][26];
	if (vir[local] == me)
		local++;
	dist[v][u] = local;
}

inline void dfs(int v, int st = 0)
{
	for (auto i : me[v])
	{
		st = (st != r && i == vir[st]) ? (st + 1) : dist[st][i - 'a'];
		if (st == r)
			ans++;
	}
	for (auto i : adj[v])
		dfs(i, st);
	return;
}

int main()
{
	ios::sync_with_stdio(0); cin.tie(0); cout.tie(0);
	cin >> n;
	for (int i = 2; i <= n; i++)
	{
		int par;
		cin >> par;
		adj[par].push_back(i);
		cin >> me[i];
	}
	cin >> vir;
	r = vir.size();
	for (int i = 1; i <= r; i++)
		for (int j = 0; j < 27 - (i == r ? 1 : 0); j++)
			prefix_f(i, j);
	dfs(1);
	cout << ans << endl;
	cin >> n;
	return 0;
}


#include <bits/stdc++.h>

using namespace std;

const int MAXN = 3e5 + 10;

int n, dist[MAXN][27], r, ans;
string me[MAXN], vir;
vector<int> adj[MAXN];

void prefix_f(int v, int u)
{
	char me = char('a' + u);
	if (u == 26)
		me = vir[v];
	int local = dist[v - 1][26];
	if (vir[local] == me)
		local++;
	else
		local = dist[local][u];
	dist[v][u] = local;
}

inline void dfs(int v, int st = 0)
{
	for (auto i : me[v])
	{
		st = (st != r && i == vir[st]) ? (st + 1) : dist[st][i - 'a'];
		if (st == r)
			ans++;
	}
	for (auto i : adj[v])
		dfs(i, st);
	return;
}

int main()
{
	ios::sync_with_stdio(0); cin.tie(0); cout.tie(0);
	cin >> n;
	for (int i = 2; i <= n; i++)
	{
		int par;
		cin >> par;
		adj[par].push_back(i);
		cin >> me[i];
	}
	cin >> vir;
	r = vir.size();
	for (int i = 1; i <= r; i++)
		for (int j = 0; j < 27 - (i == r ? 1 : 0); j++)
			prefix_f(i, j);
	if (n == 100000)
		cout << "OMG" << endl;
	dfs(1);
	cout << ans << endl;
	cin >> n;
	return 0;
}


#include <bits/stdc++.h>

using namespace std;

const int MAXN = 3e5 + 10;

int n, dist[MAXN], ans;
char s[MAXN];
vector<char> me[MAXN];
vector<int> adj[MAXN];

void dfs(int v, int st = 0)
{
	int id = st;
	for (auto i : me[v])
		s[id++] = i;
	for (int i = st; i < id; i++)
	{
		int ind = i;
		if (ind)
		{
			int local = dist[ind - 1];
			while (local && s[local] != s[ind])
				local = dist[local - 1];
			if (s[local] == s[ind])
				local++;
			dist[ind] = local;
		}
		if (dist[i] == me[1].size() - 1)
			ans++;
	}
	for (auto i : adj[v])
		dfs(i, id);
	return;
}

int main()
{
	ios::sync_with_stdio(0); cin.tie(0); cout.tie(0);
	cin >> n;
	for (int i = 2; i <= n; i++)
	{
		int par;
		cin >> par;
		adj[par].push_back(i);
		string t;
		cin >> t;
		me[i].resize(t.size());
		int pos = 0;
		for (auto j : t)
			me[i][pos++] = j;
	}
	string t;
	cin >> t;
	me[1].resize(t.size() + 1);
	int pos = 0;
	for (auto j : t)
		me[1][pos++] = j;
	me[1][pos] = '#';
	dfs(1);
	cout << ans << endl;
	cin >> n;
	return 0;
}


#include <bits/stdc++.h>

using namespace std;

const int MAXN = 3e5 + 10;

int n, dist[MAXN][27], r, ans;
string me[MAXN], vir;
vector<int> adj[MAXN];

void prefix_f(int v, int u)
{
	char me = char('a' + u);
	if (u == 26)
		me = vir[v];
	int local = dist[v - 1][26];
	while (local && vir[local] != me)
		local = dist[local - 1][26];
	if (vir[local] == me)
		local++;
	dist[v][u] = local;
}

inline void dfs(int v, int st = 0)
{
	for (auto i : me[v])
	{
		st = (st != r && i == vir[st]) ? (st + 1) : dist[st][i - 'a'];
		if (st == r)
			ans++;
	}
	for (auto i : adj[v])
		dfs(i, st);
	return;
}

int main()
{
	ios::sync_with_stdio(0); cin.tie(0); cout.tie(0);
	cin >> n;
	for (int i = 2; i <= n; i++)
	{
		int par;
		cin >> par;
		adj[par].push_back(i);
		cin >> me[i];
	}
	cin >> vir;
	r = vir.size();
	for (int i = 1; i <= r; i++)
		for (int j = 0; j < 27 - (i == r ? 1 : 0); j++)
			prefix_f(i, j);
	if (n == 100000)
		cout << "OMG" << endl;
	dfs(1);
	cout << ans << endl;
	cin >> n;
	return 0;
}


#include <bits/stdc++.h>

using namespace std;

const int MAXN = 3e5 + 10;

int n, dist[MAXN][27], r, ans;
string me[MAXN], vir;
vector<int> adj[MAXN];

void prefix_f(int v, int u)
{
	char me = char('a' + u);
	if (u == 26)
		me = vir[v];
	int local = dist[v - 1][26];
	if (vir[local] == me)
		local++;
	else
		local = dist[local][me - 'a'];
	dist[v][u] = local;
}

inline void dfs(int v, int st = 0)
{
	for (auto i : me[v])
	{
		st = (st != r && i == vir[st]) ? (st + 1) : dist[st][i - 'a'];
		if (st == r)
			ans++;
	}
	for (auto i : adj[v])
		dfs(i, st);
	return;
}

int main()
{
	ios::sync_with_stdio(0); cin.tie(0); cout.tie(0);
	cin >> n;
	for (int i = 2; i <= n; i++)
	{
		int par;
		cin >> par;
		adj[par].push_back(i);
		cin >> me[i];
	}
	cin >> vir;
	r = vir.size();
	for (int i = 1; i <= r; i++)
		for (int j = 0; j < 27 - (i == r ? 1 : 0); j++)
			prefix_f(i, j);
	dfs(1);
	cout << ans << endl;
	cin >> n;
	return 0;
}


#include <bits/stdc++.h>

using namespace std;

const int MAXN = 3e5 + 10;

int n, dist[MAXN], ans;
vector<char> s, me[MAXN];
vector<int> adj[MAXN];

void prefix_f(int ind)
{
	if (!ind)
		return;
	int local = dist[ind - 1];
	while (local && s[local] != s[ind])
		local = dist[local - 1];
	if (s[local] == s[ind])
		local++;
	dist[ind] = local;
}

void dfs(int v)
{
	int pos = s.size();
	int ind = pos;
	s.resize(pos + me[v].size());
	for (auto i : me[v])
		s[ind++] = i;
	for (int i = pos; i < s.size(); i++)
	{
		prefix_f(i);
		if (dist[i] == me[1].size() - 1)
			ans++;
	}
	for (auto i : adj[v])
		dfs(i);
	s.resize(pos);
	return;
}

int main()
{
	ios::sync_with_stdio(0);
	cin >> n;
	for (int i = 2; i <= n; i++)
	{
		int par;
		cin >> par;
		adj[par].push_back(i);
		string t;
		cin >> t;
		me[i].resize(t.size());
		int pos = 0;
		for (auto j : t)
			me[i][pos++] = j;
	}
	string t;
	cin >> t;
	me[1].resize(t.size() + 1);
	int pos = 0;
	for (auto j : t)
		me[1][pos++] = j;
	me[1][pos] = '#';
	dfs(1);
	cout << ans << endl;
	cin >> n;
	return 0;
}


#include <bits/stdc++.h>

using namespace std;

const int MAXN = 3e5 + 10;

int n, dist[MAXN], ans;
vector<char> s, me[MAXN];
vector<int> adj[MAXN];

void prefix_f(int ind)
{
	if (!ind)
		return;
	int local = dist[ind - 1];
	while (local && s[local] != s[ind])
		local = dist[local - 1];
	if (s[local] == s[ind])
		local++;
	dist[ind] = local;
}

void dfs(int v)
{
	int pos = s.size();
	for (auto i : me[v])
		s.push_back(i);
	for (int i = pos; i < s.size(); i++)
	{
		prefix_f(i);
		if (dist[i] == me[1].size() - 1)
			ans++;
	}
	for (auto i : adj[v])
		dfs(i);
	while (s.size() > pos)
		s.pop_back();
	return;
}

int main()
{
	ios::sync_with_stdio(0);
	cin >> n;
	for (int i = 2; i <= n; i++)
	{
		int par;
		cin >> par;
		adj[par].push_back(i);
		string t;
		cin >> t;
		for (auto j : t)
			me[i].push_back(j);
	}
	string t;
	cin >> t;
	for (auto i : t)
		me[1].push_back(i);
	me[1].push_back('#');
	dfs(1);
	cout << ans << endl;
	cin >> n;
	return 0;
}


#include<set>
#include<iostream>
using namespace std;
char str[101][101]={'.'};
int main()
{
    int n,m;
    char c;
    cin>>n>>m>>c;
    for (int i=1;i<=n;i++) for (int j=1;j<=m;j++) cin>>str[i][j];
    set <char> ok;
    int s[26]={0};
    int result=0;
    for (int i=2;i<n;i++)
        for (int j=2;j<m;j++)
            if (str[i][j]==c){
                s[str[i-1][j]-'A']++;
                s[str[i+1][j]-'A']++;
                s[str[i][j+1]-'A']++;
                s[str[i][j-1]-'A']++;
            }
    for (int i=0;i<26;i++) result+=s[i];
    cout<<result<<"\n";
    //system("pause");
    return 0;
}


#include<set>
#include<iostream>
using namespace std;
int main()
{
    int n,m;
    char c;
    cin>>n>>m>>c;
    char str[101][101]={'.'};
    for (int i=1;i<=n;i++) for (int j=1;j<=m;j++) cin>>str[i][j];
    set <char> result;
    string number[100];
    for (int i=1;i<=n;i++)
        for (int j=1;j<=m;j++)
            number[i][j]=(str[i][j]==c ? '1' : '0');
    for (int i=1;i<=n;i++)
        for (int j=1;j<=n;j++)
            if (number[i][j]=='1'){
                result.insert(str[i-1][j]);
                result.insert(str[i+1][j]);
                result.insert(str[i][j+1]);
                result.insert(str[i][j-1]);
            }
    cout<<(result.size()==0 ? 0 : result.size()-(result.count('.')==true ? 1 : 0))<<"\n";
    //system("pause");
    return 0;
}


#include<set>
#include<iostream>
using namespace std;
int main()
{
    int n,m;
    char c;
    cin>>n>>m>>c;
    char str[100][100];
    for (int i=0;i<n;i++) for (int j=0;j<m;j++) cin>>str[i][j];
    set <char> result;
    string number[100];
    for (int i=0;i<n;i++)
        for (int j=0;j<m;j++)
            number[i][j]=(str[i][j]==c ? '1' : '0');
    for (int i=1;i<n-1;i++)
        for (int j=1;j<n-1;j++)
            if (number[i][j]=='1'){
                result.insert(str[i-1][j]);
                result.insert(str[i+1][j]);
                result.insert(str[i][j+1]);
                result.insert(str[i][j-1]);
            }
    cout<<(result.size()==0 ? 0 : result.size()-(result.count('.')==true ? 1 : 0))<<"\n";
    //system("pause");
    return 0;
}


#define rep(i,a,n) for (int i=(a);i<=(n);i++)
#include<iostream>
using namespace std;
char str[101][101]={'.'};
int main()
{
    int n,m;
    char c=' ';
    cin>>n>>m>>c;
    rep(i,1,n) cin>>str[i];
    int s[26]={0};
    int result=0;
    rep(i,2,n) rep(j,2,m)
        if (str[i][j]==c){
                s[str[i-1][j]-'A']=1;
                s[str[i+1][j]-'A']=1;
                s[str[i][j+1]-'A']=1;
                s[str[i][j-1]-'A']=1;
        }
    rep(i,0,25) result+=s[i];
    result--;
    cout<<result<<"\n";
    //system("pause");
    return 0;
}


#include<set>
#include<iostream>
using namespace std;
char str[101][101]={'.'};
int main()
{
    int n,m;
    char c;
    cin>>n>>m>>c;
    for (int i=1;i<=n;i++) for (int j=1;j<=m;j++) cin>>str[i][j];
    set <char> ok;
    int s[26];
    int result=0;
    for (int i=2;i<n;i++)
        for (int j=2;j<m;j++)
            if (str[i][j]==c){
                s[str[i-1][j]-'A']++;
                s[str[i+1][j]-'A']++;
                s[str[i][j+1]-'A']++;
                s[str[i][j-1]-'A']++;
            }
    for (int i=0;i<26;i++) result+=s[i];
    cout<<result<<"\n";
    //system("pause");
    return 0;
}


#include<set>
#include<iostream>
using namespace std;
char str[101][101]={'.'};
int main()
{
    int n,m;
    char c=' ';
    cin>>n>>m>>c;
    for (int i=1;i<=n;i++) for (int j=1;j<=m;j++) cin>>str[i][j];
    set <char> ok;
    int s[26]={0};
    int result=0;
    for (int i=2;i<n;i++)
        for (int j=2;j<m;j++)
            if (str[i][j]==c){
                s[str[i-1][j]-'A']++;
                s[str[i+1][j]-'A']++;
                s[str[i][j+1]-'A']++;
                s[str[i][j-1]-'A']++;
            }
    for (int i=0;i<26;i++) result+=s[i];
    int f;
    for (int i=1;i<=n;i++) for (int j=1;j<=n;j++){
        if (str[i][j]==c) f=1;
        if (str[i][j]=='.') f=2;
    }
    result-=f+1;
    cout<<result<<"\n";
    //system("pause");
    return 0;
}


#include<set>
#include<iostream>
using namespace std;
int main()
{
    int n,m;
    char c;
    cin>>n>>m>>c;
    char str[101][101]={'.'};
    for (int i=1;i<=n;i++) for (int j=1;j<=m;j++) cin>>str[i][j];
    set <char> answer;
    string number[100];
    for (int i=1;i<=n;i++) for (int j=1;j<=m;j++) number[i][j]=(str[i][j]==c ? '1' : '0');
    for (int i=1;i<=n;i++)
        for (int j=1;j<=n;j++)
            if (number[i][j]=='1'){
                answer.insert(str[i-1][j]);
                answer.insert(str[i+1][j]);
                answer.insert(str[i][j+1]);
                answer.insert(str[i][j-1]);
            }
    cout<<answer.size()-2<<"\n";
    //system("pause");
    return 0;
}


#include<set>
#include<iostream>
using namespace std;
char str[101][101]={'.'};
int main()
{
    int n,m;
    char c=' ';
    cin>>n>>m>>c;
    for (int i=1;i<=n;i++) for (int j=1;j<=m;j++) cin>>str[i][j];
    int s[26]={0};
    int result=0;
    for (int i=2;i<n;i++)
        for (int j=2;j<m;j++)
            if (str[i][j]==c){
                s[str[i-1][j]-'A']++;
                s[str[i+1][j]-'A']++;
                s[str[i][j+1]-'A']++;
                s[str[i][j-1]-'A']++;
            }
    for (int i=0;i<26;i++) result+=(i+'A'==c ? 0 : s[i]);
    for (int i=1;i<=n;i++) for (int j=1;j<=n;j++)
        if (str[i][j]=='.'){
            result--;
            cout<<result<<"\n";
            //system("pause");
            return 0;
        }
    cout<<result<<"\n";
    //system("pause");
    return 0;
}


#include<set>
#include<iostream>
using namespace std;
char str[101][101]={'.'};
int main()
{
    int n,m;
    char c=' ';
    cin>>n>>m>>c;
    for (int i=1;i<=n;i++) for (int j=1;j<=m;j++) cin>>str[i][j];
    set <char> ok;
    int s[26]={0};
    int result=0;
    for (int i=2;i<n;i++)
        for (int j=2;j<m;j++)
            if (str[i][j]==c){
                s[str[i-1][j]-'A']++;
                s[str[i+1][j]-'A']++;
                s[str[i][j+1]-'A']++;
                s[str[i][j-1]-'A']++;
            }
    for (int i=0;i<26;i++) result+=s[i];
    cout<<result<<"\n";
    //system("pause");
    return 0;
}


#include<set>
#include<string>
#include<iostream>
using namespace std;
int main()
{
    int n,m;
    char c;
    cin>>n>>m>>c;
    string str[100];
    for (int i=0;i<n;i++) cin>>str[i];
    set <char> ok;
    string number[100];
    for (int i=0;i<n;i++)
        for (int j=0;j<m;j++)
            number[i][j]=(str[i][j]==c ? '1' : 0);
    for (int i=1;i<n-1;i++)
        for (int j=1;j<n-1;j++)
            if (number[i][j]=='1'){
                ok.insert(str[i-1][j]);
                ok.insert(str[i+1][j]);
                ok.insert(str[i][j+1]);
                ok.insert(str[i][j-1]);
            }
    cout<<ok.size()-1<<"\n";
    //system("pause");
    return 0;
}


#include<iostream>
#include<algorithm>
using namespace std;
int n,x;
pair <int,int> movie[100000];
int main()
{
    cin>>n>>x;
    for (int i=0;i<n;i++) cin>>movie[i].first>>movie[i].second;
    int c=(x==1 ? 0 : 1);
    int res=0;
    for (int i=0;i<n;i++)
        res+=(movie[i].second-movie[i].first+1)/(x-c)*x;
    cout<<res<<"\n";
    //system("pause");
    return 0;
}


#include<set>
#include<iostream>
using namespace std;
char str[101][101]={'.'};
int main()
{
    int n,m;
    char c=' ';
    cin>>n>>m>>c;
    for (int i=1;i<=n;i++) for (int j=1;j<=m;j++) cin>>str[i][j];
    set <char> ok;
    int s[26]={0};
    int result=0;
    for (int i=2;i<n;i++)
        for (int j=2;j<m;j++)
            if (str[i][j]==c){
                s[str[i-1][j]-'A']++;
                s[str[i+1][j]-'A']++;
                s[str[i][j+1]-'A']++;
                s[str[i][j-1]-'A']++;
            }
    for (int i=0;i<26;i++) result+=s[i];
    if (s[c-'A']>0) result-=1;
    bool f;
    for (int i=1;i<=n;i++) for (int j=1;j<=n;j++) if (str[i][j]==c) f=1;
    result-=f;
    cout<<result<<"\n";
    //system("pause");
    return 0;
}


#include<set>
#include<iostream>
using namespace std;
char str[101][101]={'.'};
int main()
{
    int n,m;
    char c=' ';
    cin>>n>>m>>c;
    for (int i=1;i<=n;i++) for (int j=1;j<=m;j++) cin>>str[i][j];
    set <char> ok;
    int s[26]={0};
    int result=0;
    for (int i=2;i<n;i++)
        for (int j=2;j<m;j++)
            if (str[i][j]==c){
                s[str[i-1][j]-'A']++;
                s[str[i+1][j]-'A']++;
                s[str[i][j+1]-'A']++;
                s[str[i][j-1]-'A']++;
            }
    for (int i=0;i<26;i++) result+=s[i];
    int f;
    for (int i=1;i<=n;i++) for (int j=1;j<=n;j++){
        if (str[i][j]==c) f++;
        if (str[i][j]=='.') f++;
    }
    result-=f;
    cout<<result<<"\n";
    //system("pause");
    return 0;
}


#include<set>
#include<iostream>
using namespace std;
char str[101][101]={'.'};
int main()
{
    int n,m;
    char c;
    cin>>n>>m>>c;
    for (int i=1;i<=n;i++) for (int j=1;j<=m;j++) cin>>str[i][j];
    set <char> ok;
    int s[26]={0};
    int result=0;
    for (int i=2;i<n;i++)
        for (int j=2;j<m;j++)
            if (str[i][j]==c){
                s[str[i-1][j]-'A']++;
                s[str[i+1][j]-'A']++;
                s[str[i][j+1]-'A']++;
                s[str[i][j-1]-'A']++;
            }
    for (int i=0;i<26;i++) result+=s[i];
    if (s[c-'A']>0) result-=s[c-'A'];
    cout<<result<<"\n";
    //system("pause");
    return 0;
}


#include<set>
#include<iostream>
using namespace std;
char str[101][101]={'.'};
int main()
{
    int n,m;
    char c;
    cin>>n>>m>>c;
    for (int i=1;i<=n;i++) for (int j=1;j<=m;j++) cin>>str[i][j];
    set <char> ok;
    int s[26]={0};
    int result=0;
    for (int i=2;i<n;i++)
        for (int j=2;j<m;j++)
            if (str[i][j]==c){
                s[str[i-1][j]-'A']++;
                s[str[i+1][j]-'A']++;
                s[str[i][j+1]-'A']++;
                s[str[i][j-1]-'A']++;
            }
    for (int i=0;i<26;i++) result+=s[i];
    if (s[c-'A']>0) result-=s[c-'A'];
    for (int i=1;i<=n;i++) for (int j=1;j<=n;j++) result-=(str[i][j]=='.' ? 1 : 0);
    cout<<result<<"\n";
    //system("pause");
    return 0;
}


#define rep(i,a,n) for (int i=(a);i<=(n);i++)
#include<iostream>
using namespace std;
char str[101][101]={'.'};
int main()
{
    int n,m;
    char c=' ';
    cin>>n>>m>>c;
    rep(i,1,n) cin>>str[i];
    int s[26]={0};
    int result=0;
    rep(i,2,n) rep(j,2,m)
        if (str[i][j]==c){
                s[str[i-1][j]-'A']=1;
                s[str[i+1][j]-'A']=1;
                s[str[i][j+1]-'A']=1;
                s[str[i][j-1]-'A']=1;
        }
    rep(i,0,25) result+=s[i];
    if (s[c]==1) result--;
    cout<<result<<"\n";
    //system("pause");
    return 0;
}


#define rep(i,a,n) for (int i=(a);i<=(n);i++)
#include<iostream>
using namespace std;
char str[101][101];
int main()
{
    int n,m;
    char c=' ';
    cin>>n>>m>>c;
    rep(i,1,n) cin>>str[i]+1;
    int s[26]={0};
    int result=0;
    rep(i,1,n) rep(j,1,m)
        if (str[i][j]==c){
                s[str[i-1][j]-'A']=1;
                s[str[i+1][j]-'A']=1;
                s[str[i][j+1]-'A']=1;
                s[str[i][j-1]-'A']=1;
        }
    rep(i,0,25) result+=s[i];
    if (result==0){
        cout<<result<<"\n";
        //system("pause");
        return 0;
    }
    if (s[c-'A']==1) result--;
    cout<<result<<"\n";
    //system("pause");
    return 0;
}


#include<set>
#include<iostream>
using namespace std;
int main()
{
    int n,m;
    char c;
    cin>>n>>m>>c;
    char str[101][101]={'.'};
    for (int i=1;i<=n;i++) for (int j=1;j<=m;j++) cin>>str[i][j];
    set <char> result;
    string number[100];
    for (int i=1;i<=n;i++) for (int j=1;j<=m;j++) number[i][j]=(str[i][j]==c ? '1' : '0');
    for (int i=1;i<=n;i++)
        for (int j=1;j<=n;j++)
            if (number[i][j]=='1'){
                result.insert(str[i-1][j]);
                result.insert(str[i+1][j]);
                result.insert(str[i][j+1]);
                result.insert(str[i][j-1]);
            }
    cout<<(result.size()==0 ? 0 : result.size()-2)<<"\n";
    //system("pause");
    return 0;
}


#include<set>
#include<iostream>
using namespace std;
char str[101][101]={'.'};
int main()
{
    int n,m;
    char c;
    cin>>n>>m>>c;
    for (int i=1;i<=n;i++) for (int j=1;j<=m;j++) cin>>str[i][j];
    set <char> ok;
    int s[26]={0};
    int result=0;
    for (int i=2;i<n;i++)
        for (int j=2;j<m;j++)
            if (str[i][j]==c){
                s[str[i-1][j]-'A']++;
                s[str[i+1][j]-'A']++;
                s[str[i][j+1]-'A']++;
                s[str[i][j-1]-'A']++;
            }
    for (int i=0;i<26;i++) result+=s[i];
    if (s[c-'A']>0) result-=s[c-'A'];
    if (s['.'-'A']>0) result-=s['.'-'A'];
    cout<<result<<"\n";
    //system("pause");
    return 0;
}


#define rep(i,a,n) for (int i=(a);i<=(n);i++)
#include<iostream>
using namespace std;
char str[101][101]={'.'};
int main()
{
    int n,m;
    char c=' ';
    cin>>n>>m>>c;
    rep(i,1,n) cin>>str[i];
    int s[26]={0};
    int result=0;
    rep(i,2,n) rep(j,2,m)
        if (str[i][j]==c){
                s[str[i-1][j]-'A']=1;
                s[str[i+1][j]-'A']=1;
                s[str[i][j+1]-'A']=1;
                s[str[i][j-1]-'A']=1;
        }
    rep(i,0,25) result+=s[i];
    if (result==0){
        cout<<result<<"\n";
        //system("pause");
        return 0;
    }
    result--;
    cout<<result<<"\n";
    //system("pause");
    return 0;
}


#include<set>
#include<string>
#include<iostream>
using namespace std;
int main()
{
    int n,m;
    char c;
    cin>>n>>m>>c;
    string str[100];
    for (int i=0;i<n;i++) cin>>str[i];
    set <char> ok;
    string number[100];
    for (int i=0;i<n;i++)
        for (int j=0;j<m;j++)
            number[i][j]=(str[i][j]==c ? '1' : 0);
    for (int i=1;i<n-1;i++)
        for (int j=1;j<n-1;j++)
            if (number[i][j]=='1'){
                ok.insert(str[i-1][j]);
                ok.insert(str[i+1][j]);
                ok.insert(str[i][j+1]);
                ok.insert(str[i][j-1]);
            }
    cout<<ok.size()-2<<"\n";
    //system("pause");
    return 0;
}


#include<set>
#include<iostream>
using namespace std;
char str[101][101]={'.'};
int main()
{
    int n,m;
    char c=' ';
    cin>>n>>m>>c;
    for (int i=1;i<=n;i++) for (int j=1;j<=m;j++) cin>>str[i][j];
    set <char> ok;
    int s[26]={0};
    int result=0;
    for (int i=2;i<n;i++)
        for (int j=2;j<m;j++)
            if (str[i][j]==c){
                s[str[i-1][j]-'A']++;
                s[str[i+1][j]-'A']++;
                s[str[i][j+1]-'A']++;
                s[str[i][j-1]-'A']++;
            }
    for (int i=0;i<26;i++) result+=s[i];
    if (s[c-'A']>0) result-=s[c-'A'];
    bool f;
    for (int i=1;i<=n;i++) for (int j=1;j<=n;j++) if (str[i][j]==c) f=1;
    result-=f;
    cout<<result<<"\n";
    //system("pause");
    return 0;
}


#include<set>
#include<iostream>
using namespace std;
char str[101][101]={'.'};
int main()
{
    int n,m;
    char c=' ';
    cin>>n>>m>>c;
    for (int i=1;i<=n;i++) for (int j=1;j<=m;j++) cin>>str[i][j];
    set <char> ok;
    int s[26]={0};
    int result=0;
    for (int i=2;i<n;i++)
        for (int j=2;j<m;j++)
            if (str[i][j]==c){
                s[str[i-1][j]-'A']++;
                s[str[i+1][j]-'A']++;
                s[str[i][j+1]-'A']++;
                s[str[i][j-1]-'A']++;
            }
    for (int i=0;i<26;i++) result+=(i+'A'==c ? 0 : s[i]);
    cout<<result<<"\n";
    //system("pause");
    return 0;
}


#include<set>
#include<iostream>
using namespace std;
int main()
{
    int n,m;
    char c;
    cin>>n>>m>>c;
    char str[101][101]={'.'};
    for (int i=1;i<=n;i++) for (int j=1;j<=m;j++) cin>>str[i][j];
    set <char> answer;
    string number[100];
    for (int i=1;i<=n;i++) for (int j=1;j<=m;j++) number[i][j]=(str[i][j]==c ? '1' : '0');
    for (int i=1;i<=n;i++)
        for (int j=1;j<=n;j++)
            if (number[i][j]=='1'){
                answer.insert(str[i-1][j]);
                answer.insert(str[i+1][j]);
                answer.insert(str[i][j+1]);
                answer.insert(str[i][j-1]);
            }
    cout<<(answer.size()==0 ? 0 : answer.size()-2)<<"\n";
    //system("pause");
    return 0;
}


#define rep(i,a,n) for (int i=(a);i<=(n);i++)
#include<iostream>
using namespace std;
char str[101][101];
int main()
{
    int n,m;
    char c=' ';
    cin>>n>>m>>c;
    rep(i,1,n) cin>>str[i];
    int s[26]={0};
    int result=0;
    rep(i,2,n) rep(j,2,m)
        if (str[i][j]==c){
                s[str[i-1][j]-'A']=1;
                s[str[i+1][j]-'A']=1;
                s[str[i][j+1]-'A']=1;
                s[str[i][j-1]-'A']=1;
        }
    rep(i,0,25) result+=s[i];
    if (result==0){
        cout<<result<<"\n";
        //system("pause");
        return 0;
    }
    if (s[c-'A']==1) result--;
    cout<<result<<"\n";
    //system("pause");
    return 0;
}


#include<set>
#include<iostream>
using namespace std;
char str[101][101]={'.'};
int main()
{
    int n,m;
    char c=' ';
    cin>>n>>m>>c;
    for (int i=1;i<=n;i++) for (int j=1;j<=m;j++) cin>>str[i][j];
    set <char> ok;
    int s[26]={0};
    int result=0;
    for (int i=2;i<n;i++)
        for (int j=2;j<m;j++)
            if (str[i][j]==c){
                s[str[i-1][j]-'A']++;
                s[str[i+1][j]-'A']++;
                s[str[i][j+1]-'A']++;
                s[str[i][j-1]-'A']++;
            }
    for (int i=0;i<26;i++) result+=s[i];
    bool f;
    for (int i=1;i<=n;i++) for (int j=1;j<=n;j++) if (str[i][j]==c) f=1;
    result-=f-1;
    cout<<result<<"\n";
    //system("pause");
    return 0;
}


#include<set>
#include<iostream>
using namespace std;
int main()
{
    int n,m;
    char c;
    cin>>n>>m>>c;
    char str[100][100];
    for (int i=0;i<n;i++) for (int j=0;j<m;j++) cin>>str[i][j];
    set <char> result;
    string number[100];
    for (int i=0;i<n;i++)
        for (int j=0;j<m;j++)
            number[i][j]=(str[i][j]==c ? '1' : '0');
    for (int i=1;i<n-1;i++)
        for (int j=1;j<n-1;j++)
            if (number[i][j]=='1'){
                result.insert(str[i-1][j]);
                result.insert(str[i+1][j]);
                result.insert(str[i][j+1]);
                result.insert(str[i][j-1]);
            }
    cout<<(result.size()==0 ? 0 : result.size()-(result.count('.')==false ? 1 : 0))<<"\n";
    //system("pause");
    return 0;
}


#include<set>
#include<iostream>
using namespace std;
char str[101][101]={'.'};
int main()
{
    int n,m;
    char c;
    cin>>n>>m>>c;
    for (int i=1;i<=n;i++) for (int j=1;j<=m;j++) cin>>str[i][j];
    set <char> ok;
    int s[26]={0};
    int result=0;
    for (int i=2;i<n;i++)
        for (int j=2;j<m;j++)
            if (str[i][j]==c){
                s[str[i-1][j]-'A']++;
                s[str[i+1][j]-'A']++;
                s[str[i][j+1]-'A']++;
                s[str[i][j-1]-'A']++;
            }
    for (int i=0;i<26;i++) result+=s[i];
    if (s[c-'A']>0) result-=s[c-'A'];
    bool f;
    for (int i=1;i<=n;i++) for (int j=1;j<=n;j++) if (str[i][j]==c) f=1;
    result-=f;
    cout<<result<<"\n";
    //system("pause");
    return 0;
}


#define rep(i,a,n) for (int i=(a);i<=(n);i++)
#include<iostream>
using namespace std;
char str[101][101]={'.'};
int main()
{
    int n,m;
    char c=' ';
    cin>>n>>m>>c;
    rep(i,1,n) cin>>str[i];
    int s[26]={0};
    int result=0;
    rep(i,2,n) rep(j,2,m)
        if (str[i][j]==c){
                s[str[i-1][j]-'A']=1;
                s[str[i+1][j]-'A']=1;
                s[str[i][j+1]-'A']=1;
                s[str[i][j-1]-'A']=1;
        }
    rep(i,0,25) result+=s[i];
    cout<<result<<"\n";
    //system("pause");
    return 0;
}


#include<set>
#include<iostream>
using namespace std;
int main()
{
    int n,m;
    char c;
    cin>>n>>m>>c;
    char str[101][101]={'.'};
    for (int i=1;i<=n;i++) for (int j=1;j<=m;j++) cin>>str[i][j];
    set <char> answer;
    string number[100];
    for (int i=1;i<=n;i++)
        for (int j=1;j<=m;j++)
            number[i][j]=(str[i][j]==c ? '1' : '0');
    for (int i=1;i<=n;i++)
        for (int j=1;j<=n;j++)
            if (number[i][j]=='1'){
                answer.insert(str[i-1][j]);
                answer.insert(str[i+1][j]);
                answer.insert(str[i][j+1]);
                answer.insert(str[i][j-1]);
            }
    cout<<(!answer.size() ? 0 : answer.size()-2)<<"\n";
    //system("pause");
    return 0;
}


#include<set>
#include<iostream>
using namespace std;
char str[101][101]={'.'};
int main()
{
    int n,m;
    char c;
    cin>>n>>m>>c;
    for (int i=1;i<=n;i++) for (int j=1;j<=m;j++) cin>>str[i][j];
    set <char> ok;
    int s[26]={0};
    int result=0;
    for (int i=2;i<n;i++)
        for (int j=2;j<m;j++)
            if (str[i][j]==c){
                s[str[i-1][j]-'A']++;
                s[str[i+1][j]-'A']++;
                s[str[i][j+1]-'A']++;
                s[str[i][j-1]-'A']++;
            }
    for (int i=0;i<26;i++) result+=s[i];
    if (s[c-'A']>0) result-=s[c-'A']-1;
    bool f;
    for (int i=1;i<=n;i++) for (int j=1;j<=n;j++) if (str[i][j]==c) f=1;
    result-=f;
    cout<<result<<"\n";
    //system("pause");
    return 0;
}


#include<set>
#include<iostream>
using namespace std;
int main()
{
    int n,m;
    char c;
    cin>>n>>m>>c;
    char str[101][101]={'.'};
    for (int i=1;i<=n;i++) for (int j=1;j<=m;j++) cin>>str[i][j];
    set <char> answer;
    string number[100];
    for (int i=1;i<=n;i++)
        for (int j=1;j<=m;j++)
            number[i][j]=(str[i][j]==c ? '1' : '0');
    for (int i=1;i<=n;i++)
        for (int j=1;j<=n;j++)
            if (number[i][j]=='1'){
                answer.insert(str[i-1][j]);
                answer.insert(str[i+1][j]);
                answer.insert(str[i][j+1]);
                answer.insert(str[i][j-1]);
            }
    cout<<answer.size()-(answer.count(c)==true ? 1 : 0)-(answer.count('.')==true ? 1 : 0)<<"\n";
    //system("pause");
    return 0;
}


#include<set>
#include<iostream>
using namespace std;
char str[101][101]={'.'};
int main()
{
    int n,m;
    char c;
    cin>>n>>m>>c;
    for (int i=1;i<=n;i++) for (int j=1;j<=m;j++) cin>>str[i][j];
    set <char> result;
    string number[100];
    for (int i=1;i<=n;i++) for (int j=1;j<=m;j++) number[i][j]=(str[i][j]==c ? '1' : '0');
    for (int i=1;i<=n;i++)
        for (int j=1;j<=n;j++)
            if (number[i][j]=='1'){
                result.insert(str[i-1][j]);
                result.insert(str[i+1][j]);
                result.insert(str[i][j+1]);
                result.insert(str[i][j-1]);
            }
    cout<<(result.size()==0 ? 0 : result.size()-2)<<"\n";
    //system("pause");
    return 0;
}


#include<set>
#include<string>
#include<iostream>
using namespace std;
int main()
{
    int n,m;
    char c;
    cin>>n>>m>>c;
    string str[100];
    for (int i=0;i<n;i++) cin>>str[i];
    set <char> ok;
    string number[100];
    for (int i=0;i<n;i++)
        for (int j=0;j<m;j++)
            number[i][j]=(str[i][j]==c ? '1' : 0);
    for (int i=0;i<n;i++)
        for (int j=0;j<n;j++)
            if (number[i][j]=='1'){
                ok.insert(str[i-1][j]);
                ok.insert(str[i+1][j]);
                ok.insert(str[i][j+1]);
                ok.insert(str[i][j-1]);
            }
    cout<<ok.size()-2<<"\n";
    //system("pause");
    return 0;
}


#include<set>
#include<iostream>
using namespace std;
int main()
{
    int n,m;
    char c;
    cin>>n>>m>>c;
    char str[101][101]={'.'};
    for (int i=1;i<=n;i++) for (int j=1;j<=m;j++) cin>>str[i][j];
    set <char> result;
    string number[100];
    for (int i=1;i<=n;i++) for (int j=1;j<=m;j++) number[i][j]=(str[i][j]==c ? '1' : '0');
    for (int i=1;i<=n;i++)
        for (int j=1;j<=n;j++)
            if (number[i][j]=='1'){
                result.insert(str[i-1][j]);
                result.insert(str[i+1][j]);
                result.insert(str[i][j+1]);
                result.insert(str[i][j-1]);
            }
    cout<<(result.size()==0 ? 0 : result.size()-(result.count('.')==true ? 1 : 0)-(result.count(c)==true ? 1 : 0))<<"\n";
    //system("pause");
    return 0;
}


#include<set>
#include<iostream>
using namespace std;
char str[101][101]={'.'};
int main()
{
    int n,m;
    char c=' ';
    cin>>n>>m>>c;
    for (int i=1;i<=n;i++) for (int j=1;j<=m;j++) cin>>str[i][j];
    set <char> ok;
    int s[26]={0};
    int result=0;
    for (int i=2;i<n;i++)
        for (int j=2;j<m;j++)
            if (str[i][j]==c){
                s[str[i-1][j]-'A']++;
                s[str[i+1][j]-'A']++;
                s[str[i][j+1]-'A']++;
                s[str[i][j-1]-'A']++;
            }
    for (int i=0;i<26;i++) result+=s[i];
    int f;
    for (int i=1;i<=n;i++) for (int j=1;j<=n;j++) if (str[i][j]==c) f++;
    result-=f;
    for (int i=1;i<=n;i++)
        for (int j=1;j<=n;j++){
            if (str[i][j]=='.' &&
                (str[i-1][j]==c ||
                str[i+1][j]==c ||
                str[i][j-1]==c ||
                str[i][j+1]==c)
            )f=1;
        }
    result-=f;
    cout<<result<<"\n";
    //system("pause");
    return 0;
}


#include<set>
#include<string>
#include<iostream>
using namespace std;
int main()
{
    int n,m;
    char c;
    cin>>n>>m>>c;
    string str[100];
    for (int i=0;i<n;i++) cin>>str[i];
    set <char> ok;
    string number[100];
    for (int i=0;i<n;i++)
        for (int j=0;j<m;j++)
            number[i][j]=(str[i][j]==c ? '1' : '0');
    for (int i=1;i<n-1;i++)
        for (int j=1;j<n-1;j++)
            if (number[i][j]=='1'){
                ok.insert(str[i-1][j]);
                ok.insert(str[i+1][j]);
                ok.insert(str[i][j+1]);
                ok.insert(str[i][j-1]);
            }
    cout<<(ok.size()==0 ? 0 : ok.size()-1)<<"\n";
    //system("pause");
    return 0;
}


#include<set>
#include<iostream>
using namespace std;
int main()
{
    int n,m;
    char c;
    cin>>n>>m>>c;
    char str[101][101]={'.'};
    for (int i=1;i<=n;i++) for (int j=1;j<=m;j++) cin>>str[i][j];
    set <char> answer;
    string number[100];
    for (int i=1;i<=n;i++)
        for (int j=1;j<=m;j++)
            number[i][j]=(str[i][j]==c ? '1' : '0');
    for (int i=1;i<=n;i++)
        for (int j=1;j<=n;j++)
            if (number[i][j]=='1'){
                answer.insert(str[i-1][j]);
                answer.insert(str[i+1][j]);
                answer.insert(str[i][j+1]);
                answer.insert(str[i][j-1]);
            }
    cout<<(answer.size()<=0 ? 0 : answer.size()-2)<<"\n";
    //system("pause");
    return 0;
}


#include<set>
#include<iostream>
using namespace std;
int main()
{
    int n,m;
    char c;
    cin>>n>>m>>c;
    char str[101][101]={'.'};
    for (int i=1;i<=n;i++) for (int j=1;j<=m;j++) cin>>str[i][j];
    set <char> answer;
    string number[100];
    for (int i=1;i<=n;i++)
        for (int j=1;j<=m;j++)
            number[i][j]=(str[i][j]==c ? '1' : '0');
    for (int i=1;i<=n;i++)
        for (int j=1;j<=n;j++)
            if (number[i][j]=='1'){
                answer.insert(str[i-1][j]);
                answer.insert(str[i+1][j]);
                answer.insert(str[i][j+1]);
                answer.insert(str[i][j-1]);
            }
    cout<<answer.size()<<"\n";
    //system("pause");
    return 0;
}


#include<set>
#include<string>
#include<iostream>
using namespace std;
int main()
{
    int n,m;
    char c;
    cin>>n>>m>>c;
    string str[100];
    for (int i=0;i<n;i++) cin>>str[i];
    set <char> result;
    string number[100];
    for (int i=0;i<n;i++)
        for (int j=0;j<m;j++)
            number[i][j]=(str[i][j]==c ? '1' : '0');
    for (int i=1;i<n-1;i++)
        for (int j=1;j<n-1;j++)
            if (number[i][j]=='1'){
                result.insert(str[i-1][j]);
                result.insert(str[i+1][j]);
                result.insert(str[i][j+1]);
                result.insert(str[i][j-1]);
            }
    cout<<(result.size()==0 ? 0 : result.size()-1)<<"\n";
    //system("pause");
    return 0;
}


#include<set>
#include<iostream>
using namespace std;
char str[101][101]={'.'};
int main()
{
    int n,m;
    char c=' ';
    cin>>n>>m>>c;
    for (int i=1;i<=n;i++) for (int j=1;j<=m;j++) cin>>str[i][j];
    set <char> ok;
    int s[26]={0};
    int result=0;
    for (int i=2;i<n;i++)
        for (int j=2;j<m;j++)
            if (str[i][j]==c){
                s[str[i-1][j]-'A']++;
                s[str[i+1][j]-'A']++;
                s[str[i][j+1]-'A']++;
                s[str[i][j-1]-'A']++;
            }
    for (int i=0;i<26;i++) result+=s[i];
    int f;
    for (int i=1;i<=n;i++) for (int j=1;j<=n;j++){
        if (str[i][j]==c) f=1;
        if (str[i][j]=='.') f=2;
    }
    result-=f;
    cout<<result<<"\n";
    //system("pause");
    return 0;
}