// Slower C++ solution:
#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;  

}  


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

#include <queue>

#include <string>

#include <cstdio>

#include <cstring>

#include <utility>

using namespace std;

typedef pair<int, int> P;

char maze[1005][1005];

int d[1005][1005];

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

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

int h, w, n, hard; 

int si, sj;

const int INF = 0x3f3f3f3f;

int tot = 0; 



void BFS(int ai, int aj){

    queue<P> que;

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

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

            d[i][j] = INF;

        }

    }

    que.push(P(ai, aj));

    d[ai][aj] = 0;

    while(!que.empty()){

        P a = que.front();

        que.pop();

        int x = a.first, y = a.second;

        if(maze[x][y]-'0' == hard){

            tot += d[x][y];

            si = x; sj = y;

            break;

        }

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

            int dx = x+dirx[i], dy = y+diry[i];

            if(dx>=0 && dx<h && dy>=0 && dy<w && maze[dx][dy]!='X' && d[dx][dy]==INF){

                d[dx][dy] = d[x][y]+1;

                que.push(P(dx, dy));

            }

        }

    }

}



int main(int argc, char const *argv[]){

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

        getchar();

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

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

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

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

                    si = i; sj = j;

                }

            }

            getchar();

        }

        tot = 0;

        hard = 1;

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

            BFS(si, sj);

            hard++;

        }

        cout << tot << endl;

    }

    return 0;

}