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

#include<algorithm>

#include<math.h>

#include<string.h>

#include<queue>

#include<stdio.h>

using namespace std;



typedef struct{

	int x,y;

}p;

char a[1005][1005];

p acm[11];

p s,g;

int b[1005][1005];

int H,W,N;

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

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

int bfs(p s,p g)

{

	queue<p> q;

	p n;

	memset(b,-1,sizeof(b));

	q.push(s);

	b[s.x][s.y]=0;

	while(!q.empty())

	{

		p h;

		h=q.front();

		q.pop();

		if(h.x==g.x&&h.y==g.y)

		{

			break;

		}

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

		{

			n.x=h.x+dx[i];

			n.y=h.y+dy[i];

			if(n.x>=0&&n.x<H&&n.y>=0&&n.y<W&&a[n.x][n.y]!='X'&&b[n.x][n.y]==-1)

			{

				q.push(n);

				b[n.x][n.y]=b[h.x][h.y]+1;

			}

		}

	}

	return b[g.x][g.y];

}

int main()

{

	int i,j,t,step=0;

	scanf("%d%d%d",&H,&W,&N);

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

	{

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

	}

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

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

	{

		if(a[i][j]>='1'&&a[i][j]<='9')

		{

			t=a[i][j]-'0';

		    acm[t].x=i;

		    acm[t].y=j;

		}

		else if(a[i][j]=='S')

		{

			s.x=i;

			s.y=j;

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

		}

	}

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

		{

			g.x=acm[i].x;

			g.y=acm[i].y;

			step+=bfs(s,g);

			s.x=g.x;

			s.y=g.y;

			a[g.x][g.y]='.';

		}

	

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

	return 0;

 } 


// Faster C++ solution for the same problem (measured speedup: 2.5236933671784776x):
#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;

}