#include <iostream>
#include <bits/stdc++.h>
#include <stdio.h>
#include <ctype.h>
#include <cmath>

using namespace std;

int main() {
	int n, m, x;
	cin >> n >> m >> x;
	x *= x;
	char a[30][30];
	bool key[26] = {false};
	bool used[26] = {false};
	vector< pair<int, int> > s;
	for (int i = 0; i < n; i++) {
		for (int j = 0; j < m; j++) {
			cin >> a[i][j];
			if (a[i][j] != 'S') key[a[i][j] - 'a'] = true;
			else s.push_back(make_pair(i, j));
		}
	}

	for (int i = 0; i < s.size(); i++) {
		for (int j = 0; j < n; j++) {
			for (int k = 0; k < m; k++) {
				if (a[j][k] != 'S') {
					double dis = pow(s[i].first - j, 2) + pow(s[i].second - k, 2);
					if (dis < x) used[a[j][k] - 'a'] = true;
				}
			}
		}
	}

	int q;
	cin >> q;
	string text;
	cin >> text;
	int hands = 0;
	for (int i =0; i < q; i++) {
		char c = text[i];
		if(islower(c)) {
			if (!key[c - 'a']) {
				cout << -1;
				return 0;
			}
		} else {
			if (!key[c - 'A']) {
				cout << -1;
				return 0;
			} else if (!used[c - 'A']) hands++;
		}
	}
	cout << hands;
	return 0;
}


#include <iostream>
#include <bits/stdc++.h>
#include <stdio.h>
#include <ctype.h>
#include <cmath>

using namespace std;

int main() {
	int n, m, x;
	cin >> n >> m >> x;
	char a[30][30];
	bool key[26];
	vector< pair<int,int> > s;
	for (int i = 0; i < n; i++) {
		for (int j = 0; j < m; j++) {
			cin >> a[i][j];
			if (a[i][j] != 'S') key[a[i][j] - 'a'] = true;
			else s.push_back(make_pair(i, j));
		}
	}
	int q;
	cin >> q;
	string text;
	cin >> text;
	int hands = 0;
	for (int i = 0; i < q; i++) {
		char c =  text[i];
		if (islower(c)) {
			if(!key[c-'a']) {
				cout << -1;
				return 0;
			}
		} else {
			c = tolower(c);
			bool type = false;
			for (int j = 0; j < n; j++) {
				for (int k = 0; k < m; k++) {
					if (a[j][k] == c) {
						for (int l = 0; l < s.size(); l++) {
							double dis = sqrt(pow(j - s[l].first, 2) + pow(k - s[l].second,2));
							if (dis <= x) type = true;
						}
					}
				}
			}
			if (s.empty()) {
				cout << -1;
				return 0;
			}
			else {
				if (!type) hands++;
			}
		}
	}
	cout << hands;
	return 0;
}


#include <iostream>
#include <bits/stdc++.h>
#include <stdio.h>
#include <ctype.h>
#include <cmath>

using namespace std;

int main() {
	int n, m, x;
	cin >> n >> m >> x;
	char a[30][30];
	bool key[26] = {false};
	vector< pair<int,int> > s;
	for (int i = 0; i < n; i++) {
		for (int j = 0; j < m; j++) {
			cin >> a[i][j];
			if (a[i][j] != 'S') key[a[i][j] - 'a'] = true;
			else s.push_back(make_pair(i, j));
		}
	}
	int q;
	cin >> q;
	string text;
	cin >> text;
	int hands = 0;
	for (int i = 0; i < q; i++) {
		char c =  text[i];
		if (islower(c)) {
			if(!key[c-'a']) {
				cout << -1;
				return 0;
			}
		} else {
			c = tolower(c);
			bool type = false;
			for (int j = 0; j < n; j++) {
				for (int k = 0; k < m; k++) {
					if (a[j][k] == c) {
						for (int l = 0; l < s.size(); l++) {
							double dis = sqrt(pow(j - s[l].first, 2) + pow(k - s[l].second,2));
							if (dis <= x) type = true;
						}
					}
				}
			}
			if (s.empty() || !key[c-'a']) {
				cout << -1;
				return 0;
			}
			else {
				if (!type) hands++;
			}
		}
	}
	cout << hands;
	return 0;
}


#include<bits/stdc++.h>
using namespace std;
#define fi(n) for(int i=0;i<n;i++)
#define fd(n) for(int i=n-1;i>=0;i--)
#define ll long long
#define pb push_back

float dix(int ax,int ay,int bx,int by){
	return (sqrt((ax-bx)*(ax-bx)*1.0 + (ay-by)*(ay-by)));
}
int main()
{
	int n,m,x,sh=0;cin>>n>>m>>x;
	char a[n][m];
	int ar[26];
	memset(ar,0,sizeof(ar));
	vector<pair<int,int> > shift;
	vector<pair<int,int> > cc[26];
	fi(n){
		for(int j=0;j<m;j++){
			cin>>a[i][j];
			if(a[i][j]!='S'){
			ar[a[i][j]-'a']=1;
			cc[a[i][j]-'a'].pb(make_pair(i,j));}
			if(a[i][j]=='S'){
				//cout<<"shift found\n";
			shift.pb(make_pair(i,j));}
		}
	}
	//cout<<"number of shift:"<<shift.size()<<'\n';
	int l;cin>>l;
		string s;cin>>s;
		int c=0,f;
		fi(l){f=1;
			char ch =s[i];
			if(s[i]>='A' && s[i]<='Z'){
				if(shift.size()==0){c=-1;break;}
				else{
						if(ar[(s[i]-'A')]==0){c=-1;break;}
						else{float md=INT_MAX*1.0;
							for(int ii=0;ii<cc[s[i]-'A'].size();ii++){
									int idx=cc[s[i]-'A'][ii].first;
									int idy=cc[s[i]-'A'][ii].second;
									for(int k=0;k<shift.size();k++){
										float d=dix(idx,idy,shift[k].first,shift[k].second);
											md=min(md,d);
											}
								}
								if(md>x)c++;
							}
					}
				}
			else if(s[i]>='a' && s[i]<='z'){
				if(ar[s[i]-'a']==0){
				c=-1;break;}
			}
		}
		cout<<c<<'\n';
return 0;
}


#include<bits/stdc++.h>
using namespace std;
#define fi(n) for(int i=0;i<n;i++)
#define fd(n) for(int i=n-1;i>=0;i--)
#define ll long long
#define pb push_back

float dix(int ax,int ay,int bx,int by){
	return (sqrt((ax-bx)*(ax-bx)*1.0 + (ay-by)*(ay-by)));
}
int main()
{
	int n,m,x,sh=0;cin>>n>>m>>x;
	char a[n][m];
	int ar[26];
	memset(ar,0,sizeof(ar));
	vector<pair<int,int> > shift;
	fi(n){
		for(int j=0;j<m;j++){
			cin>>a[i][j];
			if(a[i][j]!='S')
			ar[a[i][j]-'a']=1;
			if(a[i][j]=='S'){
				//cout<<"shift found\n";
			shift.pb(make_pair(i,j));}
		}
	}
	//cout<<"number of shift:"<<shift.size()<<'\n';
	int l;cin>>l;
		string s;cin>>s;
		int c=0,f;
		fi(l){f=1;
			char ch =s[i];

			if(s[i]>='A' && s[i]<='Z'){
				if(shift.size()==0){c=-1;break;}
				else{
						if(ar[(s[i]-'A')]==0){c=-1;break;}
						else{//cout<<"\nfor::"<<s[i]<<"=>\n";
							float md=INT_MAX*1.0;
							for(int ii=0;ii<n;ii++){
								for(int j=0;j<m;j++){
									if(a[ii][j]==((char)(s[i]-'A'+'a'))){//cout<<"index of char:"<<ii<<"and"<<j<<'\n';
										for(int k=0;k<shift.size();k++){
						//cout<<"shift:"<<(k+1)<<"at: "<<shift[k].first<<","<<shift[k].second<<'\n';
											float d=dix(ii,j,shift[k].first,shift[k].second);
						//cout<<"with distance :"<<d<<'\n';
											md=min(md,d);}
										}
								}}if(md>x)c++;
							}
					}
				}
			else if(s[i]>='a' && s[i]<='z'){
				if(ar[s[i]-'a']==0){
				c=-1;break;}
			}
		}
		cout<<c<<'\n';
return 0;
}


#include<bits/stdc++.h>
using namespace std;
#define fi(n) for(int i=0;i<n;i++)
#define fd(n) for(int i=n-1;i>=0;i--)
#define ll long long
#define pb push_back

float dix(int ax,int ay,int bx,int by){
	return (sqrt((ax-bx)*(ax-bx)*1.0 + (ay-by)*(ay-by)));
}
int main()
{
	int n,m,x,sh=0;cin>>n>>m>>x;
	char a[n][m];
	int ar[26];
	memset(ar,0,sizeof(ar));
	vector<pair<int,int> > shift;
	fi(n){
		for(int j=0;j<m;j++){
			cin>>a[i][j];
			if(a[i][j]!='S')
			ar[a[i][j]-'a']=1;
			if(a[i][j]=='S'){
				//cout<<"shift found\n";
			shift.pb(make_pair(i,j));}
		}
	}
	//cout<<"number of shift:"<<shift.size()<<'\n';
	int l;cin>>l;

		string s;cin>>s;
		int c=0,f;
		fi(l){f=1;
			char ch =s[i];

			if(s[i]>=65 && s[i]<=90){
				//capital
				//cout<<"capitallitis\n";
				if(shift.size()==0){f=0;
				cout<<-1<<'\n';}
				else{
					if(ar[(s[i]-'A')]==0){
				cout<<-1<<'\n';f=0;}
					else{//cout<<"\nfor::"<<s[i]<<"=>\n";
						for(int ii=0;ii<n;ii++){

					for(int j=0;j<m;j++){
						if(a[ii][j]==((char)(s[i]-'A'+'a')))
						{//cout<<"index of char:"<<ii<<"and"<<j<<'\n';
						float md=INT_MAX*1.0;
						for(int k=0;k<shift.size();k++){
						//cout<<"shift:"<<(k+1)<<"at: "<<shift[k].first<<","<<shift[k].second<<'\n';
						float d=dix(ii,j,shift[k].first,shift[k].second);
						//cout<<"with distance :"<<d<<'\n';
						md=min(md,d);}
						if(md>x)c++;
						}
							}}
	}
					}
				}
			else if(s[i]>=97 && s[i]<=122){
				//small
				//fi(26)cout<<ar[i]<<'\t';

				//cout<<"smallitis "<<'\n';
				if(ar[s[i]-'a']==0){
				cout<<-1<<'\n';f=0;}
			}

		}
		if(f==1)
		cout<<c<<'\n';

return 0;
}


#include<bits/stdc++.h>
using namespace std;
#define fi(n) for(int i=0;i<n;i++)
#define fd(n) for(int i=n-1;i>=0;i--)
#define ll long long
#define pb push_back
int x;
float dix(int ax,int ay,int bx,int by){
	return (sqrt((ax-bx)*(ax-bx)*1.0 + (ay-by)*(ay-by)));

}
bool check(int i,int j);
vector<pair<int,int> > shift;
int main()
{
	int n,m,sh=0;cin>>n>>m>>x;
	char a[n][m];
	int ar[26];
	memset(ar,0,sizeof(ar));

	vector<pair<int,int> > cc[26];
	fi(n){
		for(int j=0;j<m;j++){
			cin>>a[i][j];
			if(a[i][j]!='S'){
			ar[a[i][j]-'a']=1;
			cc[a[i][j]-'a'].pb(make_pair(i,j));}
			if(a[i][j]=='S'){
				//cout<<"shift found\n";
			shift.pb(make_pair(i,j));}
		}
	}
	//cout<<"number of shift:"<<shift.size()<<'\n';
	bool can[26];
	memset(can,false,sizeof(can));
	for (int i = 0;i<26; i++)
		for (int j = 0;j <cc[i].size() && !can[i]; j++)
		{   can[i] = can[i] | check(cc[i][j].first, cc[i][j].second);
		}
	int l;cin>>l;
	string s;cin>>s;
		int c=0,f;
		fi(l){f=1;
			char ch =s[i];
			if(s[i]>='A' && s[i]<='Z'){
				if(shift.size()==0){c=-1;break;}
				else{
						if(ar[(s[i]-'A')]==0){c=-1;break;}
						else{//float md=INT_MAX*1.0;
//							for(int k=0;k<shift.size();k++){
//									for(int ii=0;ii<cc[s[i]-'A'].size();ii++){
//									int idx=cc[s[i]-'A'][ii].first;
//									int idy=cc[s[i]-'A'][ii].second;
//										float d=dix(idx,idy,shift[k].first,shift[k].second);
//											md=min(md,d);
//											if(md<x)break;
//											}
//											if(md<x)break;
//								}
								//if(md>x)c++;
								if(!can[s[i]-'A'])c++;
							}
					}
				}
			else if(s[i]>='a' && s[i]<='z'){
				if(ar[s[i]-'a']==0){
				c=-1;break;}
			}
		}
		cout<<c<<'\n';
return 0;
}

bool check(int i,int j){
	for (int w = 0;w < shift.size();w++)
	{
		float dis = dix(i, j, shift[w].first, shift[w].second);
		if (dis <= x)
			return true;
	}
	return false;
}


#include<bits/stdc++.h>
using namespace std;
#define fi(n) for(int i=0;i<n;i++)
#define fd(n) for(int i=n-1;i>=0;i--)
#define ll long long
#define pb push_back

float dix(int ax,int ay,int bx,int by){
	return (sqrt((ax-bx)*(ax-bx)*1.0 + (ay-by)*(ay-by)));
}
int main()
{
	int n,m,x,sh=0;cin>>n>>m>>x;
	char a[n][m];
	int ar[26];
	memset(ar,0,sizeof(ar));
	vector<pair<int,int> > shift;
	fi(n){
		for(int j=0;j<m;j++){
			cin>>a[i][j];
			if(a[i][j]!='S')
			ar[a[i][j]-'a']=1;
			if(a[i][j]=='S'){
				//cout<<"shift found\n";
			shift.pb(make_pair(i,j));}
		}
	}
	//cout<<"number of shift:"<<shift.size()<<'\n';
	int l;cin>>l;
		string s;cin>>s;
		int c=0,f;
		fi(l){f=1;
			char ch =s[i];

			if(s[i]>=65 && s[i]<=90){
				//capital
				//cout<<"capitallitis\n";
				if(shift.size()==0){c=-1;break;}
				else{
					if(ar[(s[i]-'A')]==0){c=-1;break;}
					else{//cout<<"\nfor::"<<s[i]<<"=>\n";
					float md=INT_MAX*1.0;
						for(int ii=0;ii<n;ii++){

					for(int j=0;j<m;j++){
						if(a[ii][j]==((char)(s[i]-'A'+'a')))
						{//cout<<"index of char:"<<ii<<"and"<<j<<'\n';

						for(int k=0;k<shift.size();k++){
						//cout<<"shift:"<<(k+1)<<"at: "<<shift[k].first<<","<<shift[k].second<<'\n';
						float d=dix(ii,j,shift[k].first,shift[k].second);
						//cout<<"with distance :"<<d<<'\n';
						md=min(md,d);}
						if(md>x)c++;
						}
							}}
	}
					}
				}
			else if(s[i]>=97 && s[i]<=122){
				if(ar[s[i]-'a']==0){
				c=-1;break;}
			}

	}

		cout<<c<<'\n';

return 0;
}


#include<bits/stdc++.h>
using namespace std;
#define fi(n) for(int i=0;i<n;i++)
#define fd(n) for(int i=n-1;i>=0;i--)
#define ll long long
#define pb push_back

float dix(int ax,int ay,int bx,int by){
	return (sqrt((ax-bx)*(ax-bx)*1.0 + (ay-by)*(ay-by)));
}
int main()
{
	int n,m,x,sh=0;cin>>n>>m>>x;
	char a[n][m];
	int ar[26];
	memset(ar,0,sizeof(ar));
	vector<pair<int,int> > shift;
	vector<pair<int,int> > cc[26];
	fi(n){
		for(int j=0;j<m;j++){
			cin>>a[i][j];
			if(a[i][j]!='S'){
			ar[a[i][j]-'a']=1;
			cc[a[i][j]-'a'].pb(make_pair(i,j));}
			if(a[i][j]=='S'){
				//cout<<"shift found\n";
			shift.pb(make_pair(i,j));}
		}
	}
	//cout<<"number of shift:"<<shift.size()<<'\n';
	int l;cin>>l;
		string s;cin>>s;
		int c=0,f;
		fi(l){f=1;
			char ch =s[i];
			if(s[i]>='A' && s[i]<='Z'){
				if(shift.size()==0){c=-1;break;}
				else{
						if(ar[(s[i]-'A')]==0){c=-1;break;}
						else{float md=INT_MAX*1.0;
							for(int ii=0;ii<cc[s[i]-'A'].size();ii++){
									int idx=cc[s[i]-'A'][ii].first;
									int idy=cc[s[i]-'A'][ii].second;
									for(int k=0;k<shift.size();k++){
										float d=dix(idx,idy,shift[k].first,shift[k].second);
											md=min(md,d);
											if(md<x)break;
											}
											if(md<x)break;
								}
								if(md>x)c++;
							}
					}
				}
			else if(s[i]>='a' && s[i]<='z'){
				if(ar[s[i]-'a']==0){
				c=-1;break;}
			}
		}
		cout<<c<<'\n';
return 0;
}


#include<bits/stdc++.h>
using namespace std;
#define fi(n) for(int i=0;i<n;i++)
#define fd(n) for(int i=n-1;i>=0;i--)
#define ll long long
#define pb push_back

float dix(int ax,int ay,int bx,int by){
	return (sqrt((ax-bx)*(ax-bx)*1.0 + (ay-by)*(ay-by)));
}
int main()
{
	int n,m,x,sh=0;cin>>n>>m>>x;
	char a[n][m];
	int ar[26];
	memset(ar,0,sizeof(ar));
	vector<pair<int,int> > shift;
	fi(n){
		for(int j=0;j<m;j++){
			cin>>a[i][j];
			if(a[i][j]!='S')
			ar[a[i][j]-'a']=1;
			if(a[i][j]=='S'){
				//cout<<"shift found\n";
			shift.pb(make_pair(i,j));}
		}
	}
	//cout<<"number of shift:"<<shift.size()<<'\n';
	int l;cin>>l;

		string s;cin>>s;
		int c=0,f;
		fi(l){f=1;
			char ch =s[i];

			if(s[i]>=65 && s[i]<=90){
				//capital
				//cout<<"capitallitis\n";
				if(shift.size()==0){f=0;
				cout<<-1<<'\n';}
				else{
					if(ar[(s[i]-'A')]==0){
				cout<<-1<<'\n';f=0;}
					else{//cout<<"\nfor::"<<s[i]<<"=>\n";
					float md=INT_MAX*1.0;
						for(int ii=0;ii<n;ii++){

					for(int j=0;j<m;j++){
						if(a[ii][j]==((char)(s[i]-'A'+'a')))
						{//cout<<"index of char:"<<ii<<"and"<<j<<'\n';

						for(int k=0;k<shift.size();k++){
						//cout<<"shift:"<<(k+1)<<"at: "<<shift[k].first<<","<<shift[k].second<<'\n';
						float d=dix(ii,j,shift[k].first,shift[k].second);
						//cout<<"with distance :"<<d<<'\n';
						md=min(md,d);}
						if(md>x)c++;
						}
							}}
	}
					}
				}
			else if(s[i]>=97 && s[i]<=122){
				//small
				//fi(26)cout<<ar[i]<<'\t';

				//cout<<"smallitis "<<'\n';
				if(ar[s[i]-'a']==0){
				cout<<-1<<'\n';f=0;}
			}

		}
		if(f==1)
		cout<<c<<'\n';

return 0;
}


#include<bits/stdc++.h>
using namespace std;
#define fi(n) for(int i=0;i<n;i++)
#define fd(n) for(int i=n-1;i>=0;i--)
#define ll long long
#define pb push_back

float dix(int ax,int ay,int bx,int by){
	return (sqrt((ax-bx)*(ax-bx)*1.0 + (ay-by)*(ay-by)));
}
int main()
{
	int n,m,x,sh=0;cin>>n>>m>>x;
	char a[n][m];
	int ar[26];
	memset(ar,0,sizeof(ar));
	vector<pair<int,int> > shift;
	vector<pair<int,int> > cc[26];
	fi(n){
		for(int j=0;j<m;j++){
			cin>>a[i][j];
			if(a[i][j]!='S'){
			ar[a[i][j]-'a']=1;
			cc[a[i][j]-'a'].pb(make_pair(i,j));}
			if(a[i][j]=='S'){
				//cout<<"shift found\n";
			shift.pb(make_pair(i,j));}
		}
	}
	//cout<<"number of shift:"<<shift.size()<<'\n';
	int l;cin>>l;
		string s;cin>>s;
		int c=0,f;
		fi(l){f=1;
			char ch =s[i];
			if(s[i]>='A' && s[i]<='Z'){
				if(shift.size()==0){c=-1;break;}
				else{
						if(ar[(s[i]-'A')]==0){c=-1;break;}
						else{float md=INT_MAX*1.0;
							for(int k=0;k<shift.size();k++){
									for(int ii=0;ii<cc[s[i]-'A'].size();ii++){
									int idx=cc[s[i]-'A'][ii].first;
									int idy=cc[s[i]-'A'][ii].second;
										float d=dix(idx,idy,shift[k].first,shift[k].second);
											md=min(md,d);
											if(md<x)break;
											}
											if(md<x)break;
								}
								if(md>x)c++;
							}
					}
				}
			else if(s[i]>='a' && s[i]<='z'){
				if(ar[s[i]-'a']==0){
				c=-1;break;}
			}
		}
		cout<<c<<'\n';
return 0;
}


#include <bits/stdc++.h>
#define db(x) cout<<x<<" "
#define db1(x) cout<<x<<'\n'
#define db2(x,y) cout<<x<<" "<<y<<'\n'
#define db3(x,y,z) cout<<x<<" "<<y<<" "<<z<<'\n'
#define rep(i,n) for(int i=0;i<(n);++i)
#define repA(i,a,n) for(int i=a;i<=(n);++i)
#define repD(i,a,n) for(int i=a;i>=(n);--i)
#define pair(a,b) make_pair(a,b)
#define pb(a) push_back(a)
#define ff first
#define ss second
#define mod 1000000007
using namespace std;
using ll = long long int;
int X[] = {1,0,-1,0};
int Y[] = {0,1,0,-1};
//char mat[101][101];

// int *dp = new int[1000001];
// vector<int>primes;

// void sieve(){

//     for(int i=0;i<1000001;i++)
//     dp[i]=true;

//     dp[1]=false;

//     for(int i=2;i*i<=1000000;i++){

//         if(dp[i]){
//             for(int j=i*i;j<=1000000;j+=i){
//                 dp[j]=false;
//             }
//         }

//     }

//   for(int i=2;i<=1000000;i++)
//   if(dp[i])
//   primes.push_back(i);

// }

// bool isValid(int x,int y,int n,int m) {
//     return (x>=0 && y>=0 && x<n && y<m);
// }

// int dfs(int r,int c, bool *vis,char par, int n, int m) {

//     int ans = 0;

//     for(int i=0;i<4;i++) {
//         int x = X[i]+r;
//         int y = Y[i]+c;

//         bool valid = isValid(x,y,n,m);

//         if(valid == false)
//         continue;

//         if(mat[x][y]!=par && mat[x][y]!='.' && !vis[mat[x][y]-'A']) {
//             vis[mat[x][y]-'A']=true;
//             // db(x);
//             // db(y);
//             ans+=1;
//             //db1(ans);
//         }

//     }

//     return ans;

// }

int eucledian(int x1,int x2,int y1,int y2) {
    int ans = abs(x1-x2)*abs(x1-x2) + abs(y1-y2)*abs(y1-y2);

    return ceil((double)sqrt(ans));
}

bool isPossible(vector<pair<int,int>> &v1,vector<pair<int,int>> &v2, int x) {

    for(int i=0;i<v1.size();i++) {
        for(int j=0;j<v2.size();j++) {
            int euled = eucledian(v1[i].first,v2[j].first,v1[i].second,v2[j].second);
            //db(euled);
            if(euled<=x)
            return false;
        }
    }

return true;

}

void solve(){

  int n,m,x;
  cin>>n>>m>>x;

  unordered_map<char,vector<pair<int,int>>> um;

  rep(i,n) {
      int j=0;
      rep(j,m) {
          char c;
          cin>>c;
          um[c].push_back(make_pair(i,j));
      }
  }

  int q;
  cin>>q;
  string s;
  cin>>s;

  int ans = 0;

  rep(i,s.length()) {

      if(s[i]>=65 && s[i]<=90) {
          char temp = s[i]-'A'+'a';
          if(um.find('S')==um.end()) {
              db1(-1);
              return;
          }

         // db(s[i]);

          bool possible = isPossible(um[temp],um['S'],x);

         //db1(possible);
          if(possible){
          ans++;
          }
      }
      else {
          if(um.find(s[i])==um.end()){
              db1(-1);
              return;
          }
      }
  }

  db1(ans);

}

int main(){

 int t=1;
 //cin>>t;

while(t--){
 solve();
  }

    //   ll p = binary_search((ll)4,(ll)-2,(ll)5);

    //   db1(p);

  return 0;
}


#include <bits/stdc++.h>
#define db(x) cout<<x<<" "
#define db1(x) cout<<x<<'\n'
#define db2(x,y) cout<<x<<" "<<y<<'\n'
#define db3(x,y,z) cout<<x<<" "<<y<<" "<<z<<'\n'
#define rep(i,n) for(int i=0;i<(n);++i)
#define repA(i,a,n) for(int i=a;i<=(n);++i)
#define repD(i,a,n) for(int i=a;i>=(n);--i)
#define pair(a,b) make_pair(a,b)
#define pb(a) push_back(a)
#define ff first
#define ss second
#define mod 1000000007
using namespace std;
using ll = long long int;
int X[] = {1,0,-1,0};
int Y[] = {0,1,0,-1};
//char mat[101][101];

// int *dp = new int[1000001];
// vector<int>primes;

// void sieve(){

//     for(int i=0;i<1000001;i++)
//     dp[i]=true;

//     dp[1]=false;

//     for(int i=2;i*i<=1000000;i++){

//         if(dp[i]){
//             for(int j=i*i;j<=1000000;j+=i){
//                 dp[j]=false;
//             }
//         }

//     }

//   for(int i=2;i<=1000000;i++)
//   if(dp[i])
//   primes.push_back(i);

// }

// bool isValid(int x,int y,int n,int m) {
//     return (x>=0 && y>=0 && x<n && y<m);
// }

// int dfs(int r,int c, bool *vis,char par, int n, int m) {

//     int ans = 0;

//     for(int i=0;i<4;i++) {
//         int x = X[i]+r;
//         int y = Y[i]+c;

//         bool valid = isValid(x,y,n,m);

//         if(valid == false)
//         continue;

//         if(mat[x][y]!=par && mat[x][y]!='.' && !vis[mat[x][y]-'A']) {
//             vis[mat[x][y]-'A']=true;
//             // db(x);
//             // db(y);
//             ans+=1;
//             //db1(ans);
//         }

//     }

//     return ans;

// }

int eucledian(int x1,int x2,int y1,int y2) {
    int ans = abs(x1-x2)*abs(x1-x2) + abs(y1-y2)*abs(y1-y2);
    return ans;
}

bool isPossible(vector<pair<int,int>> &v1,vector<pair<int,int>> &v2, int x) {

    for(int i=0;i<v1.size();i++) {
        for(int j=0;j<v2.size();j++) {
            int euled = eucledian(v1[i].first,v2[j].first,v1[i].second,v2[j].second);
            if(euled<=(x*x))
            return false;
        }
    }

return true;

}

void solve(){

  int n,m,x;
  cin>>n>>m>>x;

  unordered_map<char,vector<pair<int,int>>> um;

  rep(i,n) {
      int j=0;
      rep(j,m) {
          char c;
          cin>>c;
          um[c].push_back(make_pair(i,j));
      }
  }

  int q;
  cin>>q;
  string s;
  cin>>s;

  int ans = 0;

  rep(i,s.length()) {
      if(s[i]>=65 && s[i]<=90) {
          char temp = s[i]-'A'+'a';
          if(um.find('S')==um.end() || um.find(temp)==um.end()) {
                um.clear();
              db1(-1);
              return;
          }

          bool possible = isPossible(um[temp],um['S'],x);

          if(possible){

          ans++;
          }
      }
      else {
          if(um.find(s[i])==um.end()){
                um.clear();
              db1(-1);
              return;
          }
      }
  }

  db1(ans);

  um.clear();

}

int main(){

 int t=1;
 //cin>>t;

while(t--){
 solve();
  }

    //   ll p = binary_search((ll)4,(ll)-2,(ll)5);

    //   db1(p);

  return 0;
}


#include <bits/stdc++.h>
#define db(x) cout<<x<<" "
#define db1(x) cout<<x<<'\n'
#define db2(x,y) cout<<x<<" "<<y<<'\n'
#define db3(x,y,z) cout<<x<<" "<<y<<" "<<z<<'\n'
#define rep(i,n) for(int i=0;i<(n);++i)
#define repA(i,a,n) for(int i=a;i<=(n);++i)
#define repD(i,a,n) for(int i=a;i>=(n);--i)
#define pair(a,b) make_pair(a,b)
#define pb(a) push_back(a)
#define ff first
#define ss second
#define mod 1000000007
using namespace std;
using ll = long long int;
int X[] = {1,0,-1,0};
int Y[] = {0,1,0,-1};

int eucledian(int x1,int x2,int y1,int y2) {
    int ans = abs(x1-x2)*abs(x1-x2) + abs(y1-y2)*abs(y1-y2);
    return ans;
}

bool isPossible(vector<pair<int,int>> &v1,vector<pair<int,int>> &v2, int x) {

    for(int i=0;i<v1.size();i++) {
        for(int j=0;j<v2.size();j++) {
            int euled = eucledian(v1[i].first,v2[j].first,v1[i].second,v2[j].second);
            if(euled<=(x*x))
            return false;
        }
    }

return true;

}

void solve(){

  int n,m,x;
  cin>>n>>m>>x;

  unordered_map<char,vector<pair<int,int>>> um;

  rep(i,n) {
      int j=0;
      rep(j,m) {
          char c;
          cin>>c;
          um[c].push_back(make_pair(i,j));
      }
  }

  int q;
  cin>>q;
  string s;
  cin>>s;

  unordered_map<char,int> mp;

  rep(i,s.length())
  mp[s[i]]++;

  int ans = 0;

  for(auto it : mp) {
      if(it.first>=65 && it.first<=90) {
          char temp = it.first-'A'+'a';

          if(um.find('S')==um.end() || um.find(temp)==um.end()) {
              db1(-1);
              return;
          }

          bool possible = isPossible(um[temp],um['S'],x);

          if(possible){
          ans+=it.second;
          }
      }
      else {
          if(um.find(it.first)==um.end()){
              db1(-1);
              return;
          }
      }
  }

  db1(ans);

}

int main(){

    ios_base::sync_with_stdio(false);
    cin.tie(NULL);

    int t=1;

    while(t--){
        solve();
    }

  return 0;
}


#include <bits/stdc++.h>
#define db(x) cout<<x<<" "
#define db1(x) cout<<x<<'\n'
#define db2(x,y) cout<<x<<" "<<y<<'\n'
#define db3(x,y,z) cout<<x<<" "<<y<<" "<<z<<'\n'
#define rep(i,n) for(int i=0;i<(n);++i)
#define repA(i,a,n) for(int i=a;i<=(n);++i)
#define repD(i,a,n) for(int i=a;i>=(n);--i)
#define pair(a,b) make_pair(a,b)
#define pb(a) push_back(a)
#define ff first
#define ss second
#define mod 1000000007
using namespace std;
using ll = long long int;
int X[] = {1,0,-1,0};
int Y[] = {0,1,0,-1};
//char mat[101][101];

// int *dp = new int[1000001];
// vector<int>primes;

// void sieve(){

//     for(int i=0;i<1000001;i++)
//     dp[i]=true;

//     dp[1]=false;

//     for(int i=2;i*i<=1000000;i++){

//         if(dp[i]){
//             for(int j=i*i;j<=1000000;j+=i){
//                 dp[j]=false;
//             }
//         }

//     }

//   for(int i=2;i<=1000000;i++)
//   if(dp[i])
//   primes.push_back(i);

// }

// bool isValid(int x,int y,int n,int m) {
//     return (x>=0 && y>=0 && x<n && y<m);
// }

// int dfs(int r,int c, bool *vis,char par, int n, int m) {

//     int ans = 0;

//     for(int i=0;i<4;i++) {
//         int x = X[i]+r;
//         int y = Y[i]+c;

//         bool valid = isValid(x,y,n,m);

//         if(valid == false)
//         continue;

//         if(mat[x][y]!=par && mat[x][y]!='.' && !vis[mat[x][y]-'A']) {
//             vis[mat[x][y]-'A']=true;
//             // db(x);
//             // db(y);
//             ans+=1;
//             //db1(ans);
//         }

//     }

//     return ans;

// }

int eucledian(int x1,int x2,int y1,int y2) {
    int ans = abs(x1-x2)*abs(x1-x2) + abs(y1-y2)*abs(y1-y2);

    return ceil((double)sqrt(ans));
}

bool isPossible(vector<pair<int,int>> &v1,vector<pair<int,int>> &v2, int x) {

    for(int i=0;i<v1.size();i++) {
        for(int j=0;j<v2.size();j++) {
            int euled = eucledian(v1[i].first,v2[j].first,v1[i].second,v2[j].second);
            //db(euled);
            if(euled<=x)
            return false;
        }
    }

return true;

}

void solve(){

  int n,m,x;
  cin>>n>>m>>x;

  unordered_map<char,vector<pair<int,int>>> um;

  rep(i,n) {
      int j=0;
      rep(j,m) {
          char c;
          cin>>c;
          um[c].push_back(make_pair(i,j));
      }
  }

  int q;
  cin>>q;
  string s;
  cin>>s;

  int ans = 0;

  rep(i,s.length()) {
      if(s[i]>=65 && s[i]<=90) {
          char temp = s[i]-'A'+'a';
          if(um.find('S')==um.end()) {
              db1(-1);
              return;
          }

         // db(s[i]);

          bool possible = isPossible(um[temp],um['S'],x);

         //db1(possible);
          if(possible){
          ans++;
          }
      }
  }

  db1(ans);

}

int main(){

 int t=1;
 //cin>>t;

while(t--){
 solve();
  }

    //   ll p = binary_search((ll)4,(ll)-2,(ll)5);

    //   db1(p);

  return 0;
}


#include <bits/stdc++.h>
#define db(x) cout<<x<<" "
#define db1(x) cout<<x<<'\n'
#define db2(x,y) cout<<x<<" "<<y<<'\n'
#define db3(x,y,z) cout<<x<<" "<<y<<" "<<z<<'\n'
#define rep(i,n) for(int i=0;i<(n);++i)
#define repA(i,a,n) for(int i=a;i<=(n);++i)
#define repD(i,a,n) for(int i=a;i>=(n);--i)
#define pair(a,b) make_pair(a,b)
#define pb(a) push_back(a)
#define ff first
#define ss second
#define mod 1000000007
using namespace std;
using ll = long long int;
int X[] = {1,0,-1,0};
int Y[] = {0,1,0,-1};
//char mat[101][101];

// int *dp = new int[1000001];
// vector<int>primes;

// void sieve(){

//     for(int i=0;i<1000001;i++)
//     dp[i]=true;

//     dp[1]=false;

//     for(int i=2;i*i<=1000000;i++){

//         if(dp[i]){
//             for(int j=i*i;j<=1000000;j+=i){
//                 dp[j]=false;
//             }
//         }

//     }

//   for(int i=2;i<=1000000;i++)
//   if(dp[i])
//   primes.push_back(i);

// }

// bool isValid(int x,int y,int n,int m) {
//     return (x>=0 && y>=0 && x<n && y<m);
// }

// int dfs(int r,int c, bool *vis,char par, int n, int m) {

//     int ans = 0;

//     for(int i=0;i<4;i++) {
//         int x = X[i]+r;
//         int y = Y[i]+c;

//         bool valid = isValid(x,y,n,m);

//         if(valid == false)
//         continue;

//         if(mat[x][y]!=par && mat[x][y]!='.' && !vis[mat[x][y]-'A']) {
//             vis[mat[x][y]-'A']=true;
//             // db(x);
//             // db(y);
//             ans+=1;
//             //db1(ans);
//         }

//     }

//     return ans;

// }

int eucledian(int x1,int x2,int y1,int y2) {
    int ans = abs(x1-x2)*abs(x1-x2) + abs(y1-y2)*abs(y1-y2);

    return ceil(sqrt(ans));
}

bool isPossible(vector<pair<int,int>> &v1,vector<pair<int,int>> &v2, int x) {

    for(int i=0;i<v1.size();i++) {
        for(int j=0;j<v2.size();j++) {
            int euled = eucledian(v1[i].first,v2[j].first,v1[i].second,v2[j].second);
            //db(euled);
            if(euled<=x)
            return false;
        }
    }

return true;

}

void solve(){

  int n,m,x;
  cin>>n>>m>>x;

  unordered_map<char,vector<pair<int,int>>> um;

  rep(i,n) {
      int j=0;
      rep(j,m) {
          char c;
          cin>>c;
          um[c].push_back(make_pair(i,j));
      }
  }

  int q;
  cin>>q;
  string s;
  cin>>s;

  int ans = 0;

  rep(i,s.length()) {

      if(s[i]>=65 && s[i]<=90) {
          char temp = s[i]-'A'+'a';
          if(um.find('S')==um.end()) {
              db1(-1);
              return;
          }

         // db(s[i]);

          bool possible = isPossible(um[temp],um['S'],x);

         //db1(possible);
          if(possible){
          //db1(s[i]);
          ans++;
          }
      }
      else {
          if(um.find(s[i])==um.end()){
              db1(-1);
              return;
          }
      }
  }

  db1(ans);

}

int main(){

 int t=1;
 //cin>>t;

while(t--){
 solve();
  }

    //   ll p = binary_search((ll)4,(ll)-2,(ll)5);

    //   db1(p);

  return 0;
}


#include <bits/stdc++.h>
#define db(x) cout<<x<<" "
#define db1(x) cout<<x<<'\n'
#define db2(x,y) cout<<x<<" "<<y<<'\n'
#define db3(x,y,z) cout<<x<<" "<<y<<" "<<z<<'\n'
#define rep(i,n) for(int i=0;i<(n);++i)
#define repA(i,a,n) for(int i=a;i<=(n);++i)
#define repD(i,a,n) for(int i=a;i>=(n);--i)
#define pair(a,b) make_pair(a,b)
#define pb(a) push_back(a)
#define ff first
#define ss second
#define mod 1000000007
using namespace std;
using ll = long long int;
int X[] = {1,0,-1,0};
int Y[] = {0,1,0,-1};
//char mat[101][101];

// int *dp = new int[1000001];
// vector<int>primes;

// void sieve(){

//     for(int i=0;i<1000001;i++)
//     dp[i]=true;

//     dp[1]=false;

//     for(int i=2;i*i<=1000000;i++){

//         if(dp[i]){
//             for(int j=i*i;j<=1000000;j+=i){
//                 dp[j]=false;
//             }
//         }

//     }

//   for(int i=2;i<=1000000;i++)
//   if(dp[i])
//   primes.push_back(i);

// }

// bool isValid(int x,int y,int n,int m) {
//     return (x>=0 && y>=0 && x<n && y<m);
// }

// int dfs(int r,int c, bool *vis,char par, int n, int m) {

//     int ans = 0;

//     for(int i=0;i<4;i++) {
//         int x = X[i]+r;
//         int y = Y[i]+c;

//         bool valid = isValid(x,y,n,m);

//         if(valid == false)
//         continue;

//         if(mat[x][y]!=par && mat[x][y]!='.' && !vis[mat[x][y]-'A']) {
//             vis[mat[x][y]-'A']=true;
//             // db(x);
//             // db(y);
//             ans+=1;
//             //db1(ans);
//         }

//     }

//     return ans;

// }

int eucledian(int x1,int x2,int y1,int y2) {
    int ans = abs(x1-x2)*abs(x1-x2) + abs(y1-y2)*abs(y1-y2);
    return ans;
}

bool isPossible(vector<pair<int,int>> &v1,vector<pair<int,int>> &v2, int x) {

    for(int i=0;i<v1.size();i++) {
        for(int j=0;j<v2.size();j++) {
            int euled = eucledian(v1[i].first,v2[j].first,v1[i].second,v2[j].second);
            if(euled<=(x*x))
            return false;
        }
    }

return true;

}

void solve(){

  int n,m,x;
  cin>>n>>m>>x;

  unordered_map<char,vector<pair<int,int>>> um;

  rep(i,n) {
      int j=0;
      rep(j,m) {
          char c;
          cin>>c;
          um[c].push_back(make_pair(i,j));
      }
  }

  int q;
  cin>>q;
  string s;
  cin>>s;

  int ans = 0;

  rep(i,s.length()) {
      if(s[i]>=65 && s[i]<=90) {
          char temp = s[i]-'A'+'a';

          if(um.find('S')==um.end() || um.find(temp)==um.end()) {
               // um.clear();
              db1(-1);
              return;
          }

          bool possible = isPossible(um[temp],um['S'],x);

          if(possible){
          ans++;
          }
      }
      else {
          if(um.find(s[i])==um.end()){
               // um.clear();
              db1(-1);
              return;
          }
      }
  }

  db1(ans);

  //um.clear();

}

int main(){

     ios_base::sync_with_stdio(false);
    cin.tie(NULL);

 int t=1;
 //cin>>t;

while(t--){
 solve();
  }

    //   ll p = binary_search((ll)4,(ll)-2,(ll)5);

    //   db1(p);

  return 0;
}


#include <bits/stdc++.h>
#define db(x) cout<<x<<" "
#define db1(x) cout<<x<<'\n'
#define db2(x,y) cout<<x<<" "<<y<<'\n'
#define db3(x,y,z) cout<<x<<" "<<y<<" "<<z<<'\n'
#define rep(i,n) for(int i=0;i<(n);++i)
#define repA(i,a,n) for(int i=a;i<=(n);++i)
#define repD(i,a,n) for(int i=a;i>=(n);--i)
#define pair(a,b) make_pair(a,b)
#define pb(a) push_back(a)
#define ff first
#define ss second
#define mod 1000000007
using namespace std;
using ll = long long int;
int X[] = {1,0,-1,0};
int Y[] = {0,1,0,-1};
//char mat[101][101];

// int *dp = new int[1000001];
// vector<int>primes;

// void sieve(){

//     for(int i=0;i<1000001;i++)
//     dp[i]=true;

//     dp[1]=false;

//     for(int i=2;i*i<=1000000;i++){

//         if(dp[i]){
//             for(int j=i*i;j<=1000000;j+=i){
//                 dp[j]=false;
//             }
//         }

//     }

//   for(int i=2;i<=1000000;i++)
//   if(dp[i])
//   primes.push_back(i);

// }

// bool isValid(int x,int y,int n,int m) {
//     return (x>=0 && y>=0 && x<n && y<m);
// }

// int dfs(int r,int c, bool *vis,char par, int n, int m) {

//     int ans = 0;

//     for(int i=0;i<4;i++) {
//         int x = X[i]+r;
//         int y = Y[i]+c;

//         bool valid = isValid(x,y,n,m);

//         if(valid == false)
//         continue;

//         if(mat[x][y]!=par && mat[x][y]!='.' && !vis[mat[x][y]-'A']) {
//             vis[mat[x][y]-'A']=true;
//             // db(x);
//             // db(y);
//             ans+=1;
//             //db1(ans);
//         }

//     }

//     return ans;

// }

int eucledian(int x1,int x2,int y1,int y2) {
    int ans = abs(x1-x2)*abs(x1-x2) + abs(y1-y2)*abs(y1-y2);

    return ans;
}

bool isPossible(vector<pair<int,int>> &v1,vector<pair<int,int>> &v2, int x) {

    for(int i=0;i<v1.size();i++) {
        for(int j=0;j<v2.size();j++) {
            int euled = eucledian(v1[i].first,v2[j].first,v1[i].second,v2[j].second);
            //db(euled);
            if(euled<=(x*x))
            return false;
        }
    }

return true;

}

void solve(){

  int n,m,x;
  cin>>n>>m>>x;

  unordered_map<char,vector<pair<int,int>>> um;

  rep(i,n) {
      int j=0;
      rep(j,m) {
          char c;
          cin>>c;
          um[c].push_back(make_pair(i,j));
      }
  }

  int q;
  cin>>q;
  string s;
  cin>>s;

  int ans = 0;

  rep(i,s.length()) {

      if(s[i]>=65 && s[i]<=90) {
          char temp = s[i]-'A'+'a';
          if(um.find('S')==um.end()) {
              db1(-1);
              return;
          }

         // db(s[i]);

          bool possible = isPossible(um[temp],um['S'],x);

         //db1(possible);
          if(possible){
          //db1(s[i]);
          ans++;
          }
      }
      else {
          if(um.find(s[i])==um.end()){
              db1(-1);
              return;
          }
      }
  }

  db1(ans);

}

int main(){

 int t=1;
 //cin>>t;

while(t--){
 solve();
  }

    //   ll p = binary_search((ll)4,(ll)-2,(ll)5);

    //   db1(p);

  return 0;
}


#include <iostream>
#include <vector>
#include <utility>
#include <map>
#include <cmath>
#define mymap map<char, pair<int, int> >
using namespace std;

int main()
{
    int n, m, x, f_s = 0, ans = 0;;
    vector< pair<int, int> > shifts;
    mymap keys;
    cin >> n >> m >> x;
    char kb[n][m];
    for (int i = 0; i < n; i++)
    {
        for (int j = 0; j < m; j++)
        {
            char x;
            cin >> x;
            if (x == 'S')
                shifts.push_back(make_pair(i+1, j+1));
            else
                keys[x] = make_pair(i+1, j+1);
        }
    }
    int l;
    cin >> l;
    mymap::iterator it;
    f_s = shifts.size() > 0;
    for (int i = 0; i < l; i++)
    {
        char c;
        cin >> c;
        if (c >= 'a' && c <= 'z')
        {
            it = keys.find(c);
            if (it == keys.end())
            {
                cout << -1;
                return 0;
            }
        }
        else
        {
            it = keys.find(c|32);
            if (it == keys.end() || !f_s)
            {
                cout << -1;
                return 0;
            }
            else
            {
                typedef std::vector<std::pair<int, int> > myvec;
                int f = 0;
                for( myvec::iterator it2 = shifts.begin(); it2 != shifts.end(); ++it2 )
                {
                    int x1 = it -> second.first;
                    int y1 = it -> second.second;
                  int x2 = it2->first;
                  int y2 = it2->second;
                  double xx = sqrt(pow(x1-x2, 2.0)+ pow(y1-y2, 2.0));
                  if (xx <= x)
                  {
                      f = 1;
                      break;
                  }
                }
                if (!f)
                    ans++;
            }
        }
    }
    cout << ans << endl;
}


#include <iostream>
#include <map>
#include <vector>
#include <cmath>
using namespace std;

int main()
{
    int n, m, x;
    cin >> n >> m >> x;
    map < char, vector<int> > mp;
    for (int i = 1; i <= n; i++)
    {
        for (int j = 1; j <= m; j++)
        {
            char c;
            cin >> c;
            mp[c].push_back(i);
            mp[c].push_back(j);
        }
    }
    int q;
    cin >> q;
    string s;
    cin >> s;
    int ans = 0;
    for (int k = 0; k < q; k++)
    {
        char i = s[k];
        if (i >= 'a' && i <= 'z')
        {
            if (!mp.count(i)) {
                cout << -1;
                return 0;
            }
        }else
        {
            i = (char)tolower(i);
            //cout << mp.count(i) << endl << mp.count('S') << endl;
            if(!mp.count(i) || !mp.count('S'))
            {
                cout << -1;
                return 0;
            }
            double min_d = 1000000;
            int num = mp[i].size();
            for (int jj = 0; jj < num-1; jj+=2)
            {
                for (int ii = 0; ii < mp['S'].size()-1 ; ii+=2)
                {
                    int x1 = mp[i][jj];
                    int y1 = mp[i][jj+1];
                    int x2 = mp['S'][ii];
                    int y2 = mp['S'][ii+1];
                    //printf("%d\t%d\t%d\t%d\n", x1, y1, x2, y2);
                    double d = sqrt(pow(x1-x2, 2) + pow(y1-y2, 2));
                    min_d = min(d, min_d);
                }
            }
            if (min_d > x)
                ans++;
        }
    }
    cout << ans << endl;
}


#include <iostream>
#include <map>
#include <vector>
#include <cmath>
using namespace std;

int main()
{
    int n, m, x;
    cin >> n >> m >> x;
    map < char, vector<int> > mp;
    for (int i = 1; i <= n; i++)
    {
        for (int j = 1; j <= m; j++)
        {
            char c;
            cin >> c;
            mp[c].push_back(i);
            mp[c].push_back(j);
        }
    }
    int q;
    cin >> q;
    string s;
    cin >> s;
    int ans = 0;
    int ns = mp['S'].size();
    for (int k = 0; k < q; k++)
    {
        char i = s[k];
        if (i >= 'a' && i <= 'z')
        {
            if (mp.count(i) == 0 ) {
                cout << -1;
                return 0;
            }
        }else
        {
            i = tolower(i);
            if(mp.count(i)==0 || mp.count('S')==0)
            {
                cout << -1;
                return 0;
            }
            double min_d = 1000000;
            int num = mp[i].size();
            for (int jj = 0; jj < num-1; jj+=2)
            {
                for (int ii = 0; ii < ns-1 ; ii+=2)
                {
                    int x1 = mp[i][jj];
                    int y1 = mp[i][jj+1];
                    int x2 = mp['S'][ii];
                    int y2 = mp['S'][ii+1];
                    //printf("%d\t%d\t%d\t%d\n", x1, y1, x2, y2);
                    double d = sqrt(pow(x1-x2, 2) + pow(y1-y2, 2));
                    min_d = min(d, min_d);
                }
            }
            if (min_d > x)
                ans++;
        }
    }
    cout << ans << endl;
}


#include <iostream>
#include <algorithm>
#include <vector>
#include <cmath>

#define fast ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0);
#define ull unsigned long long
using namespace std;

int state[26];

int main()
{
    int n, m, x, f = 0;
    cin >> n >> m >> x;
    char kb[n][m];
    for (int i = 0; i < n; i++)
    {
        for (int j = 0; j < m; j++)
        {
            char t;
            cin >> t;
            if (t == 'S')
                f++;
            else
            {
                state[t-'a']++;
            }
            kb[i][j] = t;
        }
    }
    if (f)
    {
        for (char i = 'a'; i <= 'z'; i++)
        {
            if (state[i-'a'] > 0)
            {
                state[i-'a']++;
                double min_d = 10000;
                for (int j = 0; j < n; j++)
                {
                    for (int k = 0; k < m; k++)
                    {
                        if (kb[j][k] == i)
                        {
                            for (int l = 0; l < n; l++)
                            {
                                for (int ll = 0; ll < m; ll++)
                                {
                                    if (kb[l][ll] == 'S')
                                    {

                                        double d = sqrt(pow(j-l,2.0) + pow(k-ll,2.0));
                                        min_d = min(d, min_d);
                                    }
                                }
                            }
                        }
                    }
                }
                if (min_d > x)
                    state[i-'a']++;

            }
        }
    }
    int q, ans  = 0;
    string s;
    cin >> q >> s;

    for (char c : s)
    {

        if (c >= 'a' && c <= 'z')
        {
            int i = c - 'a';
            if (state[i] == 0)
            {
                cout << -1;
                return 0;
            }
        }else
        {
            c = tolower(c);
            int i = c - 'a';

            if (state[i] == 3)
                ans++;
            else if (state[i] != 2)
            {
                cout << -1;
                return 0;
            }
        }
    }

    cout << ans << endl;
}


#include <iostream>
#include <algorithm>
#include <vector>
#include <cmath>

#define fast ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0);
#define ull unsigned long long
using namespace std;

int state[26];

int main()
{
    int n, m, x, f = 0;
    cin >> n >> m >> x;
    char kb[n][m];
    for (int i = 0; i < n; i++)
    {
        for (int j = 0; j < m; j++)
        {
            char t;
            cin >> t;
            if (t == 'S')
                f++;
            else
            {
               state[t-'a'] = 1;
            }
            kb[i][j] = t;
        }
    }
    if (f)
    {
        for (char i = 'a'; i <= 'z'; i++)
        {
            if (state[i-'a'] > 0)
            {
                state[i-'a'] = 2;
                double min_d = 10000;
                for (int j = 0; j < n; j++)
                {
                    for (int k = 0; k < m; k++)
                    {
                        if (kb[j][k] == i)
                        {
                            for (int l = 0; l < n; l++)
                            {
                                for (int ll = 0; ll < m; ll++)
                                {
                                    if (kb[l][ll] == 'S')
                                    {

                                        double d = sqrt(pow(j-l,2.0) + pow(k-ll,2.0));
                                        min_d = min(d, min_d);
                                    }
                                }
                            }
                        }
                    }
                }
                if (min_d > x)
                    state[i-'a'] = 3;

            }
        }
    }
    int q, ans  = 0;
    string s;
    cin >> q >> s;

    for (char c : s)
    {

        if (c >= 'a' && c <= 'z')
        {
            int i = c - 'a';
            //cout << state[i] << endl;
            if (state[i] == 0)
            {
                cout << -1;
                return 0;
            }
        }else
        {
            c = tolower(c);
            int i = c - 'a';
            //cout << state[i] << endl;
            if (state[i] == 3)
                ans++;
            else if (state[i] != 2)
            {
                cout << -1;
                return 0;
            }
        }
    }

    cout << ans << endl;
}


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

bool exist;
bool cheak[30];
bool visited[30];
vector <pair<int,int> >shiftPair;
map<char, pair<int,int> >mp;
int main()
{
    int n,m,x,count=0;
    cin>>n>>m>>x;
    string s[n];
    for(int i=0; i<n; i++)
        cin>>s[i];
    for(int i=0; i<n; i++)
    {
        for(int j=0; j<m; j++)
        {
            if(s[i][j]=='S')
            {
                if(!exist)
                    exist=true;
                shiftPair.push_back(make_pair(i,j));
            }
            else
            {
                cheak[s[i][j]-97]=1;
                mp[s[i][j]]=make_pair(i,j);

            }
        }
    }
    int l2=shiftPair.size();
    int q;
    cin>>q;
    string text;
    cin>>text;
    int l=text.size();
    for(int i=0;i<l;i++)
    {
        bool flag=false;
        if(text[i]>='a' && text[i]<='z' && !cheak[text[i]-97]){
            cout<<-1<<endl;
            return 0;
        }
         if(text[i]>='A'  && text[i]<='Z' &&  (!cheak[text[i]+32-97] || !exist))
        {
        cout<<-1<<endl;
        return 0;
        }
        if(text[i]>='A' && text[i]<='Z' && exist && cheak[text[i]+32-97])
        {
            int x1=mp[text[i]+32].first;
            int y1=mp[text[i]+32].second;
            for(int i=0;i<l2;i++)
            {
                int x2=shiftPair[i].first;
                int y2=shiftPair[i].second;

                if(((x2-x1)*(x2-x1)) + ((y2-y1)*(y2-y1))<=(x*x))
                {
                    flag=true;
                    break;
                }
            }
            if(!flag)
                count++;
        }
    }
    cout<<count<<endl;
    return 0;
}


#include<bits/stdc++.h>
using namespace std;
int distance(int x1,int y1,int x2,int y2);
char s[35][35];
vector<pair<int,int> >shiftPosition;
bool isShiftExist;
bool small[26];
bool capital[26];
int main()
{
    int n,m,x,count=0;
    cin>>n>>m>>x;
    for(int i=0; i<n; i++)
    {
        for(int j=0; j<m; j++)
        {
            cin>>s[i][j];
            if(s[i][j]=='S')
            {
                isShiftExist=true;
                shiftPosition.push_back(make_pair(i,j));
            }
        }
    }
    int l=shiftPosition.size();
    for(int i=0; i<n; i++)
    {
        for(int j=0; j<m; j++)
        {
            if(s[i][j]!='S')
            {
                small[s[i][j]-97]=true;
                if(!capital[s[i][j]-32-'A'])
                {
                    for(int k=0; k<l; k++)
                    {
                        int val=distance(shiftPosition[k].first,shiftPosition[k].second,i,j);
                        if(val<=x*x)
                        {
                            capital[s[i][j]-32-'A']=true;
                            break;
                        }

                    }
                }
            }
        }
    }
    int q;
    cin>>q;
    string text;
    cin>>text;
    for(int i=0; i<q; i++)
    {
        if(text[i]>='a' && text[i]<='z' && !small[text[i]-97])
        {
            cout<<-1<<endl;
            return 0;
        }
        if(text[i]>='A' && text[i]<='Z' &&(!small[text[i]+32-97] || !isShiftExist))
        {
            cout<<-1;
            return 0;
        }
        if(text[i]>='A' && text[i]<='Z' && small[text[i]+32-97] && isShiftExist && !capital[text[i]-65])
        {
            count++;
        }

    }
    cout<<count<<endl;
    return 0;

}
int distance(int x1,int y1,int x2,int y2)
{
    return (((x1-x2)*(x1-x2))+((y1-y2)*(y1-y2)));
}


#include<bits/stdc++.h>
using namespace std;
bool cheak;
bool status[30];
vector<pair<int,int> >shift;
vector<pair<int,int> >key[30];
int main()
{
    int n,m,x,count=0,c=0;
    cin>>n>>m>>x;
    string s[n];
    for(int i=0; i<n; i++)
        cin>>s[i];
    for(int i=0; i<n; i++)
    {
        for(int j=0; j<m; j++)
        {
            if(s[i][j]=='S')
            {
                cheak=true;
                shift.push_back(make_pair(i,j));
            }
            else
            {
                status[s[i][j]-'a']=1;
                key[s[i][j]-'a'].push_back(make_pair(i,j));
            }
        }
    }
    int q;
    cin>>q;
    string text;
    cin>>text;
    int l2=shift.size();
    for(int i=0; i<q; i++)
    {
        if(text[i]>='a' && text[i]<='z' && !status[text[i]-'a'])
        {
            cout<<-1<<endl;
            return 0;
        }
        if(text[i]>='A' && text[i]<='Z' && (!cheak || !status[text[i]+32-'a']))
        {
            cout<<-1<<endl;
            return 0;
        }
        if(text[i]>='A' && text[i]<='Z' && cheak && status[text[i]+32-'a'])
        {
            bool flag=false;
            for(int k=0; k<l2; k++)
            {
                int x1=shift[k].first;
                int y1=shift[k].second;
                int l3=key[text[i]+32-'a'].size();
                for(int j=0; j<l3; j++)
                {
//                    int x1=shift[i].first;
//                    int y1=shift.[i].second;
                    int x2=key[text[i]+32-'a'][j].first;
                    int y2=key[text[i]+32-'a'][j].second;
                    if(((x2-x1)*(x2-x1)) + ((y2-y1)*(y2-y1))<=x*x)
                    {
                        flag=true;
                        break;
                    }
                }
                if(flag)
                    break;
            }
            if(!flag)
                count++;
        }

    }
    cout<<count<<endl;
//    for(int i=0;i<shift.size();i++)
//        cout<<shift[i].first<<" "<<shift[i].second<<endl;
//        cout<<"FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF"<<endl;
//    for(int i=0;i<30;i++)
//    {
//       printf("v[%d]\n",i);
//        for(int j=0;j<key[i].size();j++)
//            cout<<key[i][j].first<<" "<<key[i][j].second<<endl;
//        cout<<endl;
//    }

    return 0;
}


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

bool exist;
bool cheak[30];
vector <pair<int,int> >shiftPair;
map<char, pair<int,int> >mp;
int main()
{
    int n,m,x,count=0;
    cin>>n>>m>>x;
    string s[n];
    for(int i=0; i<n; i++)
        cin>>s[i];
    for(int i=0; i<n; i++)
    {
        for(int j=0; j<m; j++)
        {
            if(s[i][j]=='S')
            {
                if(!exist)
                    exist=true;
                shiftPair.push_back(make_pair(i,j));
            }
            else
            {
                cheak[s[i][j]-97]=1;
                mp[s[i][j]]=make_pair(i,j);

            }
        }
    }
    int l2=shiftPair.size();
    int q;
    cin>>q;
    string text;
    cin>>text;
    int l=text.size();
    for(int i=0;i<l;i++)
    {
        bool flag=false;
        if(text[i]>='a' && text[i]<='z' && !cheak[text[i]-97]){
            cout<<-1<<endl;
            return 0;
        }
         if(text[i]>='A'  && text[i]<='Z' && !exist)
        {
        cout<<-1<<endl;
        return 0;
        }
        if(text[i]>='A' && text[i]<='Z' && exist)
        {
            int x1=mp[text[i]+32].first;
            int y1=mp[text[i]+32].second;
            for(int i=0;i<l2;i++)
            {
                int x2=shiftPair[i].first;
                int y2=shiftPair[i].second;

                if(((x2-x1)*(x2-x1)) + ((y2-y1)*(y2-y1))<=(x*x))
                {
                    flag=true;
                    break;
                }
            }
            if(!flag)
                count++;
        }
    }
    cout<<count<<endl;
    return 0;
}


#include<bits/stdc++.h>
using namespace std;
bool cheak;
bool status[30];
vector<pair<int,int> >shift;
vector<pair<int,int> >key[30];
int main()
{
    int n,m,x,count=0,c=0;
    cin>>n>>m>>x;
    string s[n];
    for(int i=0; i<n; i++)
        cin>>s[i];
    for(int i=0; i<n; i++)
    {
        for(int j=0; j<m; j++)
        {
            if(s[i][j]=='S')
            {
                cheak=true;
                shift.push_back(make_pair(i,j));
            }
            else
            {
                status[s[i][j]-'a']=1;
                key[s[i][j]-'a'].push_back(make_pair(i,j));
            }
        }
    }
    int q;
    cin>>q;
    string text;
    cin>>text;
    int l=text.size();
    int l2=shift.size();
    for(int i=0; i<l; i++)
    {
        if(text[i]>='a' && text[i]<='z' && !status[text[i]-'a'])
        {
            cout<<-1<<endl;
            return 0;
        }
        if(text[i]>='A' && text[i]<='Z' && (!cheak || !status[text[i]+32-'a']))
        {
            cout<<-1<<endl;
            return 0;
        }
        if(text[i]>='A' && text[i]<='Z' && cheak && status[text[i]+32-'a'])
        {
            bool flag=false;
            for(int k=0; k<l2; k++)
            {
                int x1=shift[k].first;
                int y1=shift[k].second;
                int l3=key[text[i]+32-'a'].size();
                for(int j=0; j<l3; j++)
                {
//                    int x1=shift[i].first;
//                    int y1=shift.[i].second;
                    int x2=key[text[i]+32-'a'][j].first;
                    int y2=key[text[i]+32-'a'][j].second;
                    if(((x2-x1)*(x2-x1)) + ((y2-y1)*(y2-y1))<=x*x)
                    {
                        flag=true;
                        break;
                    }
                }
                if(flag)
                    break;
            }
            if(!flag)
                count++;
        }

    }
    cout<<count<<endl;
//    for(int i=0;i<shift.size();i++)
//        cout<<shift[i].first<<" "<<shift[i].second<<endl;
//        cout<<"FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF"<<endl;
//    for(int i=0;i<30;i++)
//    {
//       printf("v[%d]\n",i);
//        for(int j=0;j<key[i].size();j++)
//            cout<<key[i][j].first<<" "<<key[i][j].second<<endl;
//        cout<<endl;
//    }

    return 0;
}


#include<bits/stdc++.h>
using namespace std;
int distance(int x1,int y1,int x2,int y2);
char s[35][35];
char text[5*100000+10];
vector<pair<int,int> >shiftPosition;
bool isShiftExist;
bool small[26];
bool capital[26];
int main()
{
    int n,m,x,count=0;
    scanf("%d%d%d",&n,&m,&x);
    for(int i=0; i<n; i++)
    {
        for(int j=0; j<m; j++)
        {
            cin>>s[i][j];
            if(s[i][j]=='S')
            {
                isShiftExist=true;
                shiftPosition.push_back(make_pair(i,j));
            }
        }
    }
    int l=shiftPosition.size();
    for(int i=0; i<n; i++)
    {
        for(int j=0; j<m; j++)
        {
            if(s[i][j]!='S')
            {
                small[s[i][j]-97]=true;
                if(!capital[s[i][j]-32-'A'])
                {
                    for(int k=0; k<l; k++)
                    {
                        int val=distance(shiftPosition[k].first,shiftPosition[k].second,i,j);
                        if(val<=x*x)
                        {
                            capital[s[i][j]-32-'A']=true;
                            break;
                        }

                    }
                }
            }
        }
    }
    int q;
    scanf("%d",&q);
    scanf("%s",text);
    for(int i=0; i<q; i++)
    {
        if(text[i]>='a' && text[i]<='z' && !small[text[i]-97])
        {
            puts("-1");
            return 0;
        }
        if(text[i]>='A' && text[i]<='Z' &&(!small[text[i]+32-97] || !isShiftExist))
        {
            puts("-1");
            return 0;
        }
        if(text[i]>='A' && text[i]<='Z' && small[text[i]+32-97] && isShiftExist && !capital[text[i]-65])
        {
            count++;
        }

    }
    printf("%d\n",count);
    return 0;

}
int distance(int x1,int y1,int x2,int y2)
{
    return (((x1-x2)*(x1-x2))+((y1-y2)*(y1-y2)));
}


#include <bits\stdc++.h>

using namespace std;

#define sz(c)	(int)(c).size()
#define all(c)	(c).begin(), (c).end()

int n, m;

inline double dis(int r1, int c1, int r2, int c2) { return sqrt(pow(r1 - r2, 2) + pow(c1 - c2, 2)); }

int oo = int(1e9);

int main()
{

	int d;
	cin >> n >> m >> d;

	vector< pair<int, int> > shifts;
	multimap<char, pair<int, int> > keys;

	for (int i = 0; i < n; ++i)
	{
		for (int j = 0; j < m; ++j)
		{
			char key;
			cin >> key;
			if (key == 'S')
			{
				shifts.push_back({ i, j });
				continue;
			}
			keys.insert({ key, {i, j} });
		}
	}

	int l;
	string t;
	cin >> l >> t;

	bool found_upper = false;

	for (int i = 0; i < l; ++i)
	{
		if (isupper(t[i]))
		{
			found_upper = true;
			break;
		}
	}

	if (found_upper && sz(shifts) == 0)
	{
		cout << -1;
		return 0;
	}

	for (int i = 0; i < l; ++i)
	{
		char ch = tolower(t[i]);
		if (keys.find(ch) == keys.end())
		{
			cout << -1;
			return 0;
		}
	}

	int ans = 0;

	for (int i = 0; i < l; ++i)
	{
		if (isupper(t[i]))
		{
			char ch = tolower(t[i]);
			double min_dis = oo;

			for (int j = 0; j < sz(shifts); ++j)
			{

				int r2 = shifts[j].first;
				int c2 = shifts[j].second;

				for (auto it : keys)
				{
					if (it.first != ch)
						continue;

					int r1 = it.second.first, c1 = it.second.second;
					double k = dis(r1, c1, r2, c2);
					if (k < min_dis)
						min_dis = k;
				}

			}
			if (min_dis > d)
				++ans;
		}
	}

	cout << ans << endl;

	return 0;
}


#include <bits\stdc++.h>

using namespace std;

#define sz(c)	(int)(c).size()
#define all(c)	(c).begin(), (c).end()

int n, m;

inline double dis(int r1, int c1, int r2, int c2) { return sqrt(pow(r1 - r2, 2) + pow(c1 - c2, 2)); }

int oo = int(1e9);

int main()
{

	int d;
	cin >> n >> m >> d;

	vector< pair<int, int> > shifts;
	multimap<char, pair<int, int> > keys;

	for (int i = 0; i < n; ++i)
	{
		for (int j = 0; j < m; ++j)
		{
			char key;
			cin >> key;
			if (key == 'S')
			{
				shifts.push_back({ i, j });
				continue;
			}
			keys.insert({ key, {i, j} });
		}
	}

	int l;
	string t;
	cin >> l >> t;

	bool found_upper = false;

	for (int i = 0; i < l; ++i)
	{
		if (isupper(t[i]))
		{
			found_upper = true;
			break;
		}
	}

	if (found_upper && sz(shifts) == 0)
	{
		cout << -1;
		return 0;
	}

	for (int i = 0; i < l; ++i)
	{
		char ch = tolower(t[i]);
		if (keys.find(ch) == keys.end())
		{
			cout << -1;
			return 0;
		}
	}

	int ans = 0;

	for (int i = 0; i < l; ++i)
	{
		if (isupper(t[i]))
		{
			char ch = tolower(t[i]);
			bool check = false;

			for (int j = 0; j < sz(shifts); ++j)
			{

				int r2 = shifts[j].first;
				int c2 = shifts[j].second;

				auto range = keys.equal_range(ch);

				for (auto it = range.first; it != range.second; ++it)
				{
					int r1 = it->second.first;
					int c1 = it->second.second;

					double k = dis(r1, c1, r2, c2);

					if (k <= d)
					{
						check = true;
						break;
					}
				}

				if (check) break;
			}

			if (!check) ++ans;
		}
	}

	cout << ans << endl;

	return 0;
}


#include <bits\stdc++.h>

using namespace std;

#define sz(c)	(int)(c).size()
#define all(c)	(c).begin(), (c).end()

inline double dis(int r1, int c1, int r2, int c2) { return sqrt(pow(r1 - r2, 2) + pow(c1 - c2, 2)); }

int oo = int(1e9);

int main()
{

	int n, m, d;
	cin >> n >> m >> d;

	vector< pair<int, int> > shifts;

	map< char, vector<pair<int, int>> > keys;

	for (int i = 0; i < n; ++i)
	{
		for (int j = 0; j < m; ++j)
		{
			char key;
			cin >> key;

			if (key == 'S')
			{
				shifts.push_back({ i, j });
			}
			else
			{
				keys[key].push_back({ i, j });
			}

		}
	}

	int l;
	string t;
	cin >> l >> t;

	bool found_upper = false;

	for (int i = 0; i < l; ++i)
	{
		if (isupper(t[i]))
		{
			found_upper = true;
			break;
		}
	}

	if (found_upper && sz(shifts) == 0)
	{
		cout << -1;
		return 0;
	}

	for (int i = 0; i < l; ++i)
	{
		char ch = tolower(t[i]);
		if (keys.find(ch) == keys.end())
		{
			cout << -1;
			return 0;
		}
	}

	int ans = 0;

	for (int i = 0; i < l; ++i)
	{
		if (isupper(t[i]))
		{
			char ch = tolower(t[i]);
			bool check = false;

			for (int j = 0; j < sz(shifts); ++j)
			{

				int r2 = shifts[j].first;
				int c2 = shifts[j].second;

				vector<pair<int, int>> &tmp = keys[ch];

				for (int tmpi = 0; tmpi < sz(tmp); ++tmpi)
				{
					int r1 = tmp[tmpi].first;
					int c1 = tmp[tmpi].second;

					double k = dis(r1, c1, r2, c2);

					if (k <= d)
					{
						check = true;
						break;
					}

				}

				if (check) break;
			}

			if (!check) ++ans;
		}
	}

	cout << ans << endl;
	return 0;
}


#include <bits\stdc++.h>

using namespace std;

#define sz(c)	(int)(c).size()
#define all(c)	(c).begin(), (c).end()

int n, m;

inline double dis(int r1, int c1, int r2, int c2) { return sqrt(pow(r1 - r2, 2) + pow(c1 - c2, 2)); }

int oo = int(1e9);

int main()
{

	int d;
	cin >> n >> m >> d;

	vector< pair<int, int> > shifts;
	multimap<char, pair<int, int> > keys;

	for (int i = 0; i < n; ++i)
	{
		for (int j = 0; j < m; ++j)
		{
			char key;
			cin >> key;
			if (key == 'S')
			{
				shifts.push_back({ i, j });
				continue;
			}
			keys.insert({ key, {i, j} });
		}
	}

	int l;
	string t;
	cin >> l >> t;

	bool found_upper = false;

	for (int i = 0; i < l; ++i)
	{
		if (isupper(t[i]))
		{
			found_upper = true;
			break;
		}
	}

	if (found_upper && sz(shifts) == 0)
	{
		cout << -1;
		return 0;
	}

	for (int i = 0; i < l; ++i)
	{
		char ch = tolower(t[i]);
		if (keys.find(ch) == keys.end())
		{
			cout << -1;
			return 0;
		}
	}

	int ans = 0;

	for (int i = 0; i < l; ++i)
	{
		if (isupper(t[i]))
		{
			char ch = tolower(t[i]);
			double min_dis = oo;

			for (int j = 0; j < sz(shifts); ++j)
			{

				int r2 = shifts[j].first;
				int c2 = shifts[j].second;

				auto range = keys.equal_range(ch);

				for (auto it = range.first; it != range.second; ++it)
				{
					int r1 = it->second.first;
					int c1 = it->second.second;

					double k = dis(r1, c1, r2, c2);

					if (k < min_dis)
						min_dis = k;
				}

			}
			if (min_dis > d)
				++ans;
		}
	}

	cout << ans << endl;

	return 0;
}


#include <bits\stdc++.h>

using namespace std;

#define sz(c)	(int)(c).size()
#define all(c)	(c).begin(), (c).end()

inline double dis(int r1, int c1, int r2, int c2) { return sqrt(pow(r1 - r2, 2) + pow(c1 - c2, 2)); }

int oo = int(1e9);

int main()
{

	int n, m, d;
	cin >> n >> m >> d;

	vector< pair<int, int> > shifts;
	map<char, pair<int, int> > keys;

	for (int i = 0; i < n; ++i)
	{
		for (int j = 0; j < m; ++j)
		{
			char key;
			cin >> key;
			if (key == 'S')
			{
				shifts.push_back({ i, j });
				continue;
			}
			keys[key] = { i, j };
		}
	}

	int l;
	string t;
	cin >> l >> t;

	bool found_upper = false;

	for (int i = 0; i < l; ++i)
	{
		if (isupper(t[i]))
		{
			found_upper = true;
			break;
		}
	}

	if (found_upper && sz(shifts) == 0)
	{
		cout << -1;
		return 0;
	}

	for (int i = 0; i < l; ++i)
	{
		char ch = tolower(t[i]);
		if (keys.find(ch) == keys.end())
		{
			cout << -1;
			return 0;
		}
	}

	int ans = 0;

	for (int i = 0; i < l; ++i)
	{
		if (isupper(t[i]))
		{
			char ch = tolower(t[i]);
			int r1 = keys[ch].first;
			int c1 = keys[ch].second;
			double min_dis = oo;

			for (int j = 0; j < sz(shifts); ++j)
			{

				int r2 = shifts[j].first;
				int c2 = shifts[j].second;

				double k = dis(r1, c1, r2, c2);
				if (k <= min_dis)
					min_dis = k;
			}
			if (min_dis > d)
				++ans;
		}
	}

	cout << ans << endl;

	return 0;
}


#include <bits/stdc++.h>

using namespace std;

#define sz(c)		  (int)(c).size()
#define all(c)		  ((c).begin()), ((c).end())
#define rep(c, s, n)  for (int (c) = (s); (c) < (n); ++(c))
#define repd(c, n, s) for (int (c) = (n); (c) > (s); --(c))

const int oo = (int)1e9+9;
typedef long long ll;

int dis(int x0, int y0, int x1, int y1)
{
	int dx = x0 - x1;
	int dy = y0 - y1;
	return dx*dx + dy*dy;
}

int main()
{

	int n, m, x;
	cin >> n >> m >> x;

	map<char, vector<pair<int, int>>> keys;
	vector< pair<int, int> > shifts;
	map<char, bool> dp;

	rep(i, 0, n)
	{
		rep(j, 0, m)
		{
			char c;
			cin >> c;

			if (c == 'S')
				shifts.push_back({i, j});
			else
				keys[c].push_back({i, j});
		}
	}

	int q;
	cin >> q;

	string s;
	cin >> s;

	rep(i, 0, q)
	{
		char c = tolower(s[i]);
		if ((isupper(s[i]) && sz(shifts) == 0) || keys.find(c) == keys.end())
		{
			cout << -1;
			return 0;
		}
	}

	for (const auto& key : keys)
	{
		const char& c = key.first;
		bool ok = false;

		rep(i, 0, sz(key.second))
		{
			if (ok) break;
			const pair<int, int> &pos = key.second[i];
			rep(j, 0, sz(shifts))
			{
				const pair<int, int> &tar = shifts[j];
				int d = dis(pos.first, pos.second, tar.first, tar.second);
				if (d <= x*x)
				{
					ok = true;
					break;
				}
			}
		}

		dp[c] = ok;
	}

	int ans = 0;

	rep(i, 0, q)
	{
		if (islower(s[i])) continue;
		char c = tolower(s[i]);
		if (!dp[c])
			++ans;
	}

	cout << ans;
}


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

#define IOS ios::sync_with_stdio(0); cin.tie(0); cout.tie(0);
#define endl "\n"
#define int long long
#define all(v) v.begin(),v.end()

void INPUT() {
	#ifndef ONLINE_JUDGE
    freopen("input.txt", "r", stdin);
    freopen("output.txt", "w", stdout);
    #endif
}

vector <pair<int,int>> v[26],shift;
int n,m,x;

int check(int c) {
	for(auto it : v[c]) {
		int a=it.first;
		int b=it.second;
		for(auto i : shift) {
		int d=ceil(sqrt(pow(i.first-a,2)+pow(i.second-b,2)));
		if (d<=x) return 0;
	  }
	}
	return 1;
}

int32_t main(){
    IOS INPUT();
    cin>>n>>m>>x;
    for(int i=0; i<n; i++) {
    	for(int j=0; j<m; j++) {
    		char a;
    		cin>>a;
    		if (a=='S') shift.push_back({i,j});
    		else v[a-'a'].push_back({i,j});
    	}
    }
    int q;
    cin>>q;
    	string s;
    	cin>>s;
    	int ans=0,cnt=0;
    	for(int i=0; s[i]; i++) {
    		int c=s[i]-(s[i]>='A' && s[i]<='Z' ? 'A' : 'a');
    		if (v[c].size()==0) {
    			ans=-1;
    			break;
    		}
    		if (s[i]>='A' && s[i]<='Z') {
    			cnt++;
    			int p=check(c);
    			ans+=p;
    		}
    	}
    	if ((cnt && !shift.size())) ans=-1;
    	cout<<ans<<endl;
    return 0;
}


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

#define IOS ios::sync_with_stdio(0); cin.tie(0); cout.tie(0);
#define endl "\n"
#define int long long
#define all(v) v.begin(),v.end()

void INPUT() {
	#ifndef ONLINE_JUDGE
    freopen("input.txt", "r", stdin);
    freopen("output.txt", "w", stdout);
    #endif
}

vector <pair<int,int>> v[26],shift;
vector <int> dis(26);
int n,m,x;

int32_t main(){
    IOS INPUT();
    cin>>n>>m>>x;
    for(int i=0; i<n; i++) {
    	for(int j=0; j<m; j++) {
    		char a;
    		cin>>a;
    		if (a=='S') shift.push_back({i,j});
    		else v[a-'a'].push_back({i,j});
    	}
    }
    for(int i=0; i<26; i++) {
    	int mn=LLONG_MAX;
    		for(auto it : v[i]) {
    			int a=it.first;
    			int b=it.second;
    			for(auto jt : shift) {
    				int d=ceil(sqrt(pow(jt.first-a,2)+pow(jt.second-b,2)));
    				mn=min(mn,d);
    			}
    		}
    	dis[i]=mn;
    }
    int q,ans=0,cnt=0;
    cin>>q;
    string s;
    cin>>s;
    for(int i=0; s[i]; i++) {
    	int c=s[i]-(s[i]>='A' && s[i]<='Z' ? 'A' : 'a');
    	if (v[c].size()==0) {
    		ans=-1;
    		break;
    	}
    	if (s[i]>='A' && s[i]<='Z') {
    		if (dis[c]>x) ans++;
    		cnt++;
    	}
    }
    if (cnt && !shift.size()) ans=-1;
    cout<<ans<<endl;
    return 0;
}


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

#define IOS ios::sync_with_stdio(0); cin.tie(0); cout.tie(0);
#define endl "\n"
#define int long long
#define all(v) v.begin(),v.end()

void INPUT() {
	#ifndef ONLINE_JUDGE
    freopen("input.txt", "r", stdin);
    freopen("output.txt", "w", stdout);
    #endif
}

vector <pair<int,int>> v(26,{-1,-1}),shift;
int n,m,x;

int check(int c) {
	int a=v[c].first;
	int b=v[c].second;
	for(auto i : shift) {
		int d=ceil(sqrt(pow(i.first-a,2)+pow(i.second-b,2)));
		if (d<=x) return 0;
	}
	return 1;
}

int32_t main(){
    IOS INPUT();
    cin>>n>>m>>x;
    for(int i=0; i<n; i++) {
    	for(int j=0; j<m; j++) {
    		char a;
    		cin>>a;
    		if (a=='S') shift.push_back({i,j});
    		else v[a-'a']={i,j};
    	}
    }
    int q;
    cin>>q;
    	string s;
    	cin>>s;
    	int ans=0,cnt=0;
    	for(int i=0; s[i]; i++) {
    		int c=s[i]-(s[i]>='A' && s[i]<='Z' ? 'A' : 'a');
    		if (v[c].first==-1) ans=-1;
    		else if (s[i]>='A' && s[i]<='Z') {
    			cnt++;
    			int p=check(c);
    			ans+=p;
    		}
    	}
    	if ((cnt && !shift.size())) ans=-1;
    	cout<<ans<<endl;
    return 0;
}


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

#define IOS ios::sync_with_stdio(0); cin.tie(0); cout.tie(0);
#define endl "\n"
#define int long long
#define all(v) v.begin(),v.end()

void INPUT() {
	#ifndef ONLINE_JUDGE
    freopen("input.txt", "r", stdin);
    freopen("output.txt", "w", stdout);
    #endif
}

vector <pair<int,int>> v[26],shift;
int n,m,x;

int check(int c) {
	int lo=0,hi=v[c].size()-1;
	while (lo<=hi) {
		int mid=lo+(hi-lo)/2;
		int a=v[c][mid].first,b=v[c][mid].second;
		for(auto i : shift) {
			int d=ceil(sqrt(pow(i.first-a,2)+pow(i.second-b,2)));
			if (d<=x) return 0;
		}
		lo=mid+1;
	}
	return 1;
}

int32_t main(){
    IOS INPUT();
    cin>>n>>m>>x;
    for(int i=0; i<n; i++) {
    	for(int j=0; j<m; j++) {
    		char a;
    		cin>>a;
    		if (a=='S') shift.push_back({i,j});
    		else v[a-'a'].push_back({i,j});
    	}
    }
    for(int i=0; i<26; i++) sort(all(v[i]));
    int q;
    cin>>q;
    	string s;
    	cin>>s;
    	int ans=0,cnt=0;
    	for(int i=0; s[i]; i++) {
    		int c=s[i]-(s[i]>='A' && s[i]<='Z' ? 'A' : 'a');
    		if (v[c].size()==0) {
    			ans=-1;
    			break;
    		}
    		if (s[i]>='A' && s[i]<='Z') {
    			cnt++;
    			int p=check(c);
    			ans+=p;
    		}
    	}
    	if ((cnt && !shift.size())) ans=-1;
    	cout<<ans<<endl;
    return 0;
}


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

#define IOS ios::sync_with_stdio(0); cin.tie(0); cout.tie(0);
#define endl "\n"
#define int long long
#define all(v) v.begin(),v.end()

void INPUT() {
	#ifndef ONLINE_JUDGE
    freopen("input.txt", "r", stdin);
    freopen("output.txt", "w", stdout);
    #endif
}

vector <pair<int,int>> v(26,{-1,-1}),shift;
int n,m,x;

int check(int c) {
	int a=v[c].first;
	int b=v[c].second;
	for(auto i : shift) {
		int d=ceil(sqrt(pow(i.first-a,2)+pow(i.second-b,2)));
		if (d<=x) return 0;
	}
	return 1;
}

int32_t main(){
    IOS INPUT();
    cin>>n>>m>>x;
    for(int i=0; i<n; i++) {
    	for(int j=0; j<m; j++) {
    		char a;
    		cin>>a;
    		if (a=='S') shift.push_back({i,j});
    		else v[a-'a']={i,j};
    	}
    }
    int q;
    cin>>q;
    	string s;
    	cin>>s;
    	int ans=0,cnt=0;
    	for(int i=0; s[i]; i++) {
    		int c=s[i]-(s[i]>='A' && s[i]<='Z' ? 'A' : 'a');
    		if (v[c].first==-1) {
    			ans=-1;
    			break;
    		}
    		if (s[i]>='A' && s[i]<='Z') {
    			cnt++;
    			int p=check(c);
    			ans+=p;
    		}
    	}
    	if ((cnt && !shift.size())) ans=-1;
    	cout<<ans<<endl;
    return 0;
}


#include <bits/stdc++.h>
using namespace std;
#define ll long long
#define Pi 3.141592653589793238
#define all(v) (v).begin(), (v).end()
#define rall(v) (v).rbegin(), (v).rend()
#define f first
#define s second
#define mp make_pair
int dx[] = {0, 0, -1, 1};
int dy[] = {1, 1, 0, 0};
char keyboard[31][31];
vector<pair <int, int> > shi;
double c[26];
int main()
{
#ifndef ONLINE_JUDGE
    freopen("in.txt", "r", stdin);
    //freopen("out.txt", "w", stdout);
#endif // OnLINE_JUDGE
    int n, m , x, q;
    fill(c, c+26, -1);
    cin >> n >> m >> x;
    string text ;
    bool ex = false, u=false;;
    for(int i=1 ; i<=n; i++)
    {
        for(int j=1; j<=m ; j++)
        {
            cin >> keyboard[i][j];
            if(keyboard[i][j]=='S')
            {
                ex = true;
                shi.push_back({i, j});
            }
        }
    }
    cin >> q >> text;
    for(int i=1 ; i<=n; i++)
    {
        for(int j=1; j<=m ; j++)
        {
            if(keyboard[i][j]!='S')
            {
                double mi = 1e5;
                for(int k=0 ; k<shi.size(); k++)
                {
                    double ec = sqrt((i-shi[k].f)*(i-shi[k].f)+(j-shi[k].s)*(j-shi[k].s));

                    mi = min(mi, ec);
                }
                if(mi!=1e5)
                {
                    if(c[keyboard[i][j]-'a'] != -1)
                        c[keyboard[i][j]-'a'] = min(mi, c[keyboard[i][j]-'a']);
                    else
                        c[keyboard[i][j]-'a'] = mi;
                }
                else
                    c[keyboard[i][j]-'a'] = 0;

            }
        }
    }

    int ans = 0;
    for(int i=0 ; i<q; i++)
    {
        if(isupper(text[i]))
            u = true;
        //cout << text[i] << " " << c[text[i]-'A'] << endl;
        if((isupper(text[i]) && c[text[i]-'A']==-1) || (islower(text[i]) && c[text[i]-'a']==-1))
        {
            cout << "-1";
            return 0;
        }
        if(isupper(text[i]) && c[text[i]-'A']>x)
            ans++;
        if(u && !ex)
        {
            cout << "-1";
            return 0;
        }
    }
    cout << ans << endl;

    return 0;
}


#include <bits/stdc++.h>
using namespace std;
#define ll long long
#define Pi 3.141592653589793238
#define all(v) (v).begin(), (v).end()
#define rall(v) (v).rbegin(), (v).rend()
#define f first
#define s second
#define mp make_pair
int dx[] = {0, 0, -1, 1};
int dy[] = {1, 1, 0, 0};
char keyboard[31][31];
vector<pair <int, int> > shi;
double c[26];
int main()
{
#ifndef ONLINE_JUDGE
    freopen("in.txt", "r", stdin);
    //freopen("out.txt", "w", stdout);
#endif // OnLINE_JUDGE
    int n, m , x, q;
    memset(c, 60, sizeof(c));
    cin >> n >> m >> x;
    string text ;
    bool ex = true;
    for(int i=1 ; i<=n; i++)
    {
        for(int j=1; j<=m ; j++)
        {
            cin >> keyboard[i][j];
            if(keyboard[i][j]=='S')
            {
                ex = true;
                shi.push_back({i, j});
            }
        }
    }
    cin >> q >> text;

    for(int i=1 ; i<=n; i++)
    {
        for(int j=1; j<=m ; j++)
        {
            if(keyboard[i][j]!='S')
            {
                double mi = 1e5;
                for(int k=0 ; k<shi.size(); k++)
                {
                    double ec = sqrt((i-shi[k].f)*(i-shi[k].f)+(j-shi[k].s)*(j-shi[k].s));

                    mi = min(mi, ec);
                }
                c[keyboard[i][j]-'a'] = mi;
            }
        }
    }

    int ans = 0;
    for(int i=0 ; i<q; i++)
    {
        if((isupper(text[i]) && c[text[i]-'A']=='-1') || (islower(text[i]) && c[text[i]-'a']=='-1'))
        {
            ex = false;
            break;
        }
        if(isupper(text[i]) && c[text[i]-'A']>x)
            ans++;
    }
    if(!ex)
        cout << "-1\n";
    else
        cout << ans << endl;

    return 0;
}


#include <bits/stdc++.h>
using namespace std;
#define ll long long
#define Pi 3.141592653589793238
#define all(v) (v).begin(), (v).end()
#define rall(v) (v).rbegin(), (v).rend()
#define f first
#define s second
#define mp make_pair
int dx[] = {0, 0, -1, 1};
int dy[] = {1, 1, 0, 0};
char keyboard[31][31];
vector<pair <int, int> > shi;
double c[26];
int main()
{
#ifndef ONLINE_JUDGE
    freopen("in.txt", "r", stdin);
    //freopen("out.txt", "w", stdout);
#endif // OnLINE_JUDGE
    int n, m , x, q;
    memset(c, 60, sizeof(c));
    cin >> n >> m >> x;
    string text ;
    bool ex = false, u=false;;
    for(int i=1 ; i<=n; i++)
    {
        for(int j=1; j<=m ; j++)
        {
            cin >> keyboard[i][j];
            if(keyboard[i][j]=='S')
            {
                ex = true;
                shi.push_back({i, j});
            }
            if(isupper(keyboard[i][j]))
                u = true;
        }
    }
    cin >> q >> text;
    ex = true;
    if(u && !ex)
    {
        cout << "-1";
        return 0;
    }
    for(int i=1 ; i<=n; i++)
    {
        for(int j=1; j<=m ; j++)
        {
            if(keyboard[i][j]!='S')
            {
                double mi = 1e5;
                for(int k=0 ; k<shi.size(); k++)
                {
                    double ec = sqrt((i-shi[k].f)*(i-shi[k].f)+(j-shi[k].s)*(j-shi[k].s));

                    mi = min(mi, ec);
                }
                c[keyboard[i][j]-'a'] = mi;
            }
        }
    }

    int ans = 0;
    for(int i=0 ; i<q; i++)
    {
        if((isupper(text[i]) && c[text[i]-'A']=='-1') || (islower(text[i]) && c[text[i]-'a']=='-1'))
        {
            ex = false;
            break;
        }
        if(isupper(text[i]) && c[text[i]-'A']>x)
            ans++;
    }
    if(!ex)
        cout << "-1\n";
    else
        cout << ans << endl;

    return 0;
}


#include <bits/stdc++.h>
using namespace std;
#define ll long long
#define Pi 3.141592653589793238
#define all(v) (v).begin(), (v).end()
#define rall(v) (v).rbegin(), (v).rend()
#define f first
#define s second
#define mp make_pair
int dx[] = {0, 0, -1, 1};
int dy[] = {1, 1, 0, 0};
char keyboard[31][31];
vector<pair <int, int> > shi;
double c[26];
int main()
{
#ifndef ONLINE_JUDGE
    freopen("in.txt", "r", stdin);
    //freopen("out.txt", "w", stdout);
#endif // OnLINE_JUDGE
    int n, m , x, q;
    memset(c, 60, sizeof(c));
    cin >> n >> m >> x;
    string text ;
    bool ex = false, u=false;;
    for(int i=1 ; i<=n; i++)
    {
        for(int j=1; j<=m ; j++)
        {
            cin >> keyboard[i][j];
            if(keyboard[i][j]=='S')
            {
                ex = true;
                shi.push_back({i, j});
            }
        }
    }
    cin >> q >> text;
    for(int i=1 ; i<=n; i++)
    {
        for(int j=1; j<=m ; j++)
        {
            if(keyboard[i][j]!='S')
            {
                double mi = 1e5;
                for(int k=0 ; k<shi.size(); k++)
                {
                    double ec = sqrt((i-shi[k].f)*(i-shi[k].f)+(j-shi[k].s)*(j-shi[k].s));

                    mi = min(mi, ec);
                }
                c[keyboard[i][j]-'a'] = mi;
            }
        }
    }

    int ans = 0;
    for(int i=0 ; i<q; i++)
    {
        if(isupper(text[i]))
            u = true;
        if((isupper(text[i]) && c[text[i]-'A']=='-1') || (islower(text[i]) && c[text[i]-'a']=='-1'))
        {
            ex = false;
            break;
        }
        if(isupper(text[i]) && c[text[i]-'A']>x)
            ans++;
        if(u && !ex)
        {
            cout << "-1";
            return 0;
        }
    }
    cout << ans << endl;

    return 0;
}


#include <bits/stdc++.h>
using namespace std;
#define ll long long
#define Pi 3.141592653589793238
#define all(v) (v).begin(), (v).end()
#define rall(v) (v).rbegin(), (v).rend()
#define f first
#define s second
#define mp make_pair
int dx[] = {0, 0, -1, 1};
int dy[] = {1, 1, 0, 0};
char keyboard[31][31];
vector<pair <int, int> > shi;
double c[26];
int main()
{
#ifndef ONLINE_JUDGE
    freopen("in.txt", "r", stdin);
    //freopen("out.txt", "w", stdout);
#endif // OnLINE_JUDGE
    int n, m , x, q;
    fill(c, c+26, -1);
    cin >> n >> m >> x;
    string text ;
    bool ex = false, u=false;;
    for(int i=1 ; i<=n; i++)
    {
        for(int j=1; j<=m ; j++)
        {
            cin >> keyboard[i][j];
            if(keyboard[i][j]=='S')
            {
                ex = true;
                shi.push_back({i, j});
            }
        }
    }
    cin >> q >> text;
    for(int i=1 ; i<=n; i++)
    {
        for(int j=1; j<=m ; j++)
        {
            if(keyboard[i][j]!='S')
            {
                double mi = 1e5;
                for(int k=0 ; k<shi.size(); k++)
                {
                    double ec = sqrt((i-shi[k].f)*(i-shi[k].f)+(j-shi[k].s)*(j-shi[k].s));

                    mi = min(mi, ec);
                }
                if(mi!=1e5)
                    c[keyboard[i][j]-'a'] = mi;
            }
        }
    }

    int ans = 0;
    for(int i=0 ; i<q; i++)
    {
        if(isupper(text[i]))
            u = true;
        if((isupper(text[i]) && c[text[i]-'A']=='-1') || (islower(text[i]) && c[text[i]-'a']==-1))
        {
            cout << "-1";
            return 0;
        }
        if(isupper(text[i]) && c[text[i]-'A']>x)
            ans++;
        if(u && !ex)
        {
            cout << "-1";
            return 0;
        }
    }
    cout << ans << endl;

    return 0;
}


#include <bits/stdc++.h>
using namespace std;
#define ll long long
#define Pi 3.141592653589793238
#define all(v) (v).begin(), (v).end()
#define rall(v) (v).rbegin(), (v).rend()
#define f first
#define s second
#define mp make_pair
int dx[] = {0, 0, -1, 1};
int dy[] = {1, 1, 0, 0};
char keyboard[31][31];
vector<pair <int, int> > shi;
double c[26];
int main()
{
#ifndef ONLINE_JUDGE
    freopen("in.txt", "r", stdin);
    //freopen("out.txt", "w", stdout);
#endif // OnLINE_JUDGE
    int n, m , x, q;
    fill(c, c+26, -1);
    cin >> n >> m >> x;
    string text ;
    bool ex = false, u=false;;
    for(int i=1 ; i<=n; i++)
    {
        for(int j=1; j<=m ; j++)
        {
            cin >> keyboard[i][j];
            if(keyboard[i][j]=='S')
            {
                ex = true;
                shi.push_back({i, j});
            }
        }
    }
    cin >> q >> text;
    for(int i=1 ; i<=n; i++)
    {
        for(int j=1; j<=m ; j++)
        {
            if(keyboard[i][j]!='S')
            {
                double mi = 1e5;
                for(int k=0 ; k<shi.size(); k++)
                {
                    double ec = sqrt((i-shi[k].f)*(i-shi[k].f)+(j-shi[k].s)*(j-shi[k].s));

                    mi = min(mi, ec);
                }
                if(mi!=1e5)
                {
                    if(c[keyboard[i][j]-'a'] != -1)
                        c[keyboard[i][j]-'a'] = min(mi, c[keyboard[i][j]-'a']);
                    else
                        c[keyboard[i][j]-'a'] = mi;
                }
                else
                    c[keyboard[i][j]-'a'] = 0;

            }
        }
    }

    int ans = 0;
    for(int i=0 ; i<q; i++)
    {
        if(isupper(text[i]))
            u = true;
       // cout << text[i] << " " << c[text[i]-'a'] << endl;
        if((isupper(text[i]) && c[text[i]-'A']=='-1') || (islower(text[i]) && c[text[i]-'a']==-1))
        {
            cout << "-1";
            return 0;
        }
        if(isupper(text[i]) && c[text[i]-'A']>x)
            ans++;
        if(u && !ex)
        {
            cout << "-1";
            return 0;
        }
    }
    cout << ans << endl;

    return 0;
}


#include <bits/stdc++.h>
using namespace std;
#define ll long long
#define Pi 3.141592653589793238
#define all(v) (v).begin(), (v).end()
#define rall(v) (v).rbegin(), (v).rend()
#define f first
#define s second
#define mp make_pair
int dx[] = {0, 0, -1, 1};
int dy[] = {1, 1, 0, 0};
char keyboard[31][31];
vector<pair <int, int> > shi;
double c[26];
int main()
{
#ifndef ONLINE_JUDGE
    freopen("in.txt", "r", stdin);
    //freopen("out.txt", "w", stdout);
#endif // OnLINE_JUDGE
    int n, m , x, q;
    memset(c, 60, sizeof(c));
    cin >> n >> m >> x;
    string text ;
    bool ex = false, u=false;;
    for(int i=1 ; i<=n; i++)
    {
        for(int j=1; j<=m ; j++)
        {
            cin >> keyboard[i][j];
            if(keyboard[i][j]=='S')
            {
                ex = true;
                shi.push_back({i, j});
            }
        }
    }
    cin >> q >> text;
    for(int i=1 ; i<=n; i++)
    {
        for(int j=1; j<=m ; j++)
        {
            if(keyboard[i][j]!='S')
            {
                double mi = 1e5;
                for(int k=0 ; k<shi.size(); k++)
                {
                    double ec = sqrt((i-shi[k].f)*(i-shi[k].f)+(j-shi[k].s)*(j-shi[k].s));

                    mi = min(mi, ec);
                }
                c[keyboard[i][j]-'a'] = mi;
            }
        }
    }

    int ans = 0;
    for(int i=0 ; i<q; i++)
    {
        if(isupper(text[i]))
            u = true;
        if((isupper(text[i]) && c[text[i]-'A']=='-1') || (islower(text[i]) && c[text[i]-'a']=='-1'))
        {
            ex = false;
            break;
        }
        if(isupper(text[i]) && c[text[i]-'A']>x)
            ans++;
        if(u && !ex)
        {
            cout << "-1";
            return 0;
        }
    }
    if(!ex)
        cout << "-1\n";
    else
        cout << ans << endl;

    return 0;
}


#include <bits/stdc++.h>
using namespace std;
#define ll long long
#define Pi 3.141592653589793238
#define all(v) (v).begin(), (v).end()
#define rall(v) (v).rbegin(), (v).rend()
#define f first
#define s second
#define mp make_pair
int dx[] = {0, 0, -1, 1};
int dy[] = {1, 1, 0, 0};
char keyboard[31][31];
vector<pair <int, int> > shi;
double c[26];
int main()
{
#ifndef ONLINE_JUDGE
    freopen("in.txt", "r", stdin);
    //freopen("out.txt", "w", stdout);
#endif // OnLINE_JUDGE
    int n, m , x, q;
    memset(c, 60, sizeof(c));
    cin >> n >> m >> x;
    string text ;
    bool ex = false;
    for(int i=1 ; i<=n; i++)
    {
        for(int j=1; j<=m ; j++)
        {
            cin >> keyboard[i][j];
            if(keyboard[i][j]=='S')
            {
                ex = true;
                shi.push_back({i, j});
            }
        }
    }
    cin >> q >> text;
    if(!ex)
        cout << "-1\n";
    else
    {
        for(int i=1 ; i<=n; i++)
        {
            for(int j=1; j<=m ; j++)
            {
                double mi = 1e5;
                for(int k=0 ; k<shi.size(); k++)
                {
                    double ec = sqrt((i-shi[k].f)*(i-shi[k].f)+(j-shi[k].s)*(j-shi[k].s));

                    mi = min(mi, ec);
                }
                c[keyboard[i][j]-'a'] = mi;
            }
        }

        int ans = 0;
        for(int i=0 ; i<q; i++)
        {
            if((isupper(text[i]) && c[text[i]-'A']=='-1') || (islower(text[i]) && c[text[i]-'a']=='-1'))
            {
                ex = false;
                break;
            }
            if(isupper(text[i]) && c[text[i]-'A']>x)
                ans++;
        }
        if(!ex)
            cout << "-1\n";
        else
            cout << ans << endl;

    }

    return 0;
}


#include <bits/stdc++.h>
using namespace std;
#define ll long long
#define Pi 3.141592653589793238
#define all(v) (v).begin(), (v).end()
#define rall(v) (v).rbegin(), (v).rend()
#define f first
#define s second
#define mp make_pair
int dx[] = {0, 0, -1, 1};
int dy[] = {1, 1, 0, 0};
char keyboard[31][31];
vector<pair <int, int> > shi;
double c[26];
int main()
{
#ifndef ONLINE_JUDGE
    freopen("in.txt", "r", stdin);
    //freopen("out.txt", "w", stdout);
#endif // OnLINE_JUDGE
    int n, m , x, q;
    memset(c, 60, sizeof(c));
    cin >> n >> m >> x;
    string text ;
    bool ex = false;
    for(int i=1 ; i<=n; i++)
    {
        for(int j=1; j<=m ; j++)
        {
            cin >> keyboard[i][j];
            if(keyboard[i][j]=='S')
            {
                ex = true;
                shi.push_back({i, j});
            }
        }
    }
    cin >> q >> text;
    if(!ex)
        cout << "-1\n";
    else
    {
        for(int i=1 ; i<=n; i++)
        {
            for(int j=1; j<=m ; j++)
            {
                if(keyboard[i][j]!='S') {
                double mi = 1e5;
                for(int k=0 ; k<shi.size(); k++)
                {
                    double ec = sqrt((i-shi[k].f)*(i-shi[k].f)+(j-shi[k].s)*(j-shi[k].s));

                    mi = min(mi, ec);
                }
                c[keyboard[i][j]-'a'] = mi;
                }
            }
        }

        int ans = 0;
        for(int i=0 ; i<q; i++)
        {
            if((isupper(text[i]) && c[text[i]-'A']=='-1') || (islower(text[i]) && c[text[i]-'a']=='-1'))
            {
                ex = false;
                break;
            }
            if(isupper(text[i]) && c[text[i]-'A']>x)
                ans++;
        }
        if(!ex)
            cout << "-1\n";
        else
            cout << ans << endl;

    }

    return 0;
}


#include <bits/stdc++.h>

using namespace std;

#define forn(i, n) for (i = 0; i < n; ++i)
#define for1(i, n) for (i = 1; i <= n; ++i)
#define pb push_back
#define all(x) (x).begin(), (x).end()
#define fill(x, b) memset((x), b, sizeof((x)))
#define fill2D(x, r, c, b) memset((x), b, sizeof(x[0][0]) * r * c)

typedef long long ll;
typedef vector<int> vi;
typedef vector<ll> vll;
typedef vector<vi> vvi;
typedef vector<vll> vvll;
typedef vector<string> vs;
typedef vector<vs> vvs;

const int mod = 1e9 + 7;

ll binPow(ll a, ll b)
{
    ll x = 1, y = a;
    while (b)
    {
        if (b & 1)
            x = (x * y) % mod;
        y = (y * y) % mod;
        b >>= 1;
    }
    return x % mod;
}

ll inverserEuler(int n) { return binPow(n, mod - 2); }

ll C(int k, int n)
{
    vll f(n + 1, 1);
    for (int i = 2; i <= n; i++)
        f[i] = (f[i - 1] * i) % mod;
    return (f[n] * ((inverserEuler(f[k]) * inverserEuler(f[n - k])) % mod) % mod) % mod;
}

/* Extend Euclid: ax + by = c; */
// ll x, y;

// void extendEuclid(ll a, ll b){if(b == 0){x = 1;y = 0;return;}extendEuclid(b,a%b);ll x1 = y, y1 = x - (a/b) * y;x = x1;y = y1;}
// nghiem : x + (b/d)*k, y - (a/d) * k;

/*--------------------------------------------------------------------------------------------------*/

double dist(int x1, int y1, int x2, int y2)
{
    return sqrt(pow(x2 - x1, 2) + pow(y2 - y1, 2));
}

int n, m, x;

char key[31][31];

void trunghieu()
{
    cin >> n >> m >> x;
    int i, j, ans = 0, k;
    map<char, vector<pair<int, int>>> pos;
    for1(i, n)
        for1(j, m)
        {
            cin >> key[i][j];
            pos[key[i][j]].push_back({i, j});
        }
    int q;
    cin >> q;
    string s;
    cin >> s;
    char preChar;
    vector<pair<int, int>> pS = pos['S'];
    forn(i, s.size())
    {
        auto pI = pos[tolower(s[i])];
        if (pI.empty())
        {
            cout << -1;
            return;
        }
        if (isupper(s[i]))
        {
            if (pS.empty())
            {
                cout << -1;
                return;
            }
            else
            {
                int flag = 0;
                for (auto j : pS)
                {
                    for (auto k : pI)
                    {
                        if (dist(j.first, j.second, k.first, k.second) <= x)
                        {
                            flag = 1;
                            break;
                        }
                    }
                    if(flag)
                        break;
                }
                if (!flag)
                    ans++;
            }
        }
    }
    cout << ans;
}

int main()
{
    ios::sync_with_stdio(false);
    cin.tie();
#ifndef ONLINE_JUDGE
    freopen("input.txt", "r", stdin);
    //freopen("output.txt","w",stdout);
#endif
    trunghieu();
    return 0;
}


#include <bits/stdc++.h>

using namespace std;

#define forn(i, n) for (i = 0; i < n; ++i)
#define for1(i, n) for (i = 1; i <= n; ++i)
#define pb push_back
#define all(x) (x).begin(), (x).end()
#define fill(x, b) memset((x), b, sizeof((x)))
#define fill2D(x, r, c, b) memset((x), b, sizeof(x[0][0]) * r * c)

typedef long long ll;
typedef vector<int> vi;
typedef vector<ll> vll;
typedef vector<vi> vvi;
typedef vector<vll> vvll;
typedef vector<string> vs;
typedef vector<vs> vvs;

const int mod = 1e9 + 7;

ll binPow(ll a, ll b)
{
    ll x = 1, y = a;
    while (b)
    {
        if (b & 1)
            x = (x * y) % mod;
        y = (y * y) % mod;
        b >>= 1;
    }
    return x % mod;
}

ll inverserEuler(int n) { return binPow(n, mod - 2); }

ll C(int k, int n)
{
    vll f(n + 1, 1);
    for (int i = 2; i <= n; i++)
        f[i] = (f[i - 1] * i) % mod;
    return (f[n] * ((inverserEuler(f[k]) * inverserEuler(f[n - k])) % mod) % mod) % mod;
}

/* Extend Euclid: ax + by = c; */
// ll x, y;

// void extendEuclid(ll a, ll b){if(b == 0){x = 1;y = 0;return;}extendEuclid(b,a%b);ll x1 = y, y1 = x - (a/b) * y;x = x1;y = y1;}
// nghiem : x + (b/d)*k, y - (a/d) * k;

/*--------------------------------------------------------------------------------------------------*/

double dist(int x1, int y1, int x2, int y2)
{
    return sqrt(pow(x2 - x1, 2) + pow(y2 - y1, 2));
}

int n, m, x;

char key[31][31];

void trunghieu()
{
    cin >> n >> m >> x;
    int i, j, ans = 0, k;
    map<char, vector<pair<int, int>>> pos;
    for1(i, n)
        for1(j, m)
    {
        cin >> key[i][j];
        pos[key[i][j]].push_back({i, j});
    }
    int q;
    cin >> q;
    string s;
    cin >> s;
    char preChar;
    vector<pair<int, int>> pS = pos['S'];
    forn(i, s.size())
    {
        auto pI = pos[tolower(s[i])];
        if (pI.empty())
        {
            cout << -1;
            return;
        }
        if (isupper(s[i]))
        {
            if (pS.empty())
            {
                cout << -1;
                return;
            }
            else
            {
                int flag = 0;
                for (auto j : pS)
                {
                    for (auto k : pI)
                    {
                        if (dist(j.first, j.second, k.first, k.second) <= x)
                        {
                            flag = 1;
                            break;
                        }
                    }
                }
                if (!flag)
                    ans++;
            }
        }
    }
    cout << ans;
}

int main()
{
    ios::sync_with_stdio(false);
    cin.tie();
#ifndef ONLINE_JUDGE
    freopen("input.txt", "r", stdin);
    //freopen("output.txt","w",stdout);
#endif
    trunghieu();
    return 0;
}


#include <bits/stdc++.h>

using namespace std;

#define forn(i, n) for (i = 0; i < n; ++i)
#define for1(i, n) for (i = 1; i <= n; ++i)
#define pb push_back
#define all(x) (x).begin(), (x).end()
#define fill(x, b) memset((x), b, sizeof((x)))
#define fill2D(x, r, c, b) memset((x), b, sizeof(x[0][0]) * r * c)

typedef long long ll;
typedef vector<int> vi;
typedef vector<ll> vll;
typedef vector<vi> vvi;
typedef vector<vll> vvll;
typedef vector<string> vs;
typedef vector<vs> vvs;

const int mod = 1e9 + 7;

ll binPow(ll a, ll b)
{
    ll x = 1, y = a;
    while (b)
    {
        if (b & 1)
            x = (x * y) % mod;
        y = (y * y) % mod;
        b >>= 1;
    }
    return x % mod;
}

ll inverserEuler(int n) { return binPow(n, mod - 2); }

ll C(int k, int n)
{
    vll f(n + 1, 1);
    for (int i = 2; i <= n; i++)
        f[i] = (f[i - 1] * i) % mod;
    return (f[n] * ((inverserEuler(f[k]) * inverserEuler(f[n - k])) % mod) % mod) % mod;
}

/* Extend Euclid: ax + by = c; */
// ll x, y;

// void extendEuclid(ll a, ll b){if(b == 0){x = 1;y = 0;return;}extendEuclid(b,a%b);ll x1 = y, y1 = x - (a/b) * y;x = x1;y = y1;}
// nghiem : x + (b/d)*k, y - (a/d) * k;

/*--------------------------------------------------------------------------------------------------*/

double dist(int x1, int y1, int x2, int y2)
{
    return sqrt(pow(x2 - x1, 2) + pow(y2 - y1, 2));
}

int n, m, x;

char key[31][31];

void trunghieu()
{
    cin >> n >> m >> x;
    int i, j, ans = 0, k,i1,j1;
    map<char, vector<pair<int, int>>> pos;
    for1(i, n)
        for1(j, m)
            cin >> key[i][j],
            pos[key[i][j]].pb({i,j});
    int q;
    cin >> q;
    string s;
    cin >> s;
    char preChar;
    vector<pair<int, int>> pS = pos['S'];
    map<char,int> can;
    for(auto u:pS)
    {
        for1(i,n)
            for1(j,m)
                if(dist(i,j,u.first, u.second) <=x && key[i][j]!='S')
                    can[toupper(key[i][j])] = 1;
    }
    forn(i, s.size())
    {
        auto pI = pos[tolower(s[i])];
        if (pI.empty())
        {
            cout << -1;
            return;
        }
        if (isupper(s[i]))
        {
            if (pS.empty())
            {
                cout << -1;
                return;
            }
            else
            {
                if(!can[s[i]])
                {
                    //cout<<s[i]<<" ";
                    ans++;
                }
            }
        }
    }
    cout << ans;
}

int main()
{
    ios::sync_with_stdio(false);
    cin.tie();
#ifndef ONLINE_JUDGE
    freopen("input.txt", "r", stdin);
    //freopen("output.txt","w",stdout);
#endif
    trunghieu();
    return 0;
}