// Slower C++ solution:
#include "cstring"

#include "cstdio"

#include "queue"

using namespace std;



const int maxn=1024;



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

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



char mp[maxn][maxn];

int vis[maxn][maxn];

int wx[10],wy[10];

int n,m,k;



int bfs(int x1,int y1,int x2,int y2)

{

	memset(vis,0,sizeof(vis));

	queue<int>qx;qx.push(x1);

	queue<int>qy;qy.push(y1);

	queue<int>qt;qt.push(0);

	vis[x1][y1]=1;

	

	while(!qx.empty())

	{

		int nx=qx.front();qx.pop();

		int ny=qy.front();qy.pop();

		int nt=qt.front();qt.pop();

		

		if(nx==x2 && ny==y2)return nt;

		

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

		{

			int px=nx+dx[i];

			int py=ny+dy[i];

			if(px>=0 && py>=0 && px<n && py<m && mp[px][py]!='X' && !vis[px][py])

			{

				qx.push(px);

				qy.push(py);

				qt.push(nt+1);

				vis[px][py]=1;

			}

		}

		

	}

	

	return 0;

}



int main()

{

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

	scanf("%d%d%d",&n,&m,&k);

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

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

		{

			mp[i][j]=getchar();

			while(mp[i][j]=='\n' || mp[i][j]=='\0')mp[i][j]=getchar();

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

			{

				wx[mp[i][j]-'0']=i;

				wy[mp[i][j]-'0']=j;

			}

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

			{

				wx[0]=i;

				wy[0]=j;

			}	

		}

	int ans=0;

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

		ans+=bfs(wx[i],wy[i],wx[i+1],wy[i+1]);

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

	return 0;

} 


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

}