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: 2.5792214517484844x):
#include<queue>

#include<cstdio>

#include<cstring>

#include<algorithm>

using namespace std;



#define MAXN 1000

struct node{

    int x,y,st;

    bool operator == (node B) const{

        return x==B.x&&y==B.y;

    }

};

int H,W,N;

char Map[MAXN+5][MAXN+5];

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



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

int bfs(node S,node T){

    queue<node> Q;

    Q.push(S);

    memset(vis,0,sizeof vis);

    while(!Q.empty()){

        node u=Q.front();Q.pop();

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

            node tmp=u;

            tmp.x+=dir[i][0],tmp.y+=dir[i][1],tmp.st++;

            if(tmp.x<1||tmp.x>H||tmp.y<1||tmp.y>W||vis[tmp.x][tmp.y]||Map[tmp.x][tmp.y]=='X')

                continue;

            if(tmp==T)

                return tmp.st;

            vis[tmp.x][tmp.y]=1;

            Q.push(tmp);

        }

    }

    return -1;

}



pair<int,int> pos[10];



int main(){

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

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

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

    node S;

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

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

            if(Map[i][j]!='.'&&Map[i][j]!='X'&&Map[i][j]!='S')

                pos[Map[i][j]-'1'+1]=make_pair(i,j);

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

                S.x=i,S.y=j,S.st=0;

        }

    int Ans=0;

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

        Ans+=bfs(S,(node){pos[i].first,pos[i].second,0});

        S=(node){pos[i].first,pos[i].second,0};

    }

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

}
// Slower C++ solution:


#include<iostream>

#include<cstdio>

#include<deque>

#include<algorithm>



using namespace std;



const int maxn=1e3+5;

const int inf=0x3f3f3f3f;



typedef pair<int,int>P;



int h,w,n,sx,sy,book[maxn][maxn],goal[10],Next[4][2]={{0,1},{1,0},{0,-1},{-1,0}};

char s[maxn][maxn];



int judge(int row,int col)

{

    if(row>=0&&row<h&&col>=0&&col<w){

        return 1;

    }

    return 0;

}



int bfs()

{

    int res=0;

    deque<P>q;

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

        fill(book[0],book[0]+maxn*maxn,inf);

        q.push_back(P(sx,sy));

        book[sx][sy]=0;

        while(!q.empty()){

            int x=q.front().first;

            int y=q.front().second;

            q.pop_front();

            if((int)s[x][y]==49+ci){

                goal[ci]=book[x][y];

                sx=x;

                sy=y;

                break;

            }

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

                int nx=x+Next[i][0];

                int ny=y+Next[i][1];

                if(judge(nx,ny)&&s[nx][ny]!='X'&&book[nx][ny]==inf){

                    q.push_back(P(nx,ny));

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

                }

            }

        }

        q.clear();

        res+=goal[ci];

    }

    return res;

}



int main()

{

    cin>>h>>w>>n;

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

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

            cin>>s[i][j];

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

                sx=i;

                sy=j;

            }

        }

    }

    int res=bfs();

    cout<<res<<endl;

    return 0;

}




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

#include<cstdio>

#include<cstring>

#include<algorithm>

using namespace std;



#define MAXN 1000

struct node{

    int x,y,st;

    bool operator == (node B) const{

        return x==B.x&&y==B.y;

    }

};

int H,W,N;

char Map[MAXN+5][MAXN+5];

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



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

int bfs(node S,node T){

    queue<node> Q;

    Q.push(S);

    memset(vis,0,sizeof vis);

    while(!Q.empty()){

        node u=Q.front();Q.pop();

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

            node tmp=u;

            tmp.x+=dir[i][0],tmp.y+=dir[i][1],tmp.st++;

            if(tmp.x<1||tmp.x>H||tmp.y<1||tmp.y>W||vis[tmp.x][tmp.y]||Map[tmp.x][tmp.y]=='X')

                continue;

            if(tmp==T)

                return tmp.st;

            vis[tmp.x][tmp.y]=1;

            Q.push(tmp);

        }

    }

    return -1;

}



pair<int,int> pos[10];



int main(){

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

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

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

    node S;

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

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

            if(Map[i][j]!='.'&&Map[i][j]!='X'&&Map[i][j]!='S')

                pos[Map[i][j]-'1'+1]=make_pair(i,j);

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

                S.x=i,S.y=j,S.st=0;

        }

    int Ans=0;

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

        Ans+=bfs(S,(node){pos[i].first,pos[i].second,0});

        S=(node){pos[i].first,pos[i].second,0};

    }

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

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

#include<cstdio>

#include<queue>

#include<cstring>

#include<algorithm>

using namespace std;

typedef long long ll;

typedef pair<int,int>P;

const int Max_n=1100;



int n,m,d;

struct Node{

    int x,y,d;

    Node(int xx,int yy,int dd):x(xx),y(yy),d(dd){}

};

char s[Max_n][Max_n];

bool vis[Max_n][Max_n];

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

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



Node bfs(int x1,int y1,int k){

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

    queue<Node>que;

    que.push(Node(x1,y1,0));

    vis[x1][y1]=1;

    while(!que.empty()){

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

        int x=node.x,y=node.y;

        if(k==s[x][y]-'0')return node;

        int d=node.d;

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

            int X=x+dx[i],Y=y+dy[i];

            if(!vis[X][Y]&&(s[X][Y]=='S'||s[X][Y]=='.'||(s[X][Y]>='1'&&s[X][Y]<='9'))){

                vis[X][Y]=1;

                que.push(Node(X,Y,d+1));

            }

        }

    }

}



int main()

{

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

        memset(s,0,sizeof(s));

        for(int i=1;i<=n;i++)scanf("%s",s[i]+1);

        int x,y;

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

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

            if(s[i][j]=='S'){x=i;y=j;}

        }

        int sum=0;

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

            Node node=bfs(x,y,i);

            sum+=node.d;

            x=node.x,y=node.y;

        }

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

    }

    return 0;

}






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

#include<cstdio>

#include<cstring>

#include<algorithm>

using namespace std;



#define MAXN 1000

struct node{

    int x,y,st;

    bool operator == (node B) const{

        return x==B.x&&y==B.y;

    }

};

int H,W,N;

char Map[MAXN+5][MAXN+5];

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



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

int bfs(node S,node T){

    queue<node> Q;

    Q.push(S);

    memset(vis,0,sizeof vis);

    while(!Q.empty()){

        node u=Q.front();Q.pop();

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

            node tmp=u;

            tmp.x+=dir[i][0],tmp.y+=dir[i][1],tmp.st++;

            if(tmp.x<1||tmp.x>H||tmp.y<1||tmp.y>W||vis[tmp.x][tmp.y]||Map[tmp.x][tmp.y]=='X')

                continue;

            if(tmp==T)

                return tmp.st;

            vis[tmp.x][tmp.y]=1;

            Q.push(tmp);

        }

    }

    return -1;

}



pair<int,int> pos[10];



int main(){

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

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

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

    node S;

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

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

            if(Map[i][j]!='.'&&Map[i][j]!='X'&&Map[i][j]!='S')

                pos[Map[i][j]-'1'+1]=make_pair(i,j);

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

                S.x=i,S.y=j,S.st=0;

        }

    int Ans=0;

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

        Ans+=bfs(S,(node){pos[i].first,pos[i].second,0});

        S=(node){pos[i].first,pos[i].second,0};

    }

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

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

#include<vector>

#include<queue>

#include<string.h>

#include<iostream>

#include<algorithm>

#define INF 1000000000

#define MAXN 100005

using namespace std;

int mmin=11;

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

int a[1005][1005];int m,n,k,sx,sy;

struct node{

    int x,y,cost;

};

int bfs(int x,int y,int s)

{

    queue<node>q;

    bool vis[1005][1005]={0};

    node now={x,y,0};

    q.push(now);

    while(!q.empty())

    {

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

        if(a[now.x][now.y]==s)break;

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

        {

            int xx=now.x+step[i][0],yy=now.y+step[i][1];

            if(!vis[xx][yy]&&a[xx][yy]!=-1){

                vis[xx][yy]=1;

                node temp={xx,yy,now.cost+1};

                q.push(temp);



            }

        }

    }

    //cout<<now.cost<<endl;

    sx=now.x;sy=now.y;

    return now.cost;

}

int main()

{

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

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

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

        string x;cin>>x;

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

        {

            char c=x[j-1];

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

            else if(c=='X')a[i][j]=-1;

            else if(c=='.')a[i][j]=0;

            else a[i][j]=c-'0';

        }

    }

    int sum=0;

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

    {

        sum+=bfs(sx,sy,i);

    }

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

}




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

#include<cstdio>

#include<cstring>

#include<algorithm>

using namespace std;



#define MAXN 1000

struct node{

    int x,y,st;

    bool operator == (node B) const{

        return x==B.x&&y==B.y;

    }

};

int H,W,N;

char Map[MAXN+5][MAXN+5];

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



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

int bfs(node S,node T){

    queue<node> Q;

    Q.push(S);

    memset(vis,0,sizeof vis);

    while(!Q.empty()){

        node u=Q.front();Q.pop();

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

            node tmp=u;

            tmp.x+=dir[i][0],tmp.y+=dir[i][1],tmp.st++;

            if(tmp.x<1||tmp.x>H||tmp.y<1||tmp.y>W||vis[tmp.x][tmp.y]||Map[tmp.x][tmp.y]=='X')

                continue;

            if(tmp==T)

                return tmp.st;

            vis[tmp.x][tmp.y]=1;

            Q.push(tmp);

        }

    }

    return -1;

}



pair<int,int> pos[10];



int main(){

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

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

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

    node S;

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

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

            if(Map[i][j]!='.'&&Map[i][j]!='X'&&Map[i][j]!='S')

                pos[Map[i][j]-'1'+1]=make_pair(i,j);

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

                S.x=i,S.y=j,S.st=0;

        }

    int Ans=0;

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

        Ans+=bfs(S,(node){pos[i].first,pos[i].second,0});

        S=(node){pos[i].first,pos[i].second,0};

    }

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

}
// Slower C++ solution:
//

//  AOJ 0558 Chees

//

//  Created by TaoSama on 2015-02-20

//  Copyright (c) 2014 TaoSama. All rights reserved.

//

#include <algorithm>

#include <cctype>

#include <cmath>

#include <cstdio>

#include <cstdlib>

#include <cstring>

#include <iomanip>

#include <iostream>

#include <map>

#include <queue>

#include <string>

#include <set>

#include <vector>

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



using namespace std;

const int INF = 0x3f3f3f3f;

const int MOD = 1e9 + 7;

const int N = 1e5 + 10;



struct Point {

	int x, y;

	Point(int x = 0, int y = 0): x(x), y(y) {}

} c[15];

int n, m, k, dp[1005][1005];

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

char a[1005][1005];



int bfs(int x) {

	memset(dp, 0x3f, sizeof dp);

	queue <Point> q; q.push(c[x - 1]);

	dp[c[x - 1].x][c[x - 1].y] = 0;

	while(!q.empty()) {

		Point cur = q.front(); q.pop();

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

			Point nxt(cur.x + d[i][0], cur.y + d[i][1]);

			if(nxt.x < 1 || nxt.x > n || nxt.y < 1 || nxt.y > m

			        || a[nxt.x][nxt.y] == 'X') continue;

			if(dp[nxt.x][nxt.y] == INF) {

				dp[nxt.x][nxt.y] = dp[cur.x][cur.y] + 1;

				q.push(nxt);

			}

		}

	}

	return dp[c[x].x][c[x].y];

}

int main() {

#ifdef LOCAL

	freopen("in.txt", "r", stdin);

//	freopen("out.txt","w",stdout);

#endif

	ios_base::sync_with_stdio(0);



	while(cin >> n >> m >> k) {

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

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

				cin >> a[i][j];

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

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

				if(isdigit(a[i][j]))

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

			}

		}

		int ans = 0;

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

			ans += bfs(i);

		cout << ans << endl;

	}

	return 0;

}

// Faster C++ solution for the same problem (measured speedup: 2.653731849055311x):
/*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"stdlib.h"

#include"string.h"

#include"algorithm"

#include"queue"

using namespace std;

struct mmm

{

	int x,y;

}vv;

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

char xx[1005][1005];

int bb[11][2];

queue <mmm> qq;

int vis[1005][1005];

int bfs(char t)

{

	int sum = 0;

	int pl = 0;

	while(!qq.empty())

	{

		vv = qq.front();

		int x0 = vv.x;

		int y0 = vv.y;

		//printf("%d %d %d\n",x0,y0,vis[x0][y0]);

		qq.pop();

		if(xx[x0][y0] == t && !pl)

		{

			sum = vis[x0][y0];

			pl = 1;

		}

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

		{

			int xt = x0 + dir[i][0];

			int yt = y0 + dir[i][1];

			if(xx[xt][yt] != '#' && xx[xt][yt] != 'X' && vis[xt][yt] == -1)

			{

				vis[xt][yt] = vis[x0][y0] + 1;

				vv.x = xt;

				vv.y = yt;

				qq.push(vv);

			}

		}

	}

	return sum;

}

int main()

{

	int a,b,c,d,x1,y1,sum;

	while(~scanf("%d %d %d",&a,&b,&c))

	{

	//	if(a == 0 && b == 0)

	//	break;

		memset(xx,'#',sizeof(xx));

		getchar();

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

		{

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

			{

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

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

				{

					x1 = i;

					y1 = j;

				}

				else if(xx[i][j] != 'X')

				{

					bb[xx[i][j] - '0'][0] = i;

					bb[xx[i][j] - '0'][1] = j;

				}

			}

			getchar();

		}

		bb[0][0] = x1;

		bb[0][1] = y1;

		sum = 0;

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

		{

			vv.x = bb[i-1][0];

			vv.y = bb[i-1][1];

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

			vis[vv.x][vv.y] = 0;

			qq.push(vv);

			sum += bfs(i + '0');

		}

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

	}

	return 0;

}

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

* Author        :somniloquy

* Created Time  :2015/10/30 16:15:09

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



#include <cstdio>

#include <algorithm>

#include <iostream>

#include <cstring>

#include <queue>



using namespace std;



const int hw_max = 1000 + 5;

char graph[hw_max][hw_max];

int sign[hw_max][hw_max];

int h, w, n, ans;

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

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

struct node

{

	int x;

	int y;

	int time;

} start;



void BFS(node op, char ed)

{

	queue <node> process;

	process.push(op);

	sign[op.x][op.y] = 1;

	while(!process.empty())

	{

		node now = process.front();

		process.pop();

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

		{

			node next = now;

			next.x += dx[i];

			next.y += dy[i];

			next.time ++;

			if(next.x >= 0 && next.x < h && next.y >= 0 && next.y < w && graph[next.x][next.y] != 'X' && !sign[next.x][next.y])

			{

				if(graph[next.x][next.y] == ed)

				{

					ans += next.time;

					next.time = 0;

					start = next;

				       	return;

				}

				else

				{

					process.push(next);

					sign[next.x][next.y] = 1;

				}

			}

		}

	}

}



int main()

{

	while(~scanf("%d %d %d", & h, & w, & n))

	{

		getchar();

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

		{

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

			{

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

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

			       {

					start.x = i;

					start.y = j;

					start.time = 0;

			       }

			}

			getchar();

		}

		ans = 0;

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

		{

			memset(sign, 0, sizeof(sign));

			BFS(start, '0' + i);

		}

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

	}

	return 0;

}

// Faster C++ solution for the same problem (measured speedup: 2.6439781207619126x):
/*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<cstdio>

#include<cmath>

#include<string>

#include<queue>

#define CLR(ARR,NUM) memset(ARR,NUM,sizeof(ARR))

#define GETNUM(NUM) scanf("%d",&NUM)

#define faster_io() ios_base::sync_with_stdio(false)

using namespace std;

const int MAXN = 1010;

char map[MAXN][MAXN];

bool vis[10][MAXN][MAXN];

struct node{

	int x;

	int y;

	int deep;

	int n;

};

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

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



int main()

{

	CLR(vis,false);

	CLR(map,'X');

	int h,w,n;

	cin >> h >> w >> n;

	getchar();

	int sx,sy;

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

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

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

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

				sx=i;

				sy=j;

			}

		}

		getchar();

	}

	node t;

	t.x=sx;

	t.y=sy;

	t.deep=0;

	t.n=0;

	queue<node>q;

	vis[0][sx][sy]=true;

	q.push(t);

	int ans = -1;

	while(!q.empty()){

		node cur=q.front();

		if(cur.n==n){

			ans=cur.deep;

			break;

		}

		q.pop();

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

			int tx=cur.x+dx[i];

			int ty=cur.y+dy[i];

			if(map[tx][ty]!='X'&&!vis[cur.n][tx][ty]){

				node t = cur;

				t.x=tx;

				t.y=ty;

				t.deep++;

				if(map[tx][ty]=='1'+t.n){

					t.n++;

				}

				vis[t.n][tx][ty]=true;

				q.push(t);

			}

		}

	}

	cout << ans << endl;

	return 0;

}

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

}//