// 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: 2.4796152146131396x):
#include <iostream>

#include <cmath>

#include <queue>

#include <cstdio>

#include <cstring>

#include <algorithm>



#define INF 0x7f7f7f7f

#define PI acos(-1.0)



#define L(x)    (x) << 1

#define R(x)    (x) << 1 | 1

#define lowbit(x)   (x)&(-x)

#define Mid(l, r)   (l + r) >> 1

#define Min(x, y)   (x) < (y) ? (x) : (y)

#define Max(x, y)   (x) < (y) ? (y) : (x)

#define iabs(x)     (x) < 0 ? -(x) : (x)

#define CLR(arr, val)    memset(arr, val, sizeof(arr))



using namespace std;



char map[1024][1024];

bool vis[1024][1024];

int cur, h, w;

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



struct node {

	int x, y, step;

} sta, now;



bool check(int x, int y) {

	if (x >= 0 && x < h && y >= 0 && y < w && !vis[x][y] && map[x][y] != 'X')	return true;

	return false;

}







void bfs(node s) {

	memset(vis, 0, sizeof(vis));



	queue<node> que;

	s.step = 0;

	que.push(s);

	node next;

	while (!que.empty()) {

//		cout << "h\n";

		s = que.front();

//		cout << s.x << ' ' << s.y << ' ' << cur << endl;

		que.pop();

		if (map[s.x][s.y] == cur + '0')	{

			sta = s;

			return ;

//			s.step;

		}



//		cout << "a\n";

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

//			cout << i << endl;

			next.x = s.x + dir[i][0];

			next.y = s.y + dir[i][1];

			next.step = s.step + 1;

			if (check(next.x, next.y)) {

				vis[next.x][next.y] = true;

				que.push(next);

			}



		}

	}

}



int main() {

	int n;

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

//		memset(map, 0, sizeof(map));

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

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

				cin >> map[i][j];

				if (map[i][j] == 'S')	sta.x = i, sta.y = j, map[i][j] = '.';

			}

		}

		sta.step = 0;

		int ans = 0;

		for (cur = 1; cur <= n; cur++) {

			bfs(sta);

			ans += sta.step;

			//cout << ans << endl;

		}

		cout << ans << endl;

	}

	return 0;

}