// Slower C++ solution:
#include<stdio.h>  

#include<string.h>  

#include<queue>  

using namespace std;  

const int maxn=1005;  

int n,m,k,dx[]={-1,1,0,0},dy[]={0,0,1,-1},vis[maxn][maxn];  

char map[maxn][maxn];  

struct maze  

{  

    int x,y,time;  

};  

maze bfs(int bx,int by,char ch)  

{  

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

    queue<maze> q;  

    maze st={bx,by,0};  

    q.push(st);vis[bx][by]=1;  

    while(!q.empty())  

    {  

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

        if(map[st.x][st.y]==ch)  

        {  

            return st;  

        }  

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

        {  

            int tx=st.x+dx[i],ty=st.y+dy[i];  

            if(tx<0||tx>=n||ty<0||ty>=m||map[tx][ty]=='X') 

            {  

                continue;  

            }  

            maze tp={tx,ty,st.time+1};  

            if(!vis[tx][ty])  

            {  

                vis[tx][ty]=1;  

                q.push(tp);  

            }  

        }  

    }  

}  

int main()  

{  

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

    {  

        int bx,by;  

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

        {  

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

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

            {  

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

                {  

                    bx=i;by=j;  

                    map[i][j]='0';  

                }  

            }  

        }  

        int ans=0;  

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

        {  

            maze tp=bfs(bx,by,map[bx][by]+1);  

            ans+=tp.time;  

            bx=tp.x;by=tp.y;  

        }  

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

    }  

    return 0;  

}


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

#include <queue>

#include <string>

#include <cstdio>

#include <cstring>

#include <utility>

using namespace std;

typedef pair<int, int> P;

char maze[1005][1005];

int d[1005][1005];

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

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

int h, w, n, hard; 

int si, sj;

const int INF = 0x3f3f3f3f;

int tot = 0; 



void BFS(int ai, int aj){

    queue<P> que;

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

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

            d[i][j] = INF;

        }

    }

    que.push(P(ai, aj));

    d[ai][aj] = 0;

    while(!que.empty()){

        P a = que.front();

        que.pop();

        int x = a.first, y = a.second;

        if(maze[x][y]-'0' == hard){

            tot += d[x][y];

            si = x; sj = y;

            break;

        }

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

            int dx = x+dirx[i], dy = y+diry[i];

            if(dx>=0 && dx<h && dy>=0 && dy<w && maze[dx][dy]!='X' && d[dx][dy]==INF){

                d[dx][dy] = d[x][y]+1;

                que.push(P(dx, dy));

            }

        }

    }

}



int main(int argc, char const *argv[]){

    while(scanf("%d%d%d", &h, &w, &n) != EOF){

        getchar();

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

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

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

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

                    si = i; sj = j;

                }

            }

            getchar();

        }

        tot = 0;

        hard = 1;

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

            BFS(si, sj);

            hard++;

        }

        cout << tot << endl;

    }

    return 0;

}