// Slower C++ solution:
#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;

}

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

#include<queue>

#include<algorithm>

using namespace std;



typedef pair<int,int> P;



const int inf=10000;

const int maxh=1010;

const int maxw=1010;

const int maxn=9;



int h,w,n;

int d[maxh][maxw];

char m[maxh][maxw];



int bx,by;

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

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



int bfs(char c){

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

		fill(d[i],d[i]+w,inf);

	}

	d[bx][by]=0;

	queue<P> que;

	que.push(P(bx,by));

	while(!que.empty()){

		P p=que.front();

		que.pop();

		if(m[p.first][p.second]==c){

			bx=p.first,by=p.second;

			break;

		}

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

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

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

			if(0<=nx && nx<h && 0<=ny && ny<w && m[nx][ny]!='X' && d[nx][ny]==inf){

				que.push(P(nx,ny));

				d[nx][ny]=d[p.first][p.second]+1;

			}

		}

	}

	return d[bx][by];

}



void solve(){

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

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

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

				bx=i,by=j;

				break;

			}

		}

	}

	int ans=0;

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

		ans+=bfs('0'+i);

	}

	cout<<ans<<endl;

}



int main(){

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

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

			cin>>m[i];

		}

		solve();

	}

	return 0;

}