// Slower C++ solution:
#include <bits/stdc++.h>

/*

#include <ext/pb_ds/assoc_container.hpp>

#include <ext/pb_ds/tree_policy.hpp>

#include <ext/rb_tree>

*/

#define mem(x,y) memset(x,y,sizeof(x))

//#define P(x) cout<<#x<<" = "<<x<<endl;

#define PN(x,n) cout<<'{'<<x[0];for(int i=1;i<n;i++)cout<<','<<x[i];cout<<'}'<<endl;

#define RP(i,n) for(int i=0;i<n;i++)

#define RP2(i,a,b) for(int i=a;i<=b;i++)

#define mp make_pair

#define P pair<int,int>

#define pb push_back

#define fi first

#define se second

#define inf 0x3f3f3f3f

#define dinf 1e99

#define OJ

#define ite iterator

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

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

using namespace std;/*

using namespace __gnu_cxx;

using namespace __gnu_pbds;

typedef long long ll;

typedef unsigned long long ull;

typedef tree<int, int, less<int>, rb_tree_tag, tree_order_statistics_node_update> Tree;

const double PI = acos(-1.0);  

const double eps = 1e-5;

const double T = 5; //t of fire

const double delta = 0.99; //decreasing speed of t

const int INF = 1000000000;*/

const int maxn=1005;

bool vis[maxn][maxn];

int d[maxn][maxn];

int ans,n,m,k,sx,sy;

int lx[maxn],ly[maxn],G[maxn][maxn];

char bf;

queue<P> q;

int bfs(int sx,int sy,int ex,int ey){

    mem(vis,0);

    q.push(mp(sx,sy));

    vis[sx][sy] = 1;

    d[sx][sy] = 0;

    while(!q.empty()){

	P u = q.front(); q.pop();

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

	    P v=mp(u.fi+dx[dr],u.se+dy[dr]);

	    if(v.fi<1 || v.se<1 || v.fi>n || v.se>m || vis[v.fi][v.se] || G[v.fi][v.se]) continue;

	    d[v.fi][v.se]=d[u.fi][u.se] + 1;

	    vis[v.fi][v.se] = 1;

	    q.push(v);

	}

    }

    return d[ex][ey];

}

int main(){

    cin>>n>>m>>k;

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

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

	    cin>>bf;

	    if(bf=='.') G[i][j] = 0;

	    else if(bf == 'S') sx = i, sy = j, G[i][j] = 0;

	    else if(bf=='X') G[i][j] = 1;

	    else lx[bf-48] = i, ly[bf-48]=j;

	}

    }

    for(int i=1;i<=k;i++){

	ans += bfs(sx,sy,lx[i],ly[i]);

	sx=lx[i], sy=ly[i];

    }

    cout<<ans<<endl;

    return 0;

}

// Faster C++ solution for the same problem (measured speedup: 2.0052696843330824x):
    #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;

     

     

     

    //************************************

    // Method:    bfs

    // FullName:  bfs

    // Access:    public 

    // Returns:   int

    // Qualifier:

    // Parameter: const int & sx 起点x

    // Parameter: const int & sy 起点y

    // Parameter: const int & gx ?点x

    // Parameter: const int & gy ?点y

    //************************************

    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;

    }

    ///////////////////////////End Sub//////////////////////////////////