// Slower C++ solution:


#include <iostream>

#include <cstdio>

#include <cstring>

#include <queue>



using namespace std;

const int maxn = 1e3 + 5;

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

int mp[maxn][maxn];

//bool vis[maxn][maxn];

int d[maxn][maxn];

struct point{

    int x,y;

};

int h,w,n;

 int dis = 0;

void bfs(point &p,int cnt)

{

   // memset(vis, 0, sizeof(vis));

    memset(d, 0, sizeof(d));

    queue<point> q;

    q.push(p);

    while (!q.empty()) {

        point t = q.front();q.pop();

        //vis[t.x][t.y] = true;

        if(mp[t.x][t.y] == cnt + 1) {dis += d[t.x][t.y];p.x = t.x,p.y = t.y;return ;}

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

            int tx = t.x + dir[i][0];

            int ty = t.y + dir[i][1];

            if (tx >= 0 && tx < h && ty >= 0 && ty < w && !d[tx][ty] && mp[tx][ty] != -1) {

                q.push({tx,ty});

                d[tx][ty] = 1 + d[t.x][t.y];

            }

        }

    }

}

int main()

{

    while (scanf("%d%d%d",&h,&w,&n) != EOF) {

        getchar();

        char ch;

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

       // int sx,sy;

        point s;

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

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

                scanf("%c",&ch);

                if(ch == 'S') {s.x = i,s.y = j;mp[i][j] = 0;}

                else if(ch == '.') {mp[i][j] = 0;}

                else if(isdigit(ch)) {mp[i][j] = ch - '0';}

            }

            getchar();

        }

       

        dis = 0;

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

            bfs(s,i);

        }

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

    }

    return 0;

}

// Faster C++ solution for the same problem (measured speedup: 1.833830875201928x):


#include<iostream>

#include<cstdio>

#include<deque>

#include<algorithm>



using namespace std;



const int maxn=1e3+5;

const int inf=0x3f3f3f3f;



typedef pair<int,int>P;



int h,w,n,sx,sy,book[maxn][maxn],goal[10],Next[4][2]={{0,1},{1,0},{0,-1},{-1,0}};

char s[maxn][maxn];



int judge(int row,int col)

{

    if(row>=0&&row<h&&col>=0&&col<w){

        return 1;

    }

    return 0;

}



int bfs()

{

    int res=0;

    deque<P>q;

    for(int ci=0;ci<n;ci++){

        fill(book[0],book[0]+maxn*maxn,inf);

        q.push_back(P(sx,sy));

        book[sx][sy]=0;

        while(!q.empty()){

            int x=q.front().first;

            int y=q.front().second;

            q.pop_front();

            if((int)s[x][y]==49+ci){

                goal[ci]=book[x][y];

                sx=x;

                sy=y;

                break;

            }

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

                int nx=x+Next[i][0];

                int ny=y+Next[i][1];

                if(judge(nx,ny)&&s[nx][ny]!='X'&&book[nx][ny]==inf){

                    q.push_back(P(nx,ny));

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

                }

            }

        }

        q.clear();

        res+=goal[ci];

    }

    return res;

}



int main()

{

    cin>>h>>w>>n;

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

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

            cin>>s[i][j];

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

                sx=i;

                sy=j;

            }

        }

    }

    int res=bfs();

    cout<<res<<endl;

    return 0;

}