// 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.0669931210092685x):
#include<cstdio>

#include<iostream>

#include<vector>

#include<map>

#include<set>

#include<queue>

#include<algorithm>

#include<string>

#include<cstring>

#include<cstdlib>

#include<cctype>

#include<sstream>

#include<stack>

#include<functional>

using namespace std;

typedef long long ll;

typedef pair<int, int> P;

const int INF=0x3f3f3f3f;

const int MAX_N = 10;

const int MAX_H = 1005;

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

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

struct T{

    int x,y;

};//坐?

char G[MAX_H][MAX_H];

T aim[MAX_N];

int vis[MAX_H][MAX_H]; // ???数

int H, W, N;

T s, t; //起点，?点



int bfs(){

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

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

            vis[i][j] = -1;

    queue<T> q;

    q.push(s);

    vis[s.x][s.y] = 0;

    while(!q.empty()){

        T cur = q.front();

        q.pop();

        if(cur.x == t.x && cur.y == t.y)break;

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

            T nxt;

            nxt.x = cur.x + dx[i];

            nxt.y = cur.y + dy[i];

            if(nxt.x >= 0 && nxt.x < H && nxt.y >= 0 && nxt.y < W && vis[nxt.x][nxt.y] == -1 && G[nxt.x][nxt.y] != 'X'){

                q.push(nxt);

                vis[nxt.x][nxt.y] = vis[cur.x][cur.y] + 1;

            }

        }

    }

    return vis[t.x][t.y];

}



void solve(){

    int ans = 0;

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

        t = aim[i]; //?个?酪??点

        ans += bfs();

        s = t; //?次?点??下次起点

        G[s.x][s.y] = '.';

	}

	cout << ans << endl;

}

int main()

{

	#ifdef LOCAL

		freopen("in.txt","r",stdin);

	//	freopen("out.txt","w",stdout);

	#endif

	//ios::sync_with_stdio(false);

	cin >> H >> W >> N;

	int p = 0;

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

        scanf("%s", G[i]);

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

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

                s.x = i;s.y =j;

                G[i][j] = '.';

            }

            else if(G[i][j] <= '9' && G[i][j] >= 0){

                int num = G[i][j] - '0' - 1;

                aim[num].x = i;aim[num].y = j;

            }

        }

	}

	solve();

	return 0;

}