// 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: 4.470340411395128x):
#include <iostream>

#include <queue>

#include <string>

using namespace std;

int main()

{

	int h,w,n;

	int sx,sy;

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

	string map[1001];

	int d[1001][1001];

	int x = 0;

	cin >> h >> w >> n;

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

	{

		cin >> map[i];

	}

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

	{

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

		{

			d[j][i] = -1;

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

			{

				sx = j;

				sy = i;

			}

		}

	}

	d[sx][sy] = 0;

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

	{

		queue< pair<int, int> > que;

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

		{

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

			{

				d[k][j] = -1;

			}

		}

		d[sx][sy] = 0;

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

		while(que.size())

		{

			pair<int, int> p = que.front();

			que.pop();

			if((map[p.second][p.first] - '0') == (i+1))

			{

				sx = p.first;

				sy = p.second;

				x+=d[sx][sy];

				break;

			}

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

			{

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

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

				if(0 <= nx && nx < w && 0 <= ny && ny < h && map[ny][nx] != 'X' && d[nx][ny] == -1)

				{

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

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

				}

			}

		}

	}

	cout << x << endl;

}