// Slower C++ solution:
#include <bits/stdc++.h>

using namespace std;

const int MAX = 1024;



int h, w, n, maze[MAX][MAX];

int b[15];

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

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

int d[MAX][MAX];

int ans = 0;



int readchar() {

    for(;;) {

        int ch = getchar();

        if(ch != '\n' && ch != '\r') return ch;

    }

}



int bfs(int x, int y, int x2, int y2)

{

    //cout << x << y << x2 << y2 << endl;



    queue<int> q;

    q.push(x * 10000 + y);

    while(!q.empty()) {

        int v = q.front();

        q.pop();

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

            int x1 = v / 10000 + r[i];

            int y1 = v % 10000 + z[i];

            if(x1 == x2 && y1 == y2) {

                d[x1][y1] = d[v / 10000][v % 10000] + 1;

                return 0;

            }

            if(x1 >= 0 && y1 >= 0 && x1 < h && y1 < w && maze[x1][y1] != 'X' && d[x1][y1] == -1) {

                 d[x1][y1] = d[v / 10000][v % 10000] + 1;

                 q.push(x1 * 10000 + y1);

            }

        }

    }

    return 0;

}

int main()

{

    cin >> h >> w >> n;

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

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

             maze[i][j] = readchar();

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

            b[0] = i * 10000 + j;

        }

        if(maze[i][j] != 'S' && maze[i][j] != 'X' && maze[i][j] != '.') {

            b[maze[i][j] - '0'] = i * 10000 + j;

        }

    }

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

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

        d[b[i] / 10000][b[i] % 10000] = 0;

        bfs(b[i] / 10000, b[i] % 10000, b[i + 1] / 10000, b[i + 1] % 10000);

        ans += d[b[i + 1] / 10000][ b[i + 1] % 10000];

    }

    cout << ans << endl;

    return 0;

}

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

#include<cstdio>

#include<vector>

#include<cmath>

#include<map>

#include<set>

#include<queue>

#include<cstring>

#include<algorithm>

using namespace std;

const int maxn = 1000 + 8;

#define inf 0x3f3f3f3f



struct node

{

    int x,y;

    node(int x = 0,int y = 0):x(x),y(y){}

}s,current,st,nd;



int h,w,n;

int step[maxn][maxn];

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

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

string Map[maxn];

int sum = 0;



int bfs()

{

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

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

            step[i][j] = inf;

        }

    }

    queue<node> q;

    step[st.x][st.y] = 0;

    q.push(st);

    while(!q.empty())

    {

        current = q.front();

        q.pop();

        if(current.x == nd.x && current.y == nd.y) break;

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

        {

            s.x = current.x + dx[i];

            s.y = current.y + dy[i];

            if(s.x >= 0 && s.x < h && s.y >= 0 && s.y < w && Map[s.x][s.y] != 'X' && step[s.x][s.y] == inf)

            {

                step[s.x][s.y] = step[current.x][current.y] + 1;

                q.push(s);

            }

        }

    }

    return step[nd.x][nd.y];

}

int main()

{

    while(cin >> h >> w >> n)

    {

        sum = 0;

        for(int i = 0 ; i < h; ++i) cin >> Map[i];

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

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

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

                    st.x = i;

                    st.y = j;

                }

            }

        }



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

        {

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

            {

                for(int k = 0; k < w; ++k)

                {

                    if(Map[j][k] == '1' + i)

                    {

                        nd.x = j; nd.y = k;

                        sum += bfs();

                        st.x = j; st.y = k;

                    }

                }

            }

        }

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

    }

    return 0;

}



/*

3 3 1

S..

...

..1

4 5 2

.X..1

....X

.XX.S

.2.X.

10 10 9

.X...X.S.X

6..5X..X1X

...XXXX..X

X..9X...X.

8.X2X..X3X

...XX.X4..

XX....7X..

X..X..XX..

X...X.XX..

..X.......

*/