stablegradients/pie-gem5-pairs huggingface dataset:
// 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.6093857854767775x):
#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 <queue>

#include <cstring>

#include <iostream>

using namespace std;



const int N = 1111;

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

char maze[N][N];

int step[N][N];

int h, w, n, sx, sy, ans;



int bfs(int cheese){

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

    queue<pair<int, int> > que;

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

    step[sx][sy] = 0;

    while(!que.empty()){

        int x = que.front().first, y = que.front().second;

        que.pop();

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

            int nx = x + cx[i], ny = y + cy[i];

            if(nx < 0 || h <= nx || ny < 0 || w <= ny || maze[nx][ny] == 'X' || step[nx][ny] != -1) continue;

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

            if(maze[nx][ny] - '0' == cheese){

                sx = nx;

                sy = ny;

                return step[nx][ny];

            }

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

        }

    }

    return 0;

}



int main(){

    ios::sync_with_stdio(false);

    cin.tie(NULL);

    while(cin >> h >> w >> n){

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

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

                cin >> maze[i][j];

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

                    sx = i;

                    sy = j;

                }

            }

        ans = 0;

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

            ans += bfs(i);

        cout << ans << endl;

    }

    return 0;

}

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

#include <string.h>

using namespace std;

typedef pair<int, int> P;

int sx,sy,H,W,N;

char map1[1002][1002];

int step[1002][1002];

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

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

void find1(int g){

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

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

        if(map1[i][k] == 'S' || map1[i][k] == (g+48)){

        if(map1[i][k] == 'S') map1[i][k] ='.';

        sx = i; sy = k;

        return ;

    }

}



int bfs(int n)

{

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

    queue <P> que;

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

    step[sx][sy] = 0;

    while(que.size()){

        int x = que.front().first, y = que.front().second;

        que.pop();

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

            int xn = x + dx[i], yn = y + dy[i];

            if(xn >= 0 && xn < W && yn >= 0 && yn < H && map1[xn][yn] != 'X' && step[xn][yn] == -1)

                {

                    step[xn][yn] = step[x][y] + 1;

                    if(map1[xn][yn] == (n+48))

                        {

                            return step[xn][yn];

                        }

                    que.push(P(xn, yn));

                }

        }

    }

return 0;

}



int main()

{

    cin >> W >> H >> N;

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

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

        cin >> map1[i][k];

    int step = 0;

    find1(0);

    step += bfs(1);

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

    {



        find1(i);

        step += bfs(i+1);

    }

    cout << step << endl;

    return 0;

}

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

#include<cstring>

using namespace std;

char map[1005][1005];

bool vis[1005][1005];

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

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

int sx,sy;

int h,w;

int bfs(int n){

	memset(vis,false,sizeof(vis));

	queue<pair<pair<int,int>,int> >que;

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

	while(!que.empty()){

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

		int cnt=que.front().second;

		que.pop();

		if(map[now.first][now.second]==n+48){

			sx=now.first;

			sy=now.second;

			return cnt;

		}

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

			int _x=now.first+dx[i];

			int _y=now.second+dy[i];

			if(_x<0||_y<0||_x>=h||_y>=w||map[_x][_y]=='X'||vis[_x][_y]){

				continue;

			}

			vis[_x][_y]=true;

			que.push(make_pair(make_pair(_x,_y),cnt+1));

		}

	}

}

int main(){

	int n;

	while(cin>>h>>w>>n){

		memset(map,0,sizeof(map));

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

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

		}

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

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

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

					sx=i;

					sy=j;

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

					break;

				}

			}

		}

		int count=0;

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

			count+=bfs(k);

		}

		cout<<count<<endl;

	}

	return 0;

}

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

#include<cstdio>

#include<cstring>

#include<iostream>

#include<algorithm>

#define SC(x) scanf("%d",&x);

#define pr(x)  cout << x << endl;

#define mem(arr,v) memset(arr,v,sizeof arr);

#define bug(x) cout << "#x = " << x <<endl;

const int inf = 0x3f3f3f3f;

const int maxn = 1010;

typedef long long ll;

using namespace std;

char Mz[maxn][maxn],c;

bool vis[maxn][maxn];

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

int M,N,C;

int px[10],py[10];

struct node {

	int x,y,step;

};



int bfs(int des,int sx,int sy) {

	mem(vis,0);

	vis[sx][sy] = 1;

	node nod = {sx,sy,0};

	queue<node> q;

	q.push(nod);



	while(!q.empty()) {

		nod =  q.front();

		q.pop();

		

		int x = nod.x, y = nod.y;

		int step =  nod.step;

		

		if(Mz[x][y] == des + '0')

			return step;

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

			int dx = x + dir[i][0];

			int dy = y + dir[i][1];

			if(Mz[dx][dy] == 'X' || Mz[dx][dy] == -1 || vis[dx][dy])	continue;

			nod = {dx,dy,step + 1};

			q.push(nod);

			vis[dx][dy] = 1;

		}

	}

}



int main() {

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

	cin >> N >> M >> C;

	mem(Mz,-1)

	mem(px,0)  mem(py,0)



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

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

			cin >> c;

			Mz[i][j] = c;

			if(c == '.' || c == 'X')	continue;

			if(c == 'S') px[0] = i, py[0] = j;

			else

				px[c - '0'] = i , py[c - '0'] = j;

		}

	int res = 0;

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

		if(px[i] && py[i]) {

//				dfs(i + 1,px[i],py[i],0);

			res += bfs(i + 1,px[i],py[i]);

		}

	}

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

}

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

}