#include <bits/stdc++.h>
using namespace std;
class edge{
public:
int x,y,dir,c;
edge(int a, int b, int d){
x=a; y=b; c=d;
}
bool operator < (const edge &other) const{
return c > other.c;
}
};
int n,m;
string A;
char matrix[55][55];
vector <edge> adj[55][55];
int mark[55][55];
int xx,yy;
int movx[]={1,-1,0,0};
int movy[]={0,0,1,-1};
void BFS (int x,int y){
queue <edge> cola;
cola.push(edge(x,y,0));
memset(mark,-1,sizeof(mark));
while (!cola.empty()){
edge v = cola.front(); cola.pop();
if (mark[v.x][v.y]>0) continue;
adj[x][y].push_back(edge(v.x,v.y,v.c));
mark[v.x][v.y]=v.c;
for (int i=0;i<4;++i){
int aux= movx[i]+v.x;
int auy= movy[i]+v.y;
if (aux<0 || auy<0 || aux>=n || auy>=m || mark[aux][auy]>=0) continue;
if (matrix[aux][auy]==matrix[v.x][v.y]){
while (matrix[aux][auy]==matrix[v.x][v.y]){
int auxx= aux+movx[i];
int auyy= auy+movy[i];
if (auxx<0 || auyy<0 || auxx>=n || auyy>=m) break;
mark[aux][auy]=0;
aux=auxx;
auy=auyy;
}
}
if (matrix[v.x][v.y]!=matrix[aux][auy] && mark[aux][auy]<0)
mark[aux][auy]=0;
cola.push(edge(aux,auy,v.c+1));
}
}
}
int dijkstra (char B){
memset(mark,0x3f3f3f3f,sizeof(mark));
priority_queue <edge> cola; cola.push(edge(xx,yy,0));
while (!cola.empty()){
edge v = cola.top(); cola.pop();
if (matrix[v.x][v.y]==B){
xx=v.x; yy=v.y;
return v.c;
}
if (matrix[v.x][v.y]<v.c) continue;
for (int i=0;i<adj[v.x][v.y].size();++i){
int p = adj[v.x][v.y][i].x;
int q = adj[v.x][v.y][i].y;
int cc= adj[v.x][v.y][i].c + v.c;
if (cc >= mark[p][q]) continue;
mark[p][q]=cc;
cola.push(edge(p,q,cc));
}
}
}
int main(){
scanf("%d %d",&n,&m);
for (int i=0;i<n;++i)
scanf("%s",matrix[i]);
int cont=0;
cin>>A;
for (int i=0;i<n;++i){
for (int j=0;j<m;++j){
BFS(i,j);
}
}
A.push_back('*');
cont+=A.size();
xx=0; yy=0;
for (int i=0;i<A.size();++i){
cont+=dijkstra(A[i]);
}
printf("%d\n",cont);
return 0;
}
I2luY2x1ZGUgPGJpdHMvc3RkYysrLmg+CiAKdXNpbmcgbmFtZXNwYWNlIHN0ZDsKIApjbGFzcyBlZGdlewoJcHVibGljOgoJCglpbnQgeCx5LGRpcixjOwoJCgllZGdlKGludCBhLCBpbnQgYiwgaW50IGQpewoJCXg9YTsgeT1iOyBjPWQ7CgkJCgl9CgkKCWJvb2wgb3BlcmF0b3IgPCAoY29uc3QgZWRnZSAmb3RoZXIpIGNvbnN0ewoJCQoJCXJldHVybiBjID4gb3RoZXIuYzsKCX0KfTsgCgppbnQgbixtOwpzdHJpbmcgQTsKIApjaGFyIG1hdHJpeFs1NV1bNTVdOwp2ZWN0b3IgPGVkZ2U+IGFkals1NV1bNTVdOwoKaW50IG1hcmtbNTVdWzU1XTsKaW50IHh4LHl5OwppbnQgbW92eFtdPXsxLC0xLDAsMH07CmludCBtb3Z5W109ezAsMCwxLC0xfTsgIAoKdm9pZCBCRlMgKGludCB4LGludCB5KXsKCQoJcXVldWUgPGVkZ2U+IGNvbGE7CgkKCWNvbGEucHVzaChlZGdlKHgseSwwKSk7CgltZW1zZXQobWFyaywtMSxzaXplb2YobWFyaykpOwoJd2hpbGUgKCFjb2xhLmVtcHR5KCkpewoJCWVkZ2UgdiA9IGNvbGEuZnJvbnQoKTsgY29sYS5wb3AoKTsKCQkKCQlpZiAobWFya1t2LnhdW3YueV0+MCkgY29udGludWU7CgkJYWRqW3hdW3ldLnB1c2hfYmFjayhlZGdlKHYueCx2Lnksdi5jKSk7CgkJbWFya1t2LnhdW3YueV09di5jOwoJCWZvciAoaW50IGk9MDtpPDQ7KytpKXsKCQkJaW50IGF1eD0gbW92eFtpXSt2Lng7CgkJCWludCBhdXk9IG1vdnlbaV0rdi55OwoJCQkKCQkJaWYgKGF1eDwwIHx8IGF1e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Vt2LnldW2ldLng7CgkJCWludCBxID0gYWRqW3YueF1bdi55XVtpXS55OwoJCQlpbnQgY2M9IGFkalt2LnhdW3YueV1baV0uYyArIHYuYzsKCQkJCgkJCWlmIChjYyA+PSBtYXJrW3BdW3FdKSBjb250aW51ZTsKCQkJbWFya1twXVtxXT1jYzsKCQkJY29sYS5wdXNoKGVkZ2UocCxxLGNjKSk7CgkJfQoJfQp9CiAKaW50IG1haW4oKXsKIAogCiAgICAgICAgc2NhbmYoIiVkICVkIiwmbiwmbSk7CiAKICAgICAgICBmb3IgKGludCBpPTA7aTxuOysraSkKICAgICAgICAgICAgICAgIHNjYW5mKCIlcyIsbWF0cml4W2ldKTsKIAogICAgICAgIAogICAgICAgIGludCBjb250PTA7CiAgICAgICAgY2luPj5BOwoJCQlmb3IgKGludCBpPTA7aTxuOysraSl7CgkJCQlmb3IgKGludCBqPTA7ajxtOysrail7CgkJCQkJCgkJCQkJQkZTKGksaik7CgkJCQl9CgkJCX0KCQkKICAgICAgICBBLnB1c2hfYmFjaygnKicpOwogICAgICAgIGNvbnQrPUEuc2l6ZSgpOwogICAgICAgIAogICAgICAgIHh4PTA7IHl5PTA7CiAgICAgICAgZm9yIChpbnQgaT0wO2k8QS5zaXplKCk7KytpKXsKCQkJY29udCs9ZGlqa3N0cmEoQVtpXSk7CgkJfQogICAgIAogICAgICAgIAogICAgICAgIHByaW50ZigiJWRcbiIsY29udCk7CiAgICAgICAgcmV0dXJuIDA7Cn0=