// Slower C++ solution:
#include <iostream>

#include<algorithm>

#include<cstdio>

#include<cstring>

#include<string>

#include<cstdlib>

#include<stack>

#include<queue>

#include <map>

#include<cmath>

#include<vector>

#define MAX_N 1000000

#define INF 0x3f3f3f3f

#define SIZE 1000

using namespace std;







struct note

{

    int x, y;

}start, ende;

typedef pair<int, int> P;

int H, W, N;

char a[1003][1003];

int sum;

int d[1003][1003];

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

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

void bfs(note h, note m)

{

    memset(d, -1, sizeof(d));

    queue<P> que;

    que.push(P(h.x, h.y));

    d[h.x][h.y] = 0;

    while (!que.empty())

    {

        P p = que.front();

        que.pop();

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

        {

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

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

            if (xx >= 0 && xx < H && yy >= 0 && yy < W && a[xx][yy] != 'X' && d[xx][yy] == -1)

            {

                d[xx][yy] = d[p.first][p.second] + 1;

                que.push(P(xx, yy));

            }

        }

    }

    sum += d[m.x][m.y];

    start = ende;

}

void solve()

{

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

    {

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

        {

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

            {

                start.x = i;

                start.y = j;

                i = H;

                j = W;

            }

        }

    }

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

    {

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

        {

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

            {

                if (a[i][j] == k + 49)

                {

                    ende.x = i;

                    ende.y = j;

                    bfs(start, ende);

                    i = H;

                    j = W;

                }

            }

        }

    }

    cout << sum << endl;

}

int main()

{

    cin >> H >> W >> N;

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

    {

        cin >> a[i];

    }

    sum = 0;

    solve();

    return 0;

}

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

}