// Original C++ solution (slower):
#include <cstdio>

#include<cstring>

#include <algorithm>

#include <queue>

#define max 1005

using namespace std;

char map[max][max];

bool vis[max][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 st,char ed)

{

    queue<node>q;

    q.push(st);

    vis[st.x][st.y]=1;

    while(!q.empty())

    {

        node now=q.front();

        q.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&&map[next.x][next.y]!='X'&&!vis[next.x][next.y])

            {

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

                {

                    ans+=next.time;

                    next.time=0;

                    start=next;

                    return ;

                }

                else

                {

                    q.push(next);

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

                }

            }

        }

    }

}

int main(void)

{

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

    {

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

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

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

        {

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

        	{

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

                {

                start.x = i;

                start.y = j;

                start.time = 0;

                }

			}

		}

        ans=0;

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

        {

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

            BFS(start,'0'+i);

        }

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

    }

    return 0;

}

// Optimized solution to the same problem (faster):
#include <iostream>

#include <queue>

using namespace std;

int w, h, n;

char map[1024][1024];

int d[1024][1024];

const int direction[4][2] = {

	{ -1, 0 },

	{ 1, 0 },

	{ 0, -1 },

	{ 0, 1 },

};



int factory[16][2];

typedef pair<int, int> P;



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

{

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

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

	{

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

		{

			d[j][i] = -1;

		}

	}

	queue<P> que;

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

	d[sx][sy] = 0;

	while (que.size())

	{

		const 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 + direction[i][0];

			int ny = p.second + direction[i][1];

			// 是否可以移?，并且?点没有???

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

			{

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

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

			}

		}

	}



	return d[gx][gy];

}



///////////////////////////SubMain//////////////////////////////////

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

{



	cin >> h >> w >> n;

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

	{

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

		{

			cin >> map[j][i];

		}

	}



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

	{

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

		{

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

			{

				factory[0][0] = j;

				factory[0][1] = i;

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

			}

			else if (isdigit(map[j][i]))

			{

				int index = map[j][i] - '0';

				factory[index][0] = j;

				factory[index][1] = i;

			}

		}

	}



	int step = 0;

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

	{

		

		step += bfs(factory[i][0], factory[i][1], factory[i + 1][0], factory[i + 1][1]);

	}



	cout << step << endl;



	return 0;

}

// Fastest known solution for this problem: