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

#include <queue>

#include <string.h>

using namespace std;

typedef pair<int, int> P;

int sx,sy,H,W,N;

char map1[1002][1002];

int step[1002][1002];

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

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

void find1(int g){

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

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

        if(map1[i][k] == 'S' || map1[i][k] == (g+48)){

        if(map1[i][k] == 'S') map1[i][k] ='.';

        sx = i; sy = k;

        return ;

    }

}



int bfs(int n)

{

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

    queue <P> que;

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

    step[sx][sy] = 0;

    while(que.size()){

        int x = que.front().first, y = que.front().second;

        que.pop();

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

            int xn = x + dx[i], yn = y + dy[i];

            if(xn >= 0 && xn < W && yn >= 0 && yn < H && map1[xn][yn] != 'X' && step[xn][yn] == -1)

                {

                    step[xn][yn] = step[x][y] + 1;

                    if(map1[xn][yn] == (n+48))

                        {

                            return step[xn][yn];

                        }

                    que.push(P(xn, yn));

                }

        }

    }

return 0;

}



int main()

{

    cin >> W >> H >> N;

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

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

        cin >> map1[i][k];

    int step = 0;

    find1(0);

    step += bfs(1);

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

    {



        find1(i);

        step += bfs(i+1);

    }

    cout << step << endl;

    return 0;

}

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

#include <queue>

#include <string>



using namespace std;



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

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

int First = 1000;

int w,h,n;

int ans = 0;

int d[1001][1001];

typedef struct p{

	int x, y;

}P;

string mapp[1001];

P nezumi[11];





//幅優先探索を行う

void bfs(){

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

		P sp = nezumi[i-1];

		int sx = nezumi[i-1].x;

		int sy = nezumi[i-1].y;

		int gx = nezumi[i].x;

		int gy = nezumi[i].y;



		//二次元配列の初期化をする

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

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

				d[y][x] = First;

			}

		}

		

		queue<P> q;

		q.push( sp );

		d[sy][sx] = 0;

		while( !q.empty() ){

			P now = q.front();

			q.pop();

			int x = now.x;

			int y = now.y;



			if( x == gx && y == gy ){

				ans += d[gy][gx];



				/*動作確認

				cout << "\n";

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

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

						if (d[y][x]==First){cout << "X";}

							else{cout << d[y][x];}

					}

					cout << "\n";

				}*/

				break;

			}



			//redandblackと同じように探索

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

				int newx = x + dx[i];

				int newy = y + dy[i];

				if( newx < 0 || newy < 0 || newx >= w || newy >= h ) continue;

				if( mapp[newy][newx] != 'X' ){

					P next;

					next.x = newx;

					next.y = newy;

					if( d[newy][newx] == First ){

						d[newy][newx] = d[y][x] + 1;

						q.push( next );

					}

				}

			}

		}

	}

}



int main(){

	//標準入力

	cin >> h >> w >> n;

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

		cin >> mapp[y];

	}

	

	//スタートと数字の確認

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

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

			char c = mapp[y][x];



			//アスキー使っていい感じにしてみる

			if( c >= '1' && c <= '9' ){

				nezumi[ c - '0' ].x = x;

				nezumi[ c - '0' ].y = y;

			}

			if(c == 'S'){

				nezumi[0].x = x;

				nezumi[0].y = y;

			}

		}

	}

	bfs();

	cout << ans << endl;

}