#include <stdio.h>
#include <stack>
#include <math.h>
#include <queue>

using namespace std;

int height,width,N;
char** table;
bool** checkTable;

struct Info{
	int cheese_id,row,col;
};

struct Data{
	int row,col,dist;
	char pre_move;
};

bool rangeCheck(int row,int col){
	if(row >= 0 && row <= height-1 && col >=0 && col <= width-1)return true;
	else{
		return false;
	}
}

int func(int row,int col,int goal_row,int goal_col,int dist){
	queue<Data> Q;
	Data data,tmp;
	data.row = row;
	data.col = col;
	data.dist = 0;
	data.pre_move = 'F';
	Q.push(data);

	checkTable[row][col] = true;

	int ret = 0;

	while(!Q.empty()){
		tmp = Q.front();
		Q.pop();

		if(tmp.col == goal_col && tmp.row == goal_row){
			ret = tmp.dist;
			break;
		}

		if(rangeCheck(tmp.row-1,tmp.col) == true && tmp.pre_move != 'D' && table[tmp.row-1][tmp.col] != 'X' && checkTable[tmp.row-1][tmp.col] == false){
			Data new_data;
			new_data.row = tmp.row-1;
			new_data.col = tmp.col;
			new_data.dist = tmp.dist+1;
			new_data.pre_move = 'U';
			checkTable[tmp.row-1][tmp.col] = true;
			Q.push(new_data);
		}
		if(rangeCheck(tmp.row+1,tmp.col) == true && tmp.pre_move != 'U' && table[tmp.row+1][tmp.col] != 'X'&& checkTable[tmp.row+1][tmp.col] == false){
			Data new_data;
			new_data.row = tmp.row+1;
			new_data.col = tmp.col;
			new_data.dist = tmp.dist+1;
			new_data.pre_move = 'D';
			checkTable[tmp.row+1][tmp.col] = true;
			Q.push(new_data);
		}
		if(rangeCheck(tmp.row,tmp.col-1) == true && tmp.pre_move != 'R' && table[tmp.row][tmp.col-1] != 'X'&& checkTable[tmp.row][tmp.col-1] == false){
			Data new_data;
			new_data.row = tmp.row;
			new_data.col = tmp.col-1;
			new_data.dist = tmp.dist+1;
			new_data.pre_move = 'L';
			checkTable[tmp.row][tmp.col-1] = true;
			Q.push(new_data);
		}
		if(rangeCheck(tmp.row,tmp.col+1) == true && tmp.pre_move != 'L' && table[tmp.row][tmp.col+1] != 'X'&& checkTable[tmp.row][tmp.col+1] == false){
			Data new_data;
			new_data.row = tmp.row;
			new_data.col = tmp.col+1;
			new_data.dist = tmp.dist+1;
			new_data.pre_move = 'R';
			checkTable[tmp.row][tmp.col+1] = true;
			Q.push(new_data);
		}
	}

	return ret;
}

int main(){

	scanf("%d %d %d",&height,&width,&N);
	table = new char*[1000];
	checkTable = new bool*[1000];
	Info info[N+1];
	int info_index = 0,total_dist = 0;


	for(int i=0; i < height; i++){
		table[i] = new char[1001];
		checkTable[i] = new bool[1000];

		scanf("%s",table[i]);

		for(int k = 0; k <width; k++){
			checkTable[i][k] = false;

			if(table[i][k] == 'S'){
				info[info_index].cheese_id = 0;
				info[info_index].row = i;
				info[info_index].col = k;
				info_index++;
			}else if(table[i][k] >= '1' && table[i][k] <= '9'){
				info[info_index].cheese_id = table[i][k] - '0';
				info[info_index].row = i;
				info[info_index].col = k;
				info_index++;
			}
		}
	}

	//???????????????????????±?????????????????????
	for(int i=1; i <= N; i++){
		for(int k=N; k >= i; k--){
			if(info[k].cheese_id < info[k-1].cheese_id)swap(info[k],info[k-1]);
		}
	}

	//i????????????????????????i+1???????????????????????§??????????????¢?????¨??¢?´¢??§?±???????
	for(int i = 0; i <= N-1; i++){
		total_dist += func(info[i].row,info[i].col,info[i+1].row,info[i+1].col,0);
		for(int k = 0; k < height; k++){
			for(int p = 0; p < width; p++)checkTable[k][p] = false;
		}
	}

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

    return 0;
}

Measured memory_bytes: 4744