// Slower C++ solution:
#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.0758276366755695x):
#include<iostream>

#include<queue>

using namespace std;



typedef struct{

 int x;

 int y;

}P;//表示坐?



char graph[1000][1000];

P aim[10];

P s,g;//s起点，g?点

int N,W,H;

int vis[1000][1000]; //用来存?距?

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



int bfs(P s,P g);



int main(){

    int i,j,t,step=0;

    cin>>H>>W>>N;

    

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

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

        cin>>graph[i][j];

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

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

         aim[t].x=i;

         aim[t].y=j;

      } //把1-N的坐?保存在 aim数?中 

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

          s.x=i;

          s.y=j;

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

      }//得到最初的起点 

    }

    

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

       g.x=aim[i].x;

       g.y=aim[i].y;//1-N ，从小到大，?次把一个 作??点 

       step+=bfs(s,g);

       s.x=g.x;

       s.y=g.y;//到??个?点后??点?起点 

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

    }

   

    cout<<step<<endl;

}



int bfs(P s,P g){

    queue<P> que;

      P n;

    

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

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

     vis[i][j]=-1;

     

     que.push(s);

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

     

     while(que.size()){

         

       P p=que.front();

         que.pop();

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

       break;

       

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

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

           n.y=p.y+dy[i];//向四方漫射 

        if(0<=n.x&&n.x<H&&0<=n.y&&n.y<W&&graph[n.x][n.y]!='X'&&vis[n.x][n.y]==-1){

               que.push(n);

               vis[n.x][n.y]=vis[p.x][p.y]+1;

           }

       }

   }

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

}