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

#include <algorithm>

#include <queue>

#include <map>



using namespace std;



int h, w, n;

int sx, sy;

char input[1002][1002];

char stage[1002][1002];



typedef pair<int, int> P;



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

map<int, P> chs;





int solve(int xs, int ys, int cur)

{

	if(cur > n) return 0;

	queue<P> que;

	queue<int> count;

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

	{

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

		{

			stage[i][j] = input[i][j];

		}

	}

	que.push(P(xs, ys));

	count.push(0);

	while (!que.empty())

	{

		P now = que.front(); que.pop();

		int c = count.front(); count.pop();

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

		{

			P next = P(now.first + dir[i], now.second + dir[i + 1]);

			if(stage[next.second][next.first] == 'X') continue;

			if(next == chs[cur])

			{

				return solve(next.first, next.second, cur + 1) + c + 1;

			}

			stage[next.second][next.first] = 'X';

			que.push(next);

			count.push(c + 1);

		}

	}

	return 0;

}



int main()

{

	fill(input[0], input[0] + sizeof(input), 'X');

	cin >> h >> w >> n;

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

	{

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

		{

			cin >> input[i][j];

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

			{

				sy = i; sx = j;

			}

			char c = input[i][j] - '0';

			if(c < 10 && c > 0)

			{

				chs[c] = P(j, i);

			}

		}

	}

	cout << solve(sx, sy, 1) << endl;

	return 0;

}

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

#include <algorithm>

#include <string>

#include <vector>

#include <map>

#include <sstream>

#include <functional>

#include <numeric>

#include <cmath>

#include <cstring>

#include <cstdio>

#include <queue>

#include <stack>

#include <set>



using namespace std;



#define ALL(co) co.begin(), co.end()

#define tpl(...) make_tuple(__VA_ARGS__)



typedef long long LL;

typedef pair<int, int> P; typedef pair<P, int> PI; typedef pair<P, P> PP;

typedef vector<int> Array; typedef vector<vector<int> > Array2;

typedef vector<LL> LArray; typedef vector<P> PArray; typedef vector<string> SArray;



const int INF = 1 << 29;

const LL LINF = 1LL << 60;



inline int getInt() { int itiv; return (cin >> itiv, itiv); }

template <typename T> void readAll(vector<T>& vec) { for (int i = 0, size = vec.size(); i < size; i++) cin >> vec[i]; }

template <typename T, typename U> void readAll(vector<pair<T, U> >& vec) { for (int i = 0, size = vec.size(); i < size; i++) cin >> vec[i].first >> vec[i].second; }

template < typename T > inline string toString( const T &a ){ ostringstream oss; oss << a; return oss.str(); };

inline bool between(int min, int max, int n) { return min <= n && n <= max; }

inline bool inRange(int begin, int end, int n) { return begin <= n && n < end; }

inline bool inRange(int size, int n) { return 0 <= n && n < size; }



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

int dr[] = { 0, -1, 0, 1 }, dc[] = { -1, 0, 1, 0 };

bool visited[1000][1000];

char stage[1000][1000];

int h, w, n;



int main(void)

{

	ios::sync_with_stdio(false);

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

	{

		PArray goals(n + 1);

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

		{

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

			{

				cin >> stage[i][j];

				if(stage[i][j] == 'S') goals[0] = P(i, j);

				int num = stage[i][j] - '0';

				if(inRange(n + 1, num)) goals[num] = P(i, j);

			}

		}



		int ans = 0;

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

		{

			P start = goals[i], goal = goals[i + 1];

			memset(visited, false, sizeof(visited));

			queue<PI> que;

			que.push(PI(start, 0));

			while (!que.empty())

			{

				PI pi = que.front(); que.pop();

				if(goal == pi.first)

				{

					ans += pi.second;

					break;

				}

				int r = pi.first.first, c = pi.first.second;



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

				{

					int nr = r + dr[i], nc = c + dc[i];

					if(!inRange(h, nr) || !inRange(w, nc) || stage[nr][nc] == 'X' || visited[nr][nc]) continue;

					que.push(PI(P(nr, nc), pi.second + 1));

					visited[nr][nc] = true;

				}

			}

		}

		cout << ans << endl;

	}

	return 0;

}