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

#include <queue>

#include <string>

#include <cstdio>

#include <cstring>

#include <utility>

using namespace std;

typedef pair<int, int> P;

char maze[1005][1005];

int d[1005][1005];

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

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

int h, w, n, hard; 

int si, sj;

const int INF = 0x3f3f3f3f;

int tot = 0; 



void BFS(int ai, int aj){

    queue<P> que;

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

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

            d[i][j] = INF;

        }

    }

    que.push(P(ai, aj));

    d[ai][aj] = 0;

    while(!que.empty()){

        P a = que.front();

        que.pop();

        int x = a.first, y = a.second;

        if(maze[x][y]-'0' == hard){

            tot += d[x][y];

            si = x; sj = y;

            break;

        }

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

            int dx = x+dirx[i], dy = y+diry[i];

            if(dx>=0 && dx<h && dy>=0 && dy<w && maze[dx][dy]!='X' && d[dx][dy]==INF){

                d[dx][dy] = d[x][y]+1;

                que.push(P(dx, dy));

            }

        }

    }

}



int main(int argc, char const *argv[]){

    while(scanf("%d%d%d", &h, &w, &n) != EOF){

        getchar();

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

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

                scanf("%c", &maze[i][j]);

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

                    si = i; sj = j;

                }

            }

            getchar();

        }

        tot = 0;

        hard = 1;

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

            BFS(si, sj);

            hard++;

        }

        cout << tot << endl;

    }

    return 0;

}