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

#include<queue>

#include<cctype>





using namespace std;



int cnt;

char field[1002][1002];

int cost[1002][1002];

int factory[9][2];

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

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



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

    queue< pair<int,int> > que;



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

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

            cost[i][j] = -1;

        }

    }



    que.push(make_pair(sx, sy));

    cost[sx][sy] = 0;



    while(!que.empty()) {

        pair<int, int> p;

        p = que.front();

        que.pop();

        

        if(p.first == gx && p.second == gy) {

            break;

        }



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

            int tx = p.first + x[i]; int ty = p.second + y[i];

            if(field[tx][ty] != 'X' && cost[tx][ty] == -1) {

                que.push(pair<int, int>(tx, ty));

                cost[tx][ty] = cost[p.first][p.second] + 1;

            }

        }

    }

    return cost[gx][gy];

}



int main() {

    int w, h, n, sx, sy;



    cin >> h >> w >> n;



    for (int y = 1; y <= h; y++) {

        for (int x = 1; x <= w; x++) {

            cin >> field[x][y];

            if (field[x][y] == 'S') {

                sx = x;

                sy = y;

            } else if(isdigit(field[x][y])) {

                int tmp = field[x][y] - '0';

                factory[tmp][0] = x;

                factory[tmp][1] = y;

            }

        }

        field[0][y] = 'X';

        field[w + 1][y] = 'X';

    }



    for (int x = 0; x <= w + 1; x++) {

        field[x][0] = 'X';

        field[x][h + 1] = 'X';

    }



    int cnt = 0;

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

        // 次の目的地へ行くで

        cnt += bfs(sx, sy, factory[i][0], factory[i][1]);

        sx = factory[i][0];

        sy = factory[i][1];

    }



    cout << cnt << endl;

}

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

#include <cctype>

#include <queue>

#include <string.h>

using namespace std;

const int DIRECTSIZE = 4;

struct _direct {

    int drow;

    int dcol;

} direct[DIRECTSIZE] = {{0, -1}, {0, 1}, {-1, 0}, {1, 0}};



const int N = 1000;

const int N2 = 10;

char grid[N][N], visited[N][N];

int h, w, n;

struct _factory {

    int row, col;

} factory[N2];



struct _node {

    int row, col, level;

};



int bfs(int n, char goal)

{

    memset(visited, 0, sizeof(visited));



    _node start={factory[n-1].row, factory[n-1].col, 0}, temp;



    visited[start.row][start.col] = 1;



    queue<_node> q;

    q.push(start);



    while(!q.empty()) {

        _node front = q.front();

        q.pop();



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

            int nextrow = front.row + direct[i].drow;

            int nextcol = front.col + direct[i].dcol;

            if( 0 <= nextrow && nextrow < h && 0 <= nextcol && nextcol < w) {

                if(grid[nextrow][nextcol] == goal) {

                    return front.level + 1;

                } else if(visited[nextrow][nextcol] == 0 && grid[nextrow][nextcol] != 'X') {

                    visited[nextrow][nextcol] = 1;

                    temp.row = nextrow;

                    temp.col = nextcol;

                    temp.level = front.level + 1;

                    q.push(temp);

                }

            }

        }

    }



    return 0;

}



int main()

{

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

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

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

                cin >> grid[i][j];



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

                    factory[0].row = i;

                    factory[0].col = j;

                } else if(isdigit(grid[i][j])) {

                    factory[grid[i][j] - '0'].row = i;

                    factory[grid[i][j] - '0'].col = j;

                }

            }



        int minstep = 0;

        char goal = '1';

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

            minstep += bfs(i, goal);

            goal++;

        }



        cout << minstep << endl;

    }



    return 0;

}