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

#include<cstdio>

#include<cstring>

#include<iostream>

#include<algorithm>

#define SC(x) scanf("%d",&x);

#define pr(x)  cout << x << endl;

#define mem(arr,v) memset(arr,v,sizeof arr);

#define bug(x) cout << "#x = " << x <<endl;

const int inf = 0x3f3f3f3f;

const int maxn = 1010;

typedef long long ll;

using namespace std;

char Mz[maxn][maxn],c;

bool vis[maxn][maxn];

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

int M,N,C;

int px[10],py[10];

struct node {

	int x,y,step;

};



int bfs(int des,int sx,int sy) {

	mem(vis,0);

	vis[sx][sy] = 1;

	node nod = {sx,sy,0};

	queue<node> q;

	q.push(nod);



	while(!q.empty()) {

		nod =  q.front();

		q.pop();

		

		int x = nod.x, y = nod.y;

		int step =  nod.step;

		

		if(Mz[x][y] == des + '0')

			return step;

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

			int dx = x + dir[i][0];

			int dy = y + dir[i][1];

			if(Mz[dx][dy] == 'X' || Mz[dx][dy] == -1 || vis[dx][dy])	continue;

			nod = {dx,dy,step + 1};

			q.push(nod);

			vis[dx][dy] = 1;

		}

	}

}



int main() {

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

	cin >> N >> M >> C;

	mem(Mz,-1)

	mem(px,0)  mem(py,0)



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

		for(int j = 1; j <= M ; j++) {

			cin >> c;

			Mz[i][j] = c;

			if(c == '.' || c == 'X')	continue;

			if(c == 'S') px[0] = i, py[0] = j;

			else

				px[c - '0'] = i , py[c - '0'] = j;

		}

	int res = 0;

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

		if(px[i] && py[i]) {

//				dfs(i + 1,px[i],py[i],0);

			res += bfs(i + 1,px[i],py[i]);

		}

	}

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

}

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

#include<cstdio>

#include<cstring>

#include<queue>

using namespace std;



const int DIRECTSIZE = 4;

struct _direct {

    int drow;

    int dcol;

} direct[DIRECTSIZE] = {{0, -1}, {0, 1}, {-1, 0}, {1, 0}};



const int N = 1000;

const int N2 = 10;

char grid[N][N], visited[N][N];

int h, w, n;

struct _factory {

    int row;

	int col;

} factory[N2];



struct _node {

    int row;

	int col;

	int level;

};



int bfs(int n, char goal) {

    memset(visited, 0, sizeof(visited));

    _node start={factory[n-1].row, factory[n-1].col, 0}, temp;

    visited[start.row][start.col] = 1;

    queue<_node> q;

    q.push(start);

    while(!q.empty()) {

        _node front = q.front();

        q.pop();

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

            int nextrow = front.row + direct[i].drow;

            int nextcol = front.col + direct[i].dcol;

            if( 0 <= nextrow && nextrow < h && 0 <= nextcol && nextcol < w) {

                if(grid[nextrow][nextcol] == goal) {

                    return front.level + 1;

                }

				else if(visited[nextrow][nextcol] == 0 && grid[nextrow][nextcol] != 'X') {

                    visited[nextrow][nextcol] = 1;

                    temp.row = nextrow;

                    temp.col = nextcol;

                    temp.level = front.level + 1;

                    q.push(temp);

                }

            }

        }

    }

    return 0;

}



int main() {

    while (~scanf("%d %d %d", &h, &w, &n)){

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

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

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

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

                    factory[0].row = i;

                    factory[0].col = j;

				}

				else if(isdigit(grid[i][j])) {

                    factory[grid[i][j] - '0'].row = i;

                    factory[grid[i][j] - '0'].col = j;

                }

            }

		}

        int minstep = 0;

        char goal = '1';

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

            minstep += bfs(i, goal);

            goal++;

        }

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

    }

    return 0;

}