// 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: 5.110284647703048x):


#include <iostream>   

#include<stack> 

#include<queue> 

#include<string> 

#include <math.h>

using namespace std;



const int MAX_H = 1000;

const int MAX_W = 1000;

const int a = 100000000;



typedef pair<int, int> P;



int H = 0, W = 0, N = 0; 

 

int food[11][2];

char cheese[MAX_H][MAX_W] = {0}; 

int r[MAX_H][MAX_W]; 

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



int search(int &sx, int &sy, int &ex, int &ey)

{

 

    P p;

    queue<P> que;

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

   {

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

       {

           r[i][j] = a;



        }

    }

    que.push(P(sx,sy));

    r[sx][sy] = 0;



    while(que.size())

   {  

        p = que.front();

        que.pop();

        if(p.first == ex && p.second == ey)

			break;

	



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

	   {

		    int xn = p.first + dx[i];

		    int yn = p.second + dy[i];





		    if(xn >= 0 && xn < H && yn >= 0 && yn < W && cheese[xn][yn]!='X' && r[xn][yn] == a )

		   {

			    que.push(P(xn,yn));

			    r[xn][yn] = r[p.first][p.second] + 1;

		    }

	    }



	}

	return r[ex][ey];

}



int main()

{

	int dis = 0;

    cin >> H >> W >> N;

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

   {

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

       {

          cin >> cheese[i][j];

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

         {

             food[0][0] = i;

             food[0][1] = j;

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

          }

          if(cheese[i][j]>48&&cheese[i][j]<58)

         {

             int num = cheese[i][j] - 48;  

             food[num][0] = i;

             food[num][1] = j;

          }

       }

    }

   



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

   {

       dis += search(food[i][0], food[i][1], food[i+1][0], food[i+1][1]); 

    }

    

	cout << dis << endl;

    return 0;

}