// Original C++ solution (slower):
#include <stdio.h>

#include <string.h>

#include <iostream>

#include <algorithm>

#include <math.h>

#include <vector>

#include <queue>

using namespace std;

int n,m,p;

int sx,sy;

char s[1505][1505];

struct node{

	int x,y,step;

	node(int sx,int sy,int step)

	{

		this->x=sx;this->y=sy;this->step=step;

	}

};

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

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

int vis[1505][1500];

node bfs(node u,int pos)

{

	queue<node> q;

	while(!q.empty())q.pop();

	q.push(u);

	

	while(!q.empty())

	{

		node v=q.front();q.pop();

		if(s[v.x][v.y]==pos+'0') return v;

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

		{

			int x=v.x+dx[i];

			int y=v.y+dy[i];

			if(x>=1&&x<=n&&y>=1&&y<=m&&s[x][y]!='X'&&vis[x][y]==0)

			{

				vis[x][y]=1;

				node now=node(x,y,v.step+1);

				q.push(now);

			}

		}

	}

}

int main()

{

	while(cin>>n>>m>>p)

	{

		int ans=0;

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

		{

			cin>>s[i]+1;

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

			{

				if(s[i][j]=='S') sx=i,sy=j;

			}

		}

		node u=node(sx,sy,0);	

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

		{		

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

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

			u=bfs(u,i);

			ans+=u.step;

			u.step=0;

		}

		cout<<ans<<endl;

	}

	return 0;

}


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

#include <queue>

using namespace std;

const int INF = 100000000;

typedef pair<int,int> Point;

Point AllPoint[1000];

int W,H;

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

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

char map[1000][1000];

int deep[1000][1000];

int N;

void BFS(Point Start,Point Over){

    queue<Point> que;

    int FirstDeep = deep[Start.first][Start.second];

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

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

            deep[i][j] = INF;

        }

    }

    que.push(Start);

    deep[Start.first][Start.second] = FirstDeep;

    while (que.size()){

        Point p = que.front();

        que.pop();

        if (p.first== Over.first&&p.second == Over.second)

            break;

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

            int ix = p.first+x[i];

            int iy = p.second+y[i];

            if (ix>=0&&ix<=H

                &&iy>=0&&iy<=W

                &&deep[ix][iy] == INF

                &&map[ix][iy]!='X'){

                que.push(Point(ix,iy));

                deep[ix][iy] = deep[p.first][p.second]+1;

            }

        }

    }

}







int main(){

    cin >> H >> W;

    cin >> N;

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

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

            cin >> map[i][j];

            if ((int)map[i][j]-48>=1&&(int)map[i][j]-48<=N){

                AllPoint[(int)map[i][j]-48].first = i;

                AllPoint[(int)map[i][j]-48].second = j;



            }

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

                AllPoint[0].first = i;

                AllPoint[0].second = j;

            }

        }

    }

    deep[AllPoint[0].first][AllPoint[0].second] = 0;

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

        BFS(AllPoint[i],AllPoint[i+1]);

    }

    cout << deep[AllPoint[N].first][AllPoint[N].second]<<endl;



    return 0;

}

//#....

//S.##.

//###G.

//#####

//#####



//#S######.#

//......#..#

//.#.##.##.#

//.#........

//##.##.####

//....#....#

//.#######.#

//....#.....

//.####.###.

//....#...G#






// Fastest known solution for this problem: