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

#include <cstdio>

#include <cstdlib>

#include <algorithm>

#include <cstring>

#include <queue>

using namespace std;

int r,c,n;

int ans;

char map[1005][1005];

bool book[1005][1005];

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

struct point

{

    int x,y;

    int step;

}p[11],now,last;

bool check(int x,int y)

{

    if(x<0||x>=r||y<0||y>=c)

        return 0;

    return 1;

}

void bfs(int st)

{

    p[st].step=0;

    queue<point> q;

    now=p[st];

    q.push(now);

    book[now.x][now.y]=1;

    while(!q.empty())

    {

        now=q.front();

        q.pop();

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

        {

            last.x=now.x+dir[i][0];

            last.y=now.y+dir[i][1];

            last.step=now.step+1;

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

            if(map[last.x][last.y]=='X'|| book[last.x][last.y]==1)    continue;

            if(last.x==p[st+1].x && last.y==p[st+1].y)

            {

                ans+=last.step;

                return;

            }

            book[last.x][last.y]=1;

            q.push(last);

        }

    }

}

int main()

{

    while(~scanf("%d%d%d",&r,&c,&n))

    {

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

        {

            getchar();

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

            {

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

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

                {

                    p[0].x=i;

                    p[0].y=j;

                }

                if(map[i][j]!='S'&&map[i][j]!='X'&&map[i][j]!='.')

                {

                    p[map[i][j]-'0'].x=i;

                    p[map[i][j]-'0'].y=j;

                }

            }

        }

        ans=0;

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

        {

            memset(book,0,sizeof(book));

            bfs(i);

        }

        cout<<ans<<endl;

    }

    //system("pause");

    return 0;

}