// Slower C++ solution:
/************************************************

* Author        :somniloquy

* Created Time  :2015/10/30 16:15:09

 ************************************************/



#include <cstdio>

#include <algorithm>

#include <iostream>

#include <cstring>

#include <queue>



using namespace std;



const int hw_max = 1000 + 5;

char graph[hw_max][hw_max];

int sign[hw_max][hw_max];

int h, w, n, ans;

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

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

struct node

{

	int x;

	int y;

	int time;

} start;



void BFS(node op, char ed)

{

	queue <node> process;

	process.push(op);

	sign[op.x][op.y] = 1;

	while(!process.empty())

	{

		node now = process.front();

		process.pop();

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

		{

			node next = now;

			next.x += dx[i];

			next.y += dy[i];

			next.time ++;

			if(next.x >= 0 && next.x < h && next.y >= 0 && next.y < w && graph[next.x][next.y] != 'X' && !sign[next.x][next.y])

			{

				if(graph[next.x][next.y] == ed)

				{

					ans += next.time;

					next.time = 0;

					start = next;

				       	return;

				}

				else

				{

					process.push(next);

					sign[next.x][next.y] = 1;

				}

			}

		}

	}

}



int main()

{

	while(~scanf("%d %d %d", & h, & w, & n))

	{

		getchar();

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

		{

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

			{

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

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

			       {

					start.x = i;

					start.y = j;

					start.time = 0;

			       }

			}

			getchar();

		}

		ans = 0;

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

		{

			memset(sign, 0, sizeof(sign));

			BFS(start, '0' + i);

		}

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

	}

	return 0;

}

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

#include<queue>

#include<cstdio>

using namespace std;

const int INF=1000000;

int d[1005][1005];

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

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

char s[1005][1005];

int m,n,k;

//int sx,sy,gx,gy;

typedef pair <int,int >P;

P aim[15];

int isd(char ch)

{

    return ch>='0'&&ch<='9'?1:0;

}

int bfs(int sx,int sy,int gx,int gy)

{

    queue<P> que;

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

    {

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

        {

            d[i][j]=INF;

//            cout <<d[i][j]<<endl;

        }

    }

    que.push(P(sx,sy));

    d[sx][sy]=0;

    while(que.size())

    {



        P p=que.front();

        que.pop();

        if(p.first==gx&&p.second==gy)

        {

            break;

        }

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

        {

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

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

//            cout << nx << ":" << ny << endl;

//            cout << que.size()<<" "<<m<<" "<<n<<endl;

            if( nx>=0&&nx<m

                    &&ny>=0&&ny<n

                    &&s[nx][ny]!='X'

                    &&d[nx][ny]==INF)

            {



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



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

            }

        }

    }

    return d[gx][gy];

}

int main()

{

    cin >> m >> n >> k;

    int sum = 0;

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

    {

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

        {

            cin >> s[i][j];

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

            {

                aim[0].first=i;

                aim[0].second=j;

//                cout << i <<" "<< j <<endl;

            }

            if(isd(s[i][j]))

            {

                aim[s[i][j]-'0'].first=i;

                aim[s[i][j]-'0'].second=j;

//                cout << i << " "<<j <<endl;

            }



        }





    }

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

    {

        int steep=bfs(aim[i].first,aim[i].second,aim[i+1].first,aim[i+1].second);

        sum+=steep;

//            cout << steep <<endl;

    }

    cout << sum <<endl ;

}