// Slower C++ solution:
/*

 * hujx.cpp

 *

 *  Created on: 2015年5月21日

 *      Author: dell

 */

#include<stdio.h>

#include<iostream>

#include<string>

#include<string.h>

#include<algorithm>

#include<iomanip>

#include<vector>

#include<time.h>

#include<queue>

#include<stack>

#include<iterator>

#include<math.h>

#include<stdlib.h>

#include<limits.h>

#include<map>

#include<set>

//#define ONLINE_JUDGE

#define eps 1e-8

#define INF 0x7fffffff

#define FOR(i,a) for((i)=0;i<(a);(i)++)

#define MEM(a) (memset((a),0,sizeof(a)))

#define sfs(a) scanf("%s",a)

#define sf(a) scanf("%d",&a)

#define sfI(a) scanf("%I64d",&a)

#define pf(a) printf("%d\n",a)

#define pfI(a) printf("%I64d\n",a)

#define pfs(a) printf("%s\n",a)

#define sfd(a,b) scanf("%d%d",&a,&b)

#define sft(a,b,num) scanf("%d%d%d",&a,&b,&num)

#define for1(i,a,b) for(int i=(a);i<b;i++)

#define for2(i,a,b) for(int i=(a);i<=b;i++)

#define for3(i,a,b)for(int i=(b);i>=a;i--)

#define MEM1(a) memset(a,0,sizeof(a))

#define MEM2(a) memset(a,-1,sizeof(a))

#define ll __int64

const double PI=acos(-1.0);

template<class T> T gcd(T a,T b){return b?gcd(b,a%b):a;}

template<class T> T lcm(T a,T b){return a/gcd(a,b)*b;}

template<class T> inline T Min(T a,T b){return a<b?a:b;}

template<class T> inline T Max(T a,T b){return a>b?a:b;}

using namespace std;

//#pragma comment(linker,"/STACK:1024000000,1024000000")

int n,m,k;

#define M 110

#define N 500010

#define Mod 258280327

#define p(x,y) make_pair(x,y)

char ch[1010][1010];

int vis[1010][1010];

int posx[15];

int posy[15];

struct Node{

	int x,y;

	int step;

};

bool check(int x,int y){

	if(x<0 || x>=n || y<0 || y>=m || ch[x][y] == 'X' ||vis[x][y])

		return false;

	return true;

}

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

int bfs(int stx,int sty,int edx,int edy){

	memset(vis,0,sizeof vis);

	queue<Node> q;

	Node cur,next;

	cur.x = stx;

	cur.y = sty;

	cur.step = 0;

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

	q.push(cur);

	int ans;

	while(!q.empty()){

		cur = q.front();

		q.pop();

		if(cur.x == edx && cur.y == edy){

			ans = cur.step;

			break;

		}

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

			next = cur;

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

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

			next.step ++;

			if(!check(next.x,next.y)) continue;

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

			q.push(next);

		}

	}

	return ans;

}

int main(){

	while(scanf("%d%d%d",&n,&m,&k)!=EOF){

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

			sfs(ch[i]);

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

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

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

					posx[0]=i;

					posy[0]=j;

				}

				if(ch[i][j]>='1' && ch[i][j]<='9'){

					posx[ch[i][j]-'0'] = i;

					posy[ch[i][j]-'0'] = j;

				}

			}

		}

		int ans=0;

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

			ans += bfs(posx[i],posy[i],posx[i+1],posy[i+1]);

		}

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

	}

    return 0;

}

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

#include<stdio.h>

#include<stdlib.h>

#include<string.h>

#include<string>

#include<math.h>

#include<queue>

using namespace std;



struct data_Node{

	int x,y,time;

};



char dataSet[1001][1001];

bool visited[1001][1001];

int H,W,N;



int total_time,total_storage,cnt_storage,cnt_time;

int start_row,start_cell;

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

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







void Find_Short_Path (int x,int y)

{

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

	data_Node temp_data;

	temp_data.x = x;

	temp_data.y = y;

	temp_data.time = 0;

	visited[x][y] = true;

	cnt_time = 0;

	queue<data_Node> temp_queue;

	temp_queue.push(temp_data);

	while (temp_queue.empty() != true)

	{

		data_Node u = temp_queue.front();

		temp_queue.pop();

		if ((int)(dataSet[u.x][u.y] - '0') == total_storage)

		{

			start_row = u.x;

			start_cell = u.y;

			cnt_time = u.time;

			break;

		}

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

		{

			int dx = u.x + control_x[i];

			int dy = u.y + control_y[i];

			if (dataSet[dx][dy] != 'X' && dx >= 0 && dx< H && dy >= 0 && dy < W)

			{

				data_Node v;

				v.x = dx;

				v.y = dy;	

				v.time = u.time + 1;//此?有坑

				if (visited[dx][dy] == false)

				{

					visited[dx][dy] = true;

					temp_queue.push(v);

				}

			}

		}

	}

}



int main ()

{



		cin>>H>>W>>N;

		memset(dataSet,'X',sizeof(dataSet));

		total_storage = 1;

		total_time = 0;

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

		{

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

			{

				cin>>dataSet[i][j];

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

				{

					start_row = i;

					start_cell = j;

				}

			}

		}

		int ans = 0;

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

		{

			total_storage = i;

			Find_Short_Path(start_row,start_cell);

			ans += cnt_time;

		}

		cout<<ans<<endl;



	return 0;

}