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

#include<cstdio>

#include<queue>

#include<cstring>

#include<vector>

#include<set>

using namespace std;

typedef long long ll;

int H,W,N;

char mm[1005][1005];

bool v[1005][1005];

int xx,yy;

struct node{

    int x,y,step;

};

queue<node> que;

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

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

bool check(int x,int  y){

    if(1<=x&&x<=H&&1<=y&&y<=W&&!v[x][y]&&mm[x][y]!='X')return true;

    else return false;

}

int bfs(int g){

    while(!que.empty())que.pop();

    node t;

    t.x=xx;

    t.y=yy;

    t.step=0;

    v[xx][yy]=1;

    que.push(t);

    while(!que.empty()){

        t=que.front();

        que.pop();

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

            int nx=t.x+dx[i];

            int ny=t.y+dy[i];

            if(check(nx,ny)){

                if(mm[nx][ny]==g+'0'){

                    xx=nx;

                    yy=ny;

                    return t.step+1;

                }

                node tt;

                tt.x=nx;

                tt.y=ny;

                tt.step=t.step+1;

                v[nx][ny]=1;

                que.push(tt);

            }

        }

    }

}

int main(){

    while(~scanf("%d%d%d",&H,&W,&N)){

        for(int i=1;i<=H;i++){

            scanf("%s",mm[i]+1);

        }

        for(int i=1;i<=H;i++){

            for(int j=1;j<=W;j++){

                if(mm[i][j]=='S'){

                    xx=i;

                    yy=j;

                }

            }

        }

        int ans=0;

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

            memset(v,0,sizeof(v));

            ans+=bfs(i);

        }

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

    }

    return 0; 

}


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

#include <queue>

using namespace std;

const int INF = 100000000;

typedef pair<int,int> Point;

Point AllPoint[1000];

int W,H;

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

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

char map[1000][1000];

int deep[1000][1000];

int N;

void BFS(Point Start,Point Over){

    queue<Point> que;

    int FirstDeep = deep[Start.first][Start.second];

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

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

            deep[i][j] = INF;

        }

    }

    que.push(Start);

    deep[Start.first][Start.second] = FirstDeep;

    while (que.size()){

        Point p = que.front();

        que.pop();

        if (p.first== Over.first&&p.second == Over.second)

            break;

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

            int ix = p.first+x[i];

            int iy = p.second+y[i];

            if (ix>=0&&ix<=H

                &&iy>=0&&iy<=W

                &&deep[ix][iy] == INF

                &&map[ix][iy]!='X'){

                que.push(Point(ix,iy));

                deep[ix][iy] = deep[p.first][p.second]+1;

            }

        }

    }

}







int main(){

    cin >> H >> W;

    cin >> N;

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

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

            cin >> map[i][j];

            if ((int)map[i][j]-48>=1&&(int)map[i][j]-48<=N){

                AllPoint[(int)map[i][j]-48].first = i;

                AllPoint[(int)map[i][j]-48].second = j;



            }

            if (map[i][j] == 'S'){

                AllPoint[0].first = i;

                AllPoint[0].second = j;

            }

        }

    }

    deep[AllPoint[0].first][AllPoint[0].second] = 0;

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

        BFS(AllPoint[i],AllPoint[i+1]);

    }

    cout << deep[AllPoint[N].first][AllPoint[N].second]<<endl;



    return 0;

}

//#....

//S.##.

//###G.

//#####

//#####



//#S######.#

//......#..#

//.#.##.##.#

//.#........

//##.##.####

//....#....#

//.#######.#

//....#.....

//.####.###.

//....#...G#