// Slower C++ solution:
//AOJ-0558

#include <cstdio>

#include <cstring>

#include <queue>

#include <algorithm>

using namespace std;



char map[1010][1010];

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

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

int vis[1010][1010];

int m, n, N;

struct node{

	int x, y, t;

};



node bfs(int x, int y, char k)

{

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

	queue<node> q;

	node s, e, p;

	s = {x, y, 0};

	q.push(s); vis[x][y] = 1;

	while(!q.empty())

	{

		p = q.front(); q.pop();

		if(map[p.x][p.y] == k) return p;

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

		{

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

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

			if(nx >= n || nx < 0 || ny >= m || ny < 0 || map[nx][ny] == 'X') continue;

			if(!vis[nx][ny]){

				vis[nx][ny] = 1;

				e = {nx, ny, p.t + 1};

				q.push(e);

			}	

		}

	}

}





int main()

{

	while(~scanf("%d %d %d", &n, &m, &N))

	{

		node s;

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

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

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

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

			   	 s.x = i; s.y = j; s.t = 0;

			   }

			}	

		}

		int ans = 0;

		char c = '0';

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

		{

			node t = bfs(s.x, s.y, c + i);

			ans += t.t;

			s.x = t.x; s.y = t.y;

		}

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

	}

	return 0; 

}


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

}