// Slower C++ solution:
/************************************************

* Author        :somniloquy

* Created Time  :2015/10/30 16:15:09

 ************************************************/



#include <cstdio>

#include <algorithm>

#include <iostream>

#include <cstring>

#include <queue>



using namespace std;



const int hw_max = 1000 + 5;

char graph[hw_max][hw_max];

int sign[hw_max][hw_max];

int h, w, n, ans;

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

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

struct node

{

	int x;

	int y;

	int time;

} start;



void BFS(node op, char ed)

{

	queue <node> process;

	process.push(op);

	sign[op.x][op.y] = 1;

	while(!process.empty())

	{

		node now = process.front();

		process.pop();

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

		{

			node next = now;

			next.x += dx[i];

			next.y += dy[i];

			next.time ++;

			if(next.x >= 0 && next.x < h && next.y >= 0 && next.y < w && graph[next.x][next.y] != 'X' && !sign[next.x][next.y])

			{

				if(graph[next.x][next.y] == ed)

				{

					ans += next.time;

					next.time = 0;

					start = next;

				       	return;

				}

				else

				{

					process.push(next);

					sign[next.x][next.y] = 1;

				}

			}

		}

	}

}



int main()

{

	while(~scanf("%d %d %d", & h, & w, & n))

	{

		getchar();

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

		{

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

			{

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

			       if(graph[i][j] == 'S')

			       {

					start.x = i;

					start.y = j;

					start.time = 0;

			       }

			}

			getchar();

		}

		ans = 0;

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

		{

			memset(sign, 0, sizeof(sign));

			BFS(start, '0' + i);

		}

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

	}

	return 0;

}

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

#include <queue>

using namespace std;

const int INF = 100000000;

typedef pair<int,int> Point;

Point AllPoint[1000];

int W,H;

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

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

char map[1000][1000];

int deep[1000][1000];

int N;

void BFS(Point Start,Point Over){

    queue<Point> que;

    int FirstDeep = deep[Start.first][Start.second];

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

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

            deep[i][j] = INF;

        }

    }

    que.push(Start);

    deep[Start.first][Start.second] = FirstDeep;

    while (que.size()){

        Point p = que.front();

        que.pop();

        if (p.first== Over.first&&p.second == Over.second)

            break;

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

            int ix = p.first+x[i];

            int iy = p.second+y[i];

            if (ix>=0&&ix<=H

                &&iy>=0&&iy<=W

                &&deep[ix][iy] == INF

                &&map[ix][iy]!='X'){

                que.push(Point(ix,iy));

                deep[ix][iy] = deep[p.first][p.second]+1;

            }

        }

    }

}







int main(){

    cin >> H >> W;

    cin >> N;

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

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

            cin >> map[i][j];

            if ((int)map[i][j]-48>=1&&(int)map[i][j]-48<=N){

                AllPoint[(int)map[i][j]-48].first = i;

                AllPoint[(int)map[i][j]-48].second = j;



            }

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

                AllPoint[0].first = i;

                AllPoint[0].second = j;

            }

        }

    }

    deep[AllPoint[0].first][AllPoint[0].second] = 0;

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

        BFS(AllPoint[i],AllPoint[i+1]);

    }

    cout << deep[AllPoint[N].first][AllPoint[N].second]<<endl;



    return 0;

}

//#....

//S.##.

//###G.

//#####

//#####



//#S######.#

//......#..#

//.#.##.##.#

//.#........

//##.##.####

//....#....#

//.#######.#

//....#.....

//.####.###.

//....#...G#