stablegradients/pie-gem5-pairs huggingface dataset:
// 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: 2.6701313448142185x):
/*Lucky_Glass*/

#include<cstdio>

#include<cstring>

#include<algorithm>

#include<queue>

using namespace std;

const int MAXN=1000;

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

struct QUEUE{

	int x,y,dep;

	QUEUE(){}

	QUEUE(int _x,int _y,int _dep){x=_x;y=_y;dep=_dep;}

};

int r,c,n;

int goal[10][2];

bool vis[MAXN+5][MAXN+5];

char maz[MAXN+5][MAXN+5];

int BFS(int *beg,int cnt){

	memset(vis,false,sizeof vis);

	queue<QUEUE> que;

	que.push(QUEUE(beg[0],beg[1],0));

	while(!que.empty())

	{

		QUEUE pre=que.front();que.pop();

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

		{

			int X=pre.x+dir[i][0],Y=pre.y+dir[i][1],depth=pre.dep+1;

			if(X<1 || X>r || Y<1 || Y>c || maz[X][Y]=='X' || vis[X][Y]) continue;

			vis[X][Y]=true;

			if(X==goal[cnt][0] && Y==goal[cnt][1])

				return depth;

			que.push(QUEUE(X,Y,depth));

		}

	}

	return -1;

}

int main(){

	scanf("%d%d%d",&r,&c,&n);

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

	{

		scanf("%s",maz[i]+1);

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

		{

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

				goal[maz[i][j]-'0'][0]=i,

				goal[maz[i][j]-'0'][1]=j,

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

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

				goal[0][0]=i,

				goal[0][1]=j,

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

		}

	}

	int ans=0;

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

		ans+=BFS(goal[i-1],i);

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

	return 0;

}//
// Slower C++ solution:


#include <iostream>

#include <cstdio>

#include <cstring>

#include <queue>



using namespace std;

const int maxn = 1e3 + 5;

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

int mp[maxn][maxn];

//bool vis[maxn][maxn];

int d[maxn][maxn];

struct point{

    int x,y;

};

int h,w,n;

 int dis = 0;

void bfs(point &p,int cnt)

{

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

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

    queue<point> q;

    q.push(p);

    while (!q.empty()) {

        point t = q.front();q.pop();

        //vis[t.x][t.y] = true;

        if(mp[t.x][t.y] == cnt + 1) {dis += d[t.x][t.y];p.x = t.x,p.y = t.y;return ;}

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

            int tx = t.x + dir[i][0];

            int ty = t.y + dir[i][1];

            if (tx >= 0 && tx < h && ty >= 0 && ty < w && !d[tx][ty] && mp[tx][ty] != -1) {

                q.push({tx,ty});

                d[tx][ty] = 1 + d[t.x][t.y];

            }

        }

    }

}

int main()

{

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

        getchar();

        char ch;

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

       // int sx,sy;

        point s;

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

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

                scanf("%c",&ch);

                if(ch == 'S') {s.x = i,s.y = j;mp[i][j] = 0;}

                else if(ch == '.') {mp[i][j] = 0;}

                else if(isdigit(ch)) {mp[i][j] = ch - '0';}

            }

            getchar();

        }

       

        dis = 0;

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

            bfs(s,i);

        }

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

    }

    return 0;

}

// Faster C++ solution for the same problem (measured speedup: 4.855267777672691x):
/*Lucky_Glass*/

#include<cstdio>

#include<cstring>

#include<algorithm>

#include<queue>

using namespace std;

const int MAXN=1000;

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

struct QUEUE{

	int x,y,dep;

	QUEUE(){}

	QUEUE(int _x,int _y,int _dep){x=_x;y=_y;dep=_dep;}

};

int r,c,n;

int goal[10][2];

bool vis[MAXN+5][MAXN+5];

char maz[MAXN+5][MAXN+5];

int BFS(int *beg,int cnt){

	memset(vis,false,sizeof vis);

	queue<QUEUE> que;

	que.push(QUEUE(beg[0],beg[1],0));

	while(!que.empty())

	{

		QUEUE pre=que.front();que.pop();

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

		{

			int X=pre.x+dir[i][0],Y=pre.y+dir[i][1],depth=pre.dep+1;

			if(X<1 || X>r || Y<1 || Y>c || maz[X][Y]=='X' || vis[X][Y]) continue;

			vis[X][Y]=true;

			if(X==goal[cnt][0] && Y==goal[cnt][1])

				return depth;

			que.push(QUEUE(X,Y,depth));

		}

	}

	return -1;

}

int main(){

	scanf("%d%d%d",&r,&c,&n);

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

	{

		scanf("%s",maz[i]+1);

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

		{

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

				goal[maz[i][j]-'0'][0]=i,

				goal[maz[i][j]-'0'][1]=j,

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

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

				goal[0][0]=i,

				goal[0][1]=j,

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

		}

	}

	int ans=0;

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

		ans+=BFS(goal[i-1],i);

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

	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: 2.6013649403880446x):
/*Lucky_Glass*/

#include<cstdio>

#include<cstring>

#include<algorithm>

#include<queue>

using namespace std;

const int MAXN=1000;

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

struct QUEUE{

	int x,y,dep;

	QUEUE(){}

	QUEUE(int _x,int _y,int _dep){x=_x;y=_y;dep=_dep;}

};

int r,c,n;

int goal[10][2];

bool vis[MAXN+5][MAXN+5];

char maz[MAXN+5][MAXN+5];

int BFS(int *beg,int cnt){

	memset(vis,false,sizeof vis);

	queue<QUEUE> que;

	que.push(QUEUE(beg[0],beg[1],0));

	while(!que.empty())

	{

		QUEUE pre=que.front();que.pop();

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

		{

			int X=pre.x+dir[i][0],Y=pre.y+dir[i][1],depth=pre.dep+1;

			if(X<1 || X>r || Y<1 || Y>c || maz[X][Y]=='X' || vis[X][Y]) continue;

			vis[X][Y]=true;

			if(X==goal[cnt][0] && Y==goal[cnt][1])

				return depth;

			que.push(QUEUE(X,Y,depth));

		}

	}

	return -1;

}

int main(){

	scanf("%d%d%d",&r,&c,&n);

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

	{

		scanf("%s",maz[i]+1);

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

		{

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

				goal[maz[i][j]-'0'][0]=i,

				goal[maz[i][j]-'0'][1]=j,

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

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

				goal[0][0]=i,

				goal[0][1]=j,

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

		}

	}

	int ans=0;

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

		ans+=BFS(goal[i-1],i);

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

	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: 3.240381984076703x):
/*Lucky_Glass*/

#include<cstdio>

#include<cstring>

#include<algorithm>

#include<queue>

using namespace std;

const int MAXN=1000;

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

struct QUEUE{

	int x,y,dep;

	QUEUE(){}

	QUEUE(int _x,int _y,int _dep){x=_x;y=_y;dep=_dep;}

};

int r,c,n;

int goal[10][2];

bool vis[MAXN+5][MAXN+5];

char maz[MAXN+5][MAXN+5];

int BFS(int *beg,int cnt){

	memset(vis,false,sizeof vis);

	queue<QUEUE> que;

	que.push(QUEUE(beg[0],beg[1],0));

	while(!que.empty())

	{

		QUEUE pre=que.front();que.pop();

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

		{

			int X=pre.x+dir[i][0],Y=pre.y+dir[i][1],depth=pre.dep+1;

			if(X<1 || X>r || Y<1 || Y>c || maz[X][Y]=='X' || vis[X][Y]) continue;

			vis[X][Y]=true;

			if(X==goal[cnt][0] && Y==goal[cnt][1])

				return depth;

			que.push(QUEUE(X,Y,depth));

		}

	}

	return -1;

}

int main(){

	scanf("%d%d%d",&r,&c,&n);

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

	{

		scanf("%s",maz[i]+1);

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

		{

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

				goal[maz[i][j]-'0'][0]=i,

				goal[maz[i][j]-'0'][1]=j,

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

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

				goal[0][0]=i,

				goal[0][1]=j,

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

		}

	}

	int ans=0;

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

		ans+=BFS(goal[i-1],i);

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

	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: 2.6906228690350797x):
/*Lucky_Glass*/

#include<cstdio>

#include<cstring>

#include<algorithm>

#include<queue>

using namespace std;

const int MAXN=1000;

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

struct QUEUE{

	int x,y,dep;

	QUEUE(){}

	QUEUE(int _x,int _y,int _dep){x=_x;y=_y;dep=_dep;}

};

int r,c,n;

int goal[10][2];

bool vis[MAXN+5][MAXN+5];

char maz[MAXN+5][MAXN+5];

int BFS(int *beg,int cnt){

	memset(vis,false,sizeof vis);

	queue<QUEUE> que;

	que.push(QUEUE(beg[0],beg[1],0));

	while(!que.empty())

	{

		QUEUE pre=que.front();que.pop();

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

		{

			int X=pre.x+dir[i][0],Y=pre.y+dir[i][1],depth=pre.dep+1;

			if(X<1 || X>r || Y<1 || Y>c || maz[X][Y]=='X' || vis[X][Y]) continue;

			vis[X][Y]=true;

			if(X==goal[cnt][0] && Y==goal[cnt][1])

				return depth;

			que.push(QUEUE(X,Y,depth));

		}

	}

	return -1;

}

int main(){

	scanf("%d%d%d",&r,&c,&n);

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

	{

		scanf("%s",maz[i]+1);

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

		{

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

				goal[maz[i][j]-'0'][0]=i,

				goal[maz[i][j]-'0'][1]=j,

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

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

				goal[0][0]=i,

				goal[0][1]=j,

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

		}

	}

	int ans=0;

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

		ans+=BFS(goal[i-1],i);

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

	return 0;

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

#include <string.h>  

#include <queue>  

#define MAX_W 1000  

#define MAX_H 1000  

#define MAX_N 9  

  

using namespace std;  

  

char m[MAX_H][MAX_W + 1];  

int w, h, n;  

int gx[MAX_N + 1], gy[MAX_N + 1];  

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

  

int bfs(int sx, int sy, int gx, int gy)  

{  

    int d[MAX_H][MAX_W];  

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

    queue<int> que;  

    que.push(sx * w + sy);  

    d[sx][sy] = 0;  

          

    while(que.size())  

    {  

        int x = que.front()/w, y = que.front()%w;  

        que.pop();  

        if(x == gx && y == gy)  

            break;  

  

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

            int nx = x + dx[di], ny = y + dy[di];  

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

                d[nx][ny] = d[x][y] + 1;  

                que.push(nx * w + ny);  

            }  

        }  

    }  

    return d[gx][gy];  

}  

  

void solve()  

{  

    int ans = 0;  

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

        ans += bfs(gx[i], gy[i], gx[i + 1], gy[i + 1]);  

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

}  

  

int main()  

{  

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

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

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

          

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

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

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

                gx[m[i][j] - '0'] = i, gy[m[i][j] - '0'] = j;  

            else if('S' == m[i][j])  

                gx[0] = i, gy[0] = j;  

        }  

  

    if(w || h)  

        solve();  

    return 0;  

}  


// Faster C++ solution for the same problem (measured speedup: 2.6095737573518965x):
/*Lucky_Glass*/

#include<cstdio>

#include<cstring>

#include<algorithm>

#include<queue>

using namespace std;

const int MAXN=1000;

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

struct QUEUE{

	int x,y,dep;

	QUEUE(){}

	QUEUE(int _x,int _y,int _dep){x=_x;y=_y;dep=_dep;}

};

int r,c,n;

int goal[10][2];

bool vis[MAXN+5][MAXN+5];

char maz[MAXN+5][MAXN+5];

int BFS(int *beg,int cnt){

	memset(vis,false,sizeof vis);

	queue<QUEUE> que;

	que.push(QUEUE(beg[0],beg[1],0));

	while(!que.empty())

	{

		QUEUE pre=que.front();que.pop();

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

		{

			int X=pre.x+dir[i][0],Y=pre.y+dir[i][1],depth=pre.dep+1;

			if(X<1 || X>r || Y<1 || Y>c || maz[X][Y]=='X' || vis[X][Y]) continue;

			vis[X][Y]=true;

			if(X==goal[cnt][0] && Y==goal[cnt][1])

				return depth;

			que.push(QUEUE(X,Y,depth));

		}

	}

	return -1;

}

int main(){

	scanf("%d%d%d",&r,&c,&n);

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

	{

		scanf("%s",maz[i]+1);

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

		{

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

				goal[maz[i][j]-'0'][0]=i,

				goal[maz[i][j]-'0'][1]=j,

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

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

				goal[0][0]=i,

				goal[0][1]=j,

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

		}

	}

	int ans=0;

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

		ans+=BFS(goal[i-1],i);

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

	return 0;

}//
// Slower C++ solution:
//AOJ-0558

#include <cstdio>

#include <cstring>

#include <queue>

#include <algorithm>

using namespace std;



char map[1010][1010];

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

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

int vis[1010][1010];

int m, n, N;

struct node{

	int x, y, t;

};



node bfs(int x, int y, char k)

{

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

	queue<node> q;

	node s, e, p;

	s = {x, y, 0};

	q.push(s); vis[x][y] = 1;

	while(!q.empty())

	{

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

		if(map[p.x][p.y] == k) return p;

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

		{

			int nx = p.x + dx[i];

			int ny = p.y + dy[i];

			if(nx >= n || nx < 0 || ny >= m || ny < 0 || map[nx][ny] == 'X') continue;

			if(!vis[nx][ny]){

				vis[nx][ny] = 1;

				e = {nx, ny, p.t + 1};

				q.push(e);

			}	

		}

	}

}





int main()

{

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

	{

		node s;

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

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

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

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

			   	 s.x = i; s.y = j; s.t = 0;

			   }

			}	

		}

		int ans = 0;

		char c = '0';

		for(char i = 1; i <= N; i++)

		{

			node t = bfs(s.x, s.y, c + i);

			ans += t.t;

			s.x = t.x; s.y = t.y;

		}

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

	}

	return 0; 

}


// Faster C++ solution for the same problem (measured speedup: 2.6230436509427455x):
/*Lucky_Glass*/

#include<cstdio>

#include<cstring>

#include<algorithm>

#include<queue>

using namespace std;

const int MAXN=1000;

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

struct QUEUE{

	int x,y,dep;

	QUEUE(){}

	QUEUE(int _x,int _y,int _dep){x=_x;y=_y;dep=_dep;}

};

int r,c,n;

int goal[10][2];

bool vis[MAXN+5][MAXN+5];

char maz[MAXN+5][MAXN+5];

int BFS(int *beg,int cnt){

	memset(vis,false,sizeof vis);

	queue<QUEUE> que;

	que.push(QUEUE(beg[0],beg[1],0));

	while(!que.empty())

	{

		QUEUE pre=que.front();que.pop();

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

		{

			int X=pre.x+dir[i][0],Y=pre.y+dir[i][1],depth=pre.dep+1;

			if(X<1 || X>r || Y<1 || Y>c || maz[X][Y]=='X' || vis[X][Y]) continue;

			vis[X][Y]=true;

			if(X==goal[cnt][0] && Y==goal[cnt][1])

				return depth;

			que.push(QUEUE(X,Y,depth));

		}

	}

	return -1;

}

int main(){

	scanf("%d%d%d",&r,&c,&n);

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

	{

		scanf("%s",maz[i]+1);

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

		{

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

				goal[maz[i][j]-'0'][0]=i,

				goal[maz[i][j]-'0'][1]=j,

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

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

				goal[0][0]=i,

				goal[0][1]=j,

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

		}

	}

	int ans=0;

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

		ans+=BFS(goal[i-1],i);

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

	return 0;

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

#include<algorithm>

#include<math.h>

#include<string.h>

#include<queue>

#include<stdio.h>

using namespace std;



typedef struct{

	int x,y;

}p;

char a[1005][1005];

p acm[11];

p s,g;

int b[1005][1005];

int H,W,N;

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

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

int bfs(p s,p g)

{

	queue<p> q;

	p n;

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

	q.push(s);

	b[s.x][s.y]=0;

	while(!q.empty())

	{

		p h;

		h=q.front();

		q.pop();

		if(h.x==g.x&&h.y==g.y)

		{

			break;

		}

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

		{

			n.x=h.x+dx[i];

			n.y=h.y+dy[i];

			if(n.x>=0&&n.x<H&&n.y>=0&&n.y<W&&a[n.x][n.y]!='X'&&b[n.x][n.y]==-1)

			{

				q.push(n);

				b[n.x][n.y]=b[h.x][h.y]+1;

			}

		}

	}

	return b[g.x][g.y];

}

int main()

{

	int i,j,t,step=0;

	scanf("%d%d%d",&H,&W,&N);

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

	{

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

	}

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

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

	{

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

		{

			t=a[i][j]-'0';

		    acm[t].x=i;

		    acm[t].y=j;

		}

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

		{

			s.x=i;

			s.y=j;

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

		}

	}

		for(i=1;i<=N;i++)

		{

			g.x=acm[i].x;

			g.y=acm[i].y;

			step+=bfs(s,g);

			s.x=g.x;

			s.y=g.y;

			a[g.x][g.y]='.';

		}

	

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

	return 0;

 } 


// Faster C++ solution for the same problem (measured speedup: 2.6395714094667135x):
/*Lucky_Glass*/

#include<cstdio>

#include<cstring>

#include<algorithm>

#include<queue>

using namespace std;

const int MAXN=1000;

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

struct QUEUE{

	int x,y,dep;

	QUEUE(){}

	QUEUE(int _x,int _y,int _dep){x=_x;y=_y;dep=_dep;}

};

int r,c,n;

int goal[10][2];

bool vis[MAXN+5][MAXN+5];

char maz[MAXN+5][MAXN+5];

int BFS(int *beg,int cnt){

	memset(vis,false,sizeof vis);

	queue<QUEUE> que;

	que.push(QUEUE(beg[0],beg[1],0));

	while(!que.empty())

	{

		QUEUE pre=que.front();que.pop();

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

		{

			int X=pre.x+dir[i][0],Y=pre.y+dir[i][1],depth=pre.dep+1;

			if(X<1 || X>r || Y<1 || Y>c || maz[X][Y]=='X' || vis[X][Y]) continue;

			vis[X][Y]=true;

			if(X==goal[cnt][0] && Y==goal[cnt][1])

				return depth;

			que.push(QUEUE(X,Y,depth));

		}

	}

	return -1;

}

int main(){

	scanf("%d%d%d",&r,&c,&n);

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

	{

		scanf("%s",maz[i]+1);

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

		{

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

				goal[maz[i][j]-'0'][0]=i,

				goal[maz[i][j]-'0'][1]=j,

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

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

				goal[0][0]=i,

				goal[0][1]=j,

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

		}

	}

	int ans=0;

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

		ans+=BFS(goal[i-1],i);

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

	return 0;

}//