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

#include<deque>

#include<memory.h>



using namespace std;



int H, W, N;

char mapT[1000][1000];

int vis[1000][1000];

int temp_x = 0, temp_y;

char temp;

int final_min_route = 0;

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



struct location {

	int x, y, route;

};



location findSmallRouteBFS(int x, int y, char factory) {

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

	deque<location> que;

	location temp = { x,y,0 };

	que.push_back(temp);

	vis[x][y] = 1;

	while (!que.empty()) {

		location st = que.front();

		que.pop_front();

		if (mapT[st.x][st.y] == factory) {

			return st;

		}

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

			int t_x = st.x + dir[i][0];

			int t_y = st.y + dir[i][1];

			if (t_x < 0 || t_x >= H || t_y < 0 || t_y >= W || mapT[t_x][t_y] == 'X') {

				continue;

			}

			location temp = { t_x,t_y,st.route + 1 };

			if (!vis[t_x][t_y]) {

				vis[t_x][t_y] = 1;

				que.push_back(temp);

			}

		}

	}



}



int main() {

	cin >> H >> W >> N;

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

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

			cin >> temp;

			mapT[i][j] = temp;

			if (temp == 'S') {

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

				temp_x = i;

				temp_y = j;

			}

		}

	}



	for (int factory = 1; factory != N + 1; ++factory) {



		location lo = findSmallRouteBFS(temp_x, temp_y, mapT[temp_x][temp_y] + 1);

		temp_x = lo.x;

		temp_y = lo.y;

		final_min_route += lo.route;

	}



	cout << final_min_route << endl;

	//system("pause");

	return 0;

}

// Faster C++ solution for the same problem (measured speedup: 5.039064557264575x):


#include <iostream>   

#include<stack> 

#include<queue> 

#include<string> 

#include <math.h>

using namespace std;



const int MAX_H = 1000;

const int MAX_W = 1000;

const int a = 100000000;



typedef pair<int, int> P;



int H = 0, W = 0, N = 0; 

 

int food[11][2];

char cheese[MAX_H][MAX_W] = {0}; 

int r[MAX_H][MAX_W]; 

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



int search(int &sx, int &sy, int &ex, int &ey)

{

 

    P p;

    queue<P> que;

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

   {

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

       {

           r[i][j] = a;



        }

    }

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

    r[sx][sy] = 0;



    while(que.size())

   {  

        p = que.front();

        que.pop();

        if(p.first == ex && p.second == ey)

			break;

	



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

	   {

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

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





		    if(xn >= 0 && xn < H && yn >= 0 && yn < W && cheese[xn][yn]!='X' && r[xn][yn] == a )

		   {

			    que.push(P(xn,yn));

			    r[xn][yn] = r[p.first][p.second] + 1;

		    }

	    }



	}

	return r[ex][ey];

}



int main()

{

	int dis = 0;

    cin >> H >> W >> N;

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

   {

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

       {

          cin >> cheese[i][j];

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

         {

             food[0][0] = i;

             food[0][1] = j;

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

          }

          if(cheese[i][j]>48&&cheese[i][j]<58)

         {

             int num = cheese[i][j] - 48;  

             food[num][0] = i;

             food[num][1] = j;

          }

       }

    }

   



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

   {

       dis += search(food[i][0], food[i][1], food[i+1][0], food[i+1][1]); 

    }

    

	cout << dis << endl;

    return 0;

}