// 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.4122964059862313x):
#include <stdio.h>  

#include <string.h>  

#include <queue>  

#define MAX_W 1000  

#define MAX_H 1000  

#define MAX_N 9  

  

using namespace std;  

  

char m[MAX_H][MAX_W + 1];  

int w, h, n;  

int gx[MAX_N + 1], gy[MAX_N + 1];  

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

  

int bfs(int sx, int sy, int gx, int gy)  

{  

    int d[MAX_H][MAX_W];  

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

    queue<int> que;  

    que.push(sx * w + sy);  

    d[sx][sy] = 0;  

          

    while(que.size())  

    {  

        int x = que.front()/w, y = que.front()%w;  

        que.pop();  

        if(x == gx && y == gy)  

            break;  

  

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

            int nx = x + dx[di], ny = y + dy[di];  

            if(nx >= 0 && nx < h && ny >= 0 && ny < w && 'X' != m[nx][ny] && -1 == d[nx][ny]){  

                d[nx][ny] = d[x][y] + 1;  

                que.push(nx * w + ny);  

            }  

        }  

    }  

    return d[gx][gy];  

}  

  

void solve()  

{  

    int ans = 0;  

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

        ans += bfs(gx[i], gy[i], gx[i + 1], gy[i + 1]);  

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

}  

  

int main()  

{  

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

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

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

          

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

        for(int j = 0; j < w; j ++){  

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

                gx[m[i][j] - '0'] = i, gy[m[i][j] - '0'] = j;  

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

                gx[0] = i, gy[0] = j;  

        }  

  

    if(w || h)  

        solve();  

    return 0;  

}