stablegradients/pie-gem5-pairs huggingface dataset:
// Slower C++ solution:
#include<stdio.h>

#include<string.h>

#include<queue>

#include<algorithm>

#define MAX_N 1002

using namespace std;

struct node{int x,y;};

char _map[MAX_N][MAX_N];

int dp[MAX_N][MAX_N];

int ox[]={0,0,1,-1};

int oy[]={1,-1,0,0};

int S_x,S_y;

int h,w,cnt;



node make_node(int x,int y){

	node t;

	t.x=x,t.y=y;

	return t;

}

void solve(){

	queue<node> que;

	int step=0;

	

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

		que.push(make_node(S_x,S_y));

		_map[S_x][S_y]='.';

		memset(dp,-1,sizeof(dp));

		dp[S_x][S_y]=step;

		while(!que.empty()){

			node t=que.front();que.pop();

			if(_map[t.x][t.y]=='0'+i){

				step=dp[t.x][t.y];

				S_x=t.x,S_y=t.y;

				break;

			}

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

				int x=t.x+ox[j];

				int y=t.y+oy[j];

				if(0<=x&&x<h&&0<=y&&y<w&&_map[x][y]!='X'&&dp[x][y]==-1){

					que.push(make_node(x,y));

					dp[x][y]=dp[t.x][t.y]+1;

				}

			}

		}

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

	}

	

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

}



int main()

{

	while(scanf("%d%d%d",&h,&w,&cnt)!=EOF){

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

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

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

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

				if(_map[j][k]=='S')

					S_x=j,S_y=k,j=h,k=w;

		solve();

	}

	return 0;

}

// Faster C++ solution for the same problem (measured speedup: 4.969256756850126x):
#include <iostream>

#include <queue>

#include <string>

#include <cstdio>

#include <cstring>

#include <utility>

using namespace std;

typedef pair<int, int> P;

char maze[1005][1005];

int d[1005][1005];

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

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

int h, w, n, hard; 

int si, sj;

const int INF = 0x3f3f3f3f;

int tot = 0; 



void BFS(int ai, int aj){

    queue<P> que;

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

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

            d[i][j] = INF;

        }

    }

    que.push(P(ai, aj));

    d[ai][aj] = 0;

    while(!que.empty()){

        P a = que.front();

        que.pop();

        int x = a.first, y = a.second;

        if(maze[x][y]-'0' == hard){

            tot += d[x][y];

            si = x; sj = y;

            break;

        }

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

            int dx = x+dirx[i], dy = y+diry[i];

            if(dx>=0 && dx<h && dy>=0 && dy<w && maze[dx][dy]!='X' && d[dx][dy]==INF){

                d[dx][dy] = d[x][y]+1;

                que.push(P(dx, dy));

            }

        }

    }

}



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

    while(scanf("%d%d%d", &h, &w, &n) != EOF){

        getchar();

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

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

                scanf("%c", &maze[i][j]);

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

                    si = i; sj = j;

                }

            }

            getchar();

        }

        tot = 0;

        hard = 1;

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

            BFS(si, sj);

            hard++;

        }

        cout << tot << endl;

    }

    return 0;

}
// Slower C++ solution:
#include<cstdio>

#include<cstring>

#include<queue>

using namespace std;

int map[1005][1005];

struct node{

	int x,y,time;

}s[10],p;

queue<node> q;

int vis[1005][1005];

int n,m,T;

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

int bfs(int a,int b){

	while(!q.empty())

		q.pop();

	q.push(s[a]);

	memset(vis,0,sizeof vis);

	vis[s[a].x][s[a].y]=1;

	while(!q.empty()){

		p=q.front();

		q.pop();

		int x=p.x,y=p.y,time=p.time;

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

			int x1=x+dir[i][0],y1=y+dir[i][1];

			if(x1<=0||y1<=0||x1>n||y1>m||vis[x1][y1]||map[x1][y1])

				continue ;

			if(x1==s[b].x&&y1==s[b].y)

				return time+1;

			vis[x1][y1]=1;

			p.x=x1,p.y=y1,p.time=time+1;

			q.push(p);

		}

	}

}

int main()

{

	scanf("%d%d%d",&n,&m,&T);

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

		scanf("\n");

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

			char c;

			c=getchar();

			if(c=='S')

				s[0].x=i,s[0].y=j;

			if(c=='X')

				map[i][j]=1;

			if(c>='1'&&c<='9')

				s[c-'0'].x=i,s[c-'0'].y=j;

		}

	}

	int time=0;

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

		time+=bfs(i,i+1);

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

}

// Faster C++ solution for the same problem (measured speedup: 4.998588836552948x):
#include <iostream>

#include <queue>

#include <string>

#include <cstdio>

#include <cstring>

#include <utility>

using namespace std;

typedef pair<int, int> P;

char maze[1005][1005];

int d[1005][1005];

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

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

int h, w, n, hard; 

int si, sj;

const int INF = 0x3f3f3f3f;

int tot = 0; 



void BFS(int ai, int aj){

    queue<P> que;

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

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

            d[i][j] = INF;

        }

    }

    que.push(P(ai, aj));

    d[ai][aj] = 0;

    while(!que.empty()){

        P a = que.front();

        que.pop();

        int x = a.first, y = a.second;

        if(maze[x][y]-'0' == hard){

            tot += d[x][y];

            si = x; sj = y;

            break;

        }

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

            int dx = x+dirx[i], dy = y+diry[i];

            if(dx>=0 && dx<h && dy>=0 && dy<w && maze[dx][dy]!='X' && d[dx][dy]==INF){

                d[dx][dy] = d[x][y]+1;

                que.push(P(dx, dy));

            }

        }

    }

}



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

    while(scanf("%d%d%d", &h, &w, &n) != EOF){

        getchar();

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

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

                scanf("%c", &maze[i][j]);

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

                    si = i; sj = j;

                }

            }

            getchar();

        }

        tot = 0;

        hard = 1;

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

            BFS(si, sj);

            hard++;

        }

        cout << tot << endl;

    }

    return 0;

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

#include <cstdio>

#include <cstring>

#include <cmath>

#include <vector>

#include <queue>

#include <set>

#include <map>

#include <stack>

#include <utility>

#include <algorithm>

#include <sstream>

using namespace std;

typedef pair<int,int> P;



const int max_n=1000+10;

int h,w,n,sx,sy,dx[]={0,0,-1,1},dy[]={-1,1,0,0},d[max_n][max_n],ans;

char c[max_n][max_n];



void bfs(int x,int y,int m){

	if(m>n)return;

	queue<P> q;

	memset(d,0,sizeof(d));

	q.push(P(x,y));

	d[x][y]=1;

	while(!q.empty()){

		P p=q.front();

		q.pop();

		if(c[p.first][p.second]=='0'+m){

		    ans+=d[p.first][p.second]-1;

		    bfs(p.first,p.second,m+1);

		    return;

		}

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

			int tx=p.first+dx[i],ty=p.second+dy[i];

			if(tx>=0 && tx<h && ty>=0 && ty<w && c[tx][ty]!='X' && !d[tx][ty]){

				q.push(P(tx,ty));

				d[tx][ty]=d[p.first][p.second]+1;

			}

		}

	}

}



int main(){

    scanf("%d%d%d",&h,&w,&n);

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

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

            cin>>c[i][j];

            if(c[i][j]=='S'){sx=i;sy=j;}

        }

    bfs(sx,sy,1);

    cout<<ans<<endl;

    return 0;

}

// Faster C++ solution for the same problem (measured speedup: 5.128950380165664x):
#include <iostream>

#include <queue>

#include <string>

#include <cstdio>

#include <cstring>

#include <utility>

using namespace std;

typedef pair<int, int> P;

char maze[1005][1005];

int d[1005][1005];

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

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

int h, w, n, hard; 

int si, sj;

const int INF = 0x3f3f3f3f;

int tot = 0; 



void BFS(int ai, int aj){

    queue<P> que;

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

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

            d[i][j] = INF;

        }

    }

    que.push(P(ai, aj));

    d[ai][aj] = 0;

    while(!que.empty()){

        P a = que.front();

        que.pop();

        int x = a.first, y = a.second;

        if(maze[x][y]-'0' == hard){

            tot += d[x][y];

            si = x; sj = y;

            break;

        }

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

            int dx = x+dirx[i], dy = y+diry[i];

            if(dx>=0 && dx<h && dy>=0 && dy<w && maze[dx][dy]!='X' && d[dx][dy]==INF){

                d[dx][dy] = d[x][y]+1;

                que.push(P(dx, dy));

            }

        }

    }

}



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

    while(scanf("%d%d%d", &h, &w, &n) != EOF){

        getchar();

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

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

                scanf("%c", &maze[i][j]);

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

                    si = i; sj = j;

                }

            }

            getchar();

        }

        tot = 0;

        hard = 1;

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

            BFS(si, sj);

            hard++;

        }

        cout << tot << endl;

    }

    return 0;

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

#include <cstdio>

#include <cstring>

#include <cmath>

#include <vector>

#include <queue>

#include <set>

#include <map>

#include <stack>

#include <utility>

#include <algorithm>

#include <sstream>

using namespace std;



struct Node{

	int x,y;

};



const int max_n=1000+10;

int h,w,n,dx[]={0,0,-1,1},dy[]={-1,1,0,0},d[max_n][max_n],ans;

Node sN;

char c[max_n][max_n];



void bfs(int m){

	if(m>n)return;

	queue<Node> q;

	memset(d,0,sizeof(d));

	q.push(sN);

	d[sN.x][sN.y]=1;

	while(!q.empty()){

		Node p=q.front();

		q.pop();

		

		if(c[p.x][p.y]=='0'+m){

		    ans+=d[p.x][p.y]-1;

		    sN.x=p.x;

		    sN.y=p.y;

		    return;

		}

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

			Node nN;

			nN.x=p.x+dx[i];

			nN.y=p.y+dy[i];

			if(nN.x>=0 && nN.x<h 

            && nN.y>=0 && nN.y<w 

		   	&& c[nN.x][nN.y]!='X' && !d[nN.x][nN.y]){

				q.push(nN);

				d[nN.x][nN.y]=d[p.x][p.y]+1;

			}

		}

	}

}



int main(){

    scanf("%d%d%d",&h,&w,&n);

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

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

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

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

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

            	sN.x=i;

            	sN.y=j;

            	break;

            }

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

    cout<<ans<<endl;

    return 0;

}

// Faster C++ solution for the same problem (measured speedup: 5.1263262131210565x):
#include <iostream>

#include <queue>

#include <string>

#include <cstdio>

#include <cstring>

#include <utility>

using namespace std;

typedef pair<int, int> P;

char maze[1005][1005];

int d[1005][1005];

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

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

int h, w, n, hard; 

int si, sj;

const int INF = 0x3f3f3f3f;

int tot = 0; 



void BFS(int ai, int aj){

    queue<P> que;

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

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

            d[i][j] = INF;

        }

    }

    que.push(P(ai, aj));

    d[ai][aj] = 0;

    while(!que.empty()){

        P a = que.front();

        que.pop();

        int x = a.first, y = a.second;

        if(maze[x][y]-'0' == hard){

            tot += d[x][y];

            si = x; sj = y;

            break;

        }

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

            int dx = x+dirx[i], dy = y+diry[i];

            if(dx>=0 && dx<h && dy>=0 && dy<w && maze[dx][dy]!='X' && d[dx][dy]==INF){

                d[dx][dy] = d[x][y]+1;

                que.push(P(dx, dy));

            }

        }

    }

}



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

    while(scanf("%d%d%d", &h, &w, &n) != EOF){

        getchar();

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

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

                scanf("%c", &maze[i][j]);

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

                    si = i; sj = j;

                }

            }

            getchar();

        }

        tot = 0;

        hard = 1;

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

            BFS(si, sj);

            hard++;

        }

        cout << tot << endl;

    }

    return 0;

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

#include <queue>

#include <string>

using namespace std;

int main()

{

	int h,w,n;

	int sx,sy;

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

	string map[1001];

	int d[1001][1001];

	int x = 0;

	cin >> h >> w >> n;

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

	{

		cin >> map[i];

	}

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

	{

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

		{

			d[j][i] = -1;

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

			{

				sx = j;

				sy = i;

			}

		}

	}

	d[sx][sy] = 0;

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

	{

		queue< pair<int, int> > que;

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

		{

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

			{

				d[k][j] = -1;

			}

		}

		d[sx][sy] = 0;

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

		while(que.size())

		{

			pair<int, int> p = que.front();

			que.pop();

			if((map[p.second][p.first] - '0') == (i+1))

			{

				sx = p.first;

				sy = p.second;

				x+=d[sx][sy];

				break;

			}

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

			{

				int nx = p.first + dx[j];

				int ny = p.second + dy[j];

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

				{

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

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

				}

			}

		}

	}

	cout << x << endl;

}

// Faster C++ solution for the same problem (measured speedup: 1.1338974670638708x):
#include <iostream>

#include <queue>

#include <string>

#include <cstdio>

#include <cstring>

#include <utility>

using namespace std;

typedef pair<int, int> P;

char maze[1005][1005];

int d[1005][1005];

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

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

int h, w, n, hard; 

int si, sj;

const int INF = 0x3f3f3f3f;

int tot = 0; 



void BFS(int ai, int aj){

    queue<P> que;

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

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

            d[i][j] = INF;

        }

    }

    que.push(P(ai, aj));

    d[ai][aj] = 0;

    while(!que.empty()){

        P a = que.front();

        que.pop();

        int x = a.first, y = a.second;

        if(maze[x][y]-'0' == hard){

            tot += d[x][y];

            si = x; sj = y;

            break;

        }

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

            int dx = x+dirx[i], dy = y+diry[i];

            if(dx>=0 && dx<h && dy>=0 && dy<w && maze[dx][dy]!='X' && d[dx][dy]==INF){

                d[dx][dy] = d[x][y]+1;

                que.push(P(dx, dy));

            }

        }

    }

}



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

    while(scanf("%d%d%d", &h, &w, &n) != EOF){

        getchar();

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

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

                scanf("%c", &maze[i][j]);

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

                    si = i; sj = j;

                }

            }

            getchar();

        }

        tot = 0;

        hard = 1;

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

            BFS(si, sj);

            hard++;

        }

        cout << tot << endl;

    }

    return 0;

}
// Slower C++ solution:
#include<stdio.h>

#include<string.h>

#include<queue>

using namespace std;

const int maxn=1005;

int n,m,k,dx[]={-1,1,0,0},dy[]={0,0,1,-1},vis[maxn][maxn];

char map[maxn][maxn];

struct maze

{

    maze(int xx=0,int yy=0,int tt=0):x(xx),y(yy),time(tt){}

	int x,y,time;

};

maze bfs(int bx,int by,char ch)

{

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

	queue<maze> q;

	maze st(bx,by,0);

	q.push(st);vis[bx][by]=1;

	while(!q.empty())

	{

		st=q.front();q.pop();

		if(map[st.x][st.y]==ch)

		{

			return st;

		}

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

		{

			int tx=st.x+dx[i],ty=st.y+dy[i];

			if(tx<0||tx>=n||ty<0||ty>=m||map[tx][ty]=='X')//越界和?

			{

				continue;

			}

			maze tp(tx,ty,st.time+1);

			if(!vis[tx][ty])

			{

				vis[tx][ty]=1;

				q.push(tp);

			}

		}

	}

}

int main()

{



	while(~scanf("%d%d%d",&n,&m,&k))

	{

		int bx,by;

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

		{

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

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

			{

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

				{

					bx=i;by=j;

					

					map[i][j]='0'; //第0个起点

				}

			}

		}

		int ans=0;

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

		{

			maze tp=bfs(bx,by,map[bx][by]+1);

			ans+=tp.time;

			bx=tp.x;by=tp.y;

		}

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

	}

	return 0;

}

// Faster C++ solution for the same problem (measured speedup: 4.99430311160711x):
#include <iostream>

#include <queue>

#include <string>

#include <cstdio>

#include <cstring>

#include <utility>

using namespace std;

typedef pair<int, int> P;

char maze[1005][1005];

int d[1005][1005];

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

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

int h, w, n, hard; 

int si, sj;

const int INF = 0x3f3f3f3f;

int tot = 0; 



void BFS(int ai, int aj){

    queue<P> que;

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

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

            d[i][j] = INF;

        }

    }

    que.push(P(ai, aj));

    d[ai][aj] = 0;

    while(!que.empty()){

        P a = que.front();

        que.pop();

        int x = a.first, y = a.second;

        if(maze[x][y]-'0' == hard){

            tot += d[x][y];

            si = x; sj = y;

            break;

        }

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

            int dx = x+dirx[i], dy = y+diry[i];

            if(dx>=0 && dx<h && dy>=0 && dy<w && maze[dx][dy]!='X' && d[dx][dy]==INF){

                d[dx][dy] = d[x][y]+1;

                que.push(P(dx, dy));

            }

        }

    }

}



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

    while(scanf("%d%d%d", &h, &w, &n) != EOF){

        getchar();

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

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

                scanf("%c", &maze[i][j]);

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

                    si = i; sj = j;

                }

            }

            getchar();

        }

        tot = 0;

        hard = 1;

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

            BFS(si, sj);

            hard++;

        }

        cout << tot << endl;

    }

    return 0;

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

#include<cstring>

#include<queue>

using namespace std;

int n,m,k,step[1010][1010],temp[1010][3];

char a[1010][1010];

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

bool vis[1010][1010];

typedef pair<int,int> P;

int bfs(int x,int y,int ex,int ey)

{

	memset(vis,true,sizeof(vis));

	memset(step,0,sizeof(step));

	queue <P> que;

	que.push(P(x,y));

	vis[x][y]=false;

	while(!que.empty())

	{

		P tmp=que.front();que.pop();

		int b=que.size();

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

		  return step[tmp.first][tmp.second];

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

		{

			int tx=tmp.first+dx[i];

			int ty=tmp.second+dy[i];

			if(tx>0&&tx<=n&&ty>0&&ty<=m&&vis[tx][ty]==true&&a[tx][ty]!='X')

			{

				que.push(P(tx,ty));

				vis[tx][ty]=false;

				step[tx][ty]=step[tmp.first][tmp.second]+1;

			}

		}

	}

}

int main()

{

	ios::sync_with_stdio(false);

	cin>>n>>m>>k;

		int sx,sy;

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

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

		  {

			cin>>a[i][j];

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

			  sx=i,sy=j;

			//temp[0][0]=i;

			//temp[0][1]=j;

		  }

		a[sx][sy]='0';

		int tot=0,ans=0;

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

		{

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

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

			    if(a[j][l]=='0'+i)

			    {

			    	tot=bfs(sx,sy,j,l);

			    	sx=j,sy=l;

				}

			ans+=tot;

		}

		cout<<ans<<endl;

	return 0;

}

// Faster C++ solution for the same problem (measured speedup: 6.221137824459116x):
#include <iostream>

#include <queue>

#include <string>

#include <cstdio>

#include <cstring>

#include <utility>

using namespace std;

typedef pair<int, int> P;

char maze[1005][1005];

int d[1005][1005];

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

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

int h, w, n, hard; 

int si, sj;

const int INF = 0x3f3f3f3f;

int tot = 0; 



void BFS(int ai, int aj){

    queue<P> que;

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

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

            d[i][j] = INF;

        }

    }

    que.push(P(ai, aj));

    d[ai][aj] = 0;

    while(!que.empty()){

        P a = que.front();

        que.pop();

        int x = a.first, y = a.second;

        if(maze[x][y]-'0' == hard){

            tot += d[x][y];

            si = x; sj = y;

            break;

        }

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

            int dx = x+dirx[i], dy = y+diry[i];

            if(dx>=0 && dx<h && dy>=0 && dy<w && maze[dx][dy]!='X' && d[dx][dy]==INF){

                d[dx][dy] = d[x][y]+1;

                que.push(P(dx, dy));

            }

        }

    }

}



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

    while(scanf("%d%d%d", &h, &w, &n) != EOF){

        getchar();

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

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

                scanf("%c", &maze[i][j]);

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

                    si = i; sj = j;

                }

            }

            getchar();

        }

        tot = 0;

        hard = 1;

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

            BFS(si, sj);

            hard++;

        }

        cout << tot << endl;

    }

    return 0;

}
// Slower C++ solution:
#include "cstring"

#include "cstdio"

#include "queue"

using namespace std;



const int maxn=1024;



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

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



char mp[maxn][maxn];

int vis[maxn][maxn];

int wx[10],wy[10];

int n,m,k;



int bfs(int x1,int y1,int x2,int y2)

{

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

	queue<int>qx;qx.push(x1);

	queue<int>qy;qy.push(y1);

	queue<int>qt;qt.push(0);

	vis[x1][y1]=1;

	

	while(!qx.empty())

	{

		int nx=qx.front();qx.pop();

		int ny=qy.front();qy.pop();

		int nt=qt.front();qt.pop();

		

		if(nx==x2 && ny==y2)return nt;

		

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

		{

			int px=nx+dx[i];

			int py=ny+dy[i];

			if(px>=0 && py>=0 && px<n && py<m && mp[px][py]!='X' && !vis[px][py])

			{

				qx.push(px);

				qy.push(py);

				qt.push(nt+1);

				vis[px][py]=1;

			}

		}

		

	}

	

	return 0;

}



int main()

{

	//freopen("f.txt","r",stdin);

	scanf("%d%d%d",&n,&m,&k);

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

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

		{

			mp[i][j]=getchar();

			while(mp[i][j]=='\n' || mp[i][j]=='\0')mp[i][j]=getchar();

			if(mp[i][j]>='1' && mp[i][j]<='9')

			{

				wx[mp[i][j]-'0']=i;

				wy[mp[i][j]-'0']=j;

			}

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

			{

				wx[0]=i;

				wy[0]=j;

			}	

		}

	int ans=0;

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

		ans+=bfs(wx[i],wy[i],wx[i+1],wy[i+1]);

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

	return 0;

} 


// Faster C++ solution for the same problem (measured speedup: 5.1656674380253x):
#include <iostream>

#include <queue>

#include <string>

#include <cstdio>

#include <cstring>

#include <utility>

using namespace std;

typedef pair<int, int> P;

char maze[1005][1005];

int d[1005][1005];

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

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

int h, w, n, hard; 

int si, sj;

const int INF = 0x3f3f3f3f;

int tot = 0; 



void BFS(int ai, int aj){

    queue<P> que;

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

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

            d[i][j] = INF;

        }

    }

    que.push(P(ai, aj));

    d[ai][aj] = 0;

    while(!que.empty()){

        P a = que.front();

        que.pop();

        int x = a.first, y = a.second;

        if(maze[x][y]-'0' == hard){

            tot += d[x][y];

            si = x; sj = y;

            break;

        }

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

            int dx = x+dirx[i], dy = y+diry[i];

            if(dx>=0 && dx<h && dy>=0 && dy<w && maze[dx][dy]!='X' && d[dx][dy]==INF){

                d[dx][dy] = d[x][y]+1;

                que.push(P(dx, dy));

            }

        }

    }

}



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

    while(scanf("%d%d%d", &h, &w, &n) != EOF){

        getchar();

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

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

                scanf("%c", &maze[i][j]);

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

                    si = i; sj = j;

                }

            }

            getchar();

        }

        tot = 0;

        hard = 1;

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

            BFS(si, sj);

            hard++;

        }

        cout << tot << endl;

    }

    return 0;

}