// Slower C++ solution:
#include<cstdio>

#include<cstring>

#include<queue>

#define maxn 1002

#define maxc 10

using namespace std;

struct point{

    int r,c;

};

char graph[maxn][maxn];

int len[maxn][maxn];

bool mark[maxn][maxn];

point node[maxc];

int dir[4][2]={1,0,-1,0,0,1,0,-1};

int main(void){

    int h,w,cheese,tr,tc,answer=0;

    scanf("%d%d%d",&h,&w,&cheese);

    memset(graph,0,sizeof(graph));

    memset(node,0,sizeof(node));

    for(int i=1;i<=h;i++) for(int j=1;j<=w;j++){

        do scanf("%c",&graph[i][j]);

        while(graph[i][j]==' ' || graph[i][j]=='\n');

        if(graph[i][j]>='0' && graph[i][j]<='9'){

            node[graph[i][j]-'0'].r=i;

            node[graph[i][j]-'0'].c=j;

        }

        else if(graph[i][j]=='S'){

            node[0].r=i;

            node[0].c=j;

        }

    }

    for(int cnt=0;cnt<cheese;cnt++){

        memset(len,0,sizeof(len));

        memset(mark,false,sizeof(mark));

        for(int i=0;i<=h+1;i++){mark[i][0]=true;mark[i][w+1]=true;}

        for(int j=0;j<=w+1;j++){mark[0][j]=true;mark[h+1][j]=true;}

        queue<point> q;

        q.push(node[cnt]);

        len[node[cnt].r][node[cnt].c]=0;

        point ongo,togo;

        while(!q.empty()){

            ongo=q.front();

            q.pop();

            if(mark[ongo.r][ongo.c]) continue;

            mark[ongo.r][ongo.c]=true;

            if(ongo.r==node[cnt+1].r && ongo.c==node[cnt+1].c){

                answer+=len[ongo.r][ongo.c];

                break;

            }

            for(int i=0;i<4;i++){

                togo.r=ongo.r+dir[i][0];togo.c=ongo.c+dir[i][1];

                if(mark[togo.r][togo.c]==false && graph[togo.r][togo.c]!='X'){

                    len[togo.r][togo.c]=len[ongo.r][ongo.c]+1;

                    q.push(togo);

                }

            }

        }

    }

    printf("%d\n",answer);

    return 0;

}


// Faster C++ solution for the same problem (measured speedup: 1.3962462335537529x):
#include<iostream>

#include<algorithm>

#include<math.h>

#include<string.h>

#include<queue>

#include<stdio.h>

using namespace std;



typedef struct{

	int x,y;

}p;

char a[1005][1005];

p acm[11];

p s,g;

int b[1005][1005];

int H,W,N;

int dx[]={1,-1,0,0};

int dy[]={0,0,1,-1};

int bfs(p s,p g)

{

	queue<p> q;

	p n;

	memset(b,-1,sizeof(b));

	q.push(s);

	b[s.x][s.y]=0;

	while(!q.empty())

	{

		p h;

		h=q.front();

		q.pop();

		if(h.x==g.x&&h.y==g.y)

		{

			break;

		}

		for(int i=0;i<4;i++)

		{

			n.x=h.x+dx[i];

			n.y=h.y+dy[i];

			if(n.x>=0&&n.x<H&&n.y>=0&&n.y<W&&a[n.x][n.y]!='X'&&b[n.x][n.y]==-1)

			{

				q.push(n);

				b[n.x][n.y]=b[h.x][h.y]+1;

			}

		}

	}

	return b[g.x][g.y];

}

int main()

{

	int i,j,t,step=0;

	scanf("%d%d%d",&H,&W,&N);

	for(i=0;i<H;i++)

	{

		scanf("%s",a[i]);

	}

	for(i=0;i<H;i++)

	for(j=0;j<W;j++)

	{

		if(a[i][j]>='1'&&a[i][j]<='9')

		{

			t=a[i][j]-'0';

		    acm[t].x=i;

		    acm[t].y=j;

		}

		else if(a[i][j]=='S')

		{

			s.x=i;

			s.y=j;

			a[i][j]='.';

		}

	}

		for(i=1;i<=N;i++)

		{

			g.x=acm[i].x;

			g.y=acm[i].y;

			step+=bfs(s,g);

			s.x=g.x;

			s.y=g.y;

			a[g.x][g.y]='.';

		}

	

	printf("%d\n",step);

	return 0;

 }