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: 5.123467327383566x):
#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=0;

Node sN;

char c[max_n][max_n];



int bfs(int m){

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

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

	        d[i][j]=0;

	queue<Node> q;

	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){

		    sN.x=p.x;

		    sN.y=p.y;

		}

		else{

			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;

				}

			}

		}

	}

	return d[sN.x][sN.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++)ans+=bfs(i);

    cout<<ans<<endl;

    return 0;

}
// 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: 4.4932115392255625x):
#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;

}
// 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: 5.3392726525560406x):
#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;

}
// 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: 4.47669684708213x):
#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;

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

#include <cmath>

#include <queue>

#include <cstdio>

#include <cstring>

#include <algorithm>



#define INF 0x7f7f7f7f

#define PI acos(-1.0)



#define L(x)    (x) << 1

#define R(x)    (x) << 1 | 1

#define lowbit(x)   (x)&(-x)

#define Mid(l, r)   (l + r) >> 1

#define Min(x, y)   (x) < (y) ? (x) : (y)

#define Max(x, y)   (x) < (y) ? (y) : (x)

#define iabs(x)     (x) < 0 ? -(x) : (x)

#define CLR(arr, val)    memset(arr, val, sizeof(arr))



using namespace std;



char map[1024][1024];

bool vis[1024][1024];

int cur, h, w;

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



struct node {

	int x, y, step;

} sta, now;



bool check(int x, int y) {

	if (x >= 0 && x < h && y >= 0 && y < w && !vis[x][y] && map[x][y] != 'X')	return true;

	return false;

}







void bfs(node s) {

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



	queue<node> que;

	s.step = 0;

	que.push(s);

	node next;

	while (!que.empty()) {

//		cout << "h\n";

		s = que.front();

//		cout << s.x << ' ' << s.y << ' ' << cur << endl;

		que.pop();

		if (map[s.x][s.y] == cur + '0')	{

			sta = s;

			return ;

//			s.step;

		}



//		cout << "a\n";

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

//			cout << i << endl;

			next.x = s.x + dir[i][0];

			next.y = s.y + dir[i][1];

			next.step = s.step + 1;

			if (check(next.x, next.y)) {

				vis[next.x][next.y] = true;

				que.push(next);

			}



		}

	}

}



int main() {

	int n;

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

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

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

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

				cin >> map[i][j];

				if (map[i][j] == 'S')	sta.x = i, sta.y = j, map[i][j] = '.';

			}

		}

		sta.step = 0;

		int ans = 0;

		for (cur = 1; cur <= n; cur++) {

			bfs(sta);

			ans += sta.step;

			//cout << ans << endl;

		}

		cout << ans << endl;

	}

	return 0;

}

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

}
// 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: 11.230268031045359x):
#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;

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

using namespace std;

const int MAX = 1024;



int h, w, n, maze[MAX][MAX];

int b[15];

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

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

int d[MAX][MAX];

int ans = 0;



int readchar() {

    for(;;) {

        int ch = getchar();

        if(ch != '\n' && ch != '\r') return ch;

    }

}



int bfs(int x, int y, int x2, int y2)

{

    //cout << x << y << x2 << y2 << endl;



    queue<int> q;

    q.push(x * 10000 + y);

    while(!q.empty()) {

        int v = q.front();

        q.pop();

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

            int x1 = v / 10000 + r[i];

            int y1 = v % 10000 + z[i];

            if(x1 == x2 && y1 == y2) {

                d[x1][y1] = d[v / 10000][v % 10000] + 1;

                return 0;

            }

            if(x1 >= 0 && y1 >= 0 && x1 < h && y1 < w && maze[x1][y1] != 'X' && d[x1][y1] == -1) {

                 d[x1][y1] = d[v / 10000][v % 10000] + 1;

                 q.push(x1 * 10000 + y1);

            }

        }

    }

    return 0;

}

int main()

{

    cin >> h >> w >> n;

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

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

             maze[i][j] = readchar();

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

            b[0] = i * 10000 + j;

        }

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

            b[maze[i][j] - '0'] = i * 10000 + j;

        }

    }

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

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

        d[b[i] / 10000][b[i] % 10000] = 0;

        bfs(b[i] / 10000, b[i] % 10000, b[i + 1] / 10000, b[i + 1] % 10000);

        ans += d[b[i + 1] / 10000][ b[i + 1] % 10000];

    }

    cout << ans << endl;

    return 0;

}

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

}
// 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: 5.336682939408512x):
#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;

}