stablegradients/pie-gem5-pairs huggingface dataset:
// 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: 6.0499357330383x):
#include <iostream>

#include <cstdio>

#include <queue>

#include <algorithm>

#include <cstring>

#include <string>

#include <stack>

#include <stdlib.h>

#include <cctype>

#include <vector>

#include <map>

#include <set>

#include <cmath>

#include <iomanip>

using namespace std;

const int INF=1000000000;

const int max_n=1005;

typedef pair<int,int>P;

char maze[max_n][max_n+1];

int n,m,sx,sy;

int d[max_n][max_n];

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

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

int bfs(char e)

{

    queue<P>que;

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

        for(int j=0;j<m;j++)d[i][j]=INF;

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

    d[sx][sy]=0;

    while(!que.empty())

    {

        P q=que.front();

        que.pop();

        if(maze[q.first][q.second]==e){sx=q.first;sy=q.second;break;}

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

        {

            int nx=q.first+dx[i],ny=q.second+dy[i];

            if(0<=nx&&nx<n&&ny>=0&&ny<m&&maze[nx][ny]!='X'&&d[nx][ny]==INF)

            {

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

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

            }

        }

    }

return d[sx][sy];

}

int main()

{

    int t;

    while(cin>>n>>m>>t)

    {

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

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

       {

           cin>>maze[i][j];

           if(maze[i][j]=='S')sx=i,sy=j;

       }



       int res=0;

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

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

       }

        cout<<res<<endl;

    }

    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: 12.725029095506725x):
#include <iostream>

#include <cstdio>

#include <queue>

#include <algorithm>

#include <cstring>

#include <string>

#include <stack>

#include <stdlib.h>

#include <cctype>

#include <vector>

#include <map>

#include <set>

#include <cmath>

#include <iomanip>

using namespace std;

const int INF=1000000000;

const int max_n=1005;

typedef pair<int,int>P;

char maze[max_n][max_n+1];

int n,m,sx,sy;

int d[max_n][max_n];

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

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

int bfs(char e)

{

    queue<P>que;

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

        for(int j=0;j<m;j++)d[i][j]=INF;

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

    d[sx][sy]=0;

    while(!que.empty())

    {

        P q=que.front();

        que.pop();

        if(maze[q.first][q.second]==e){sx=q.first;sy=q.second;break;}

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

        {

            int nx=q.first+dx[i],ny=q.second+dy[i];

            if(0<=nx&&nx<n&&ny>=0&&ny<m&&maze[nx][ny]!='X'&&d[nx][ny]==INF)

            {

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

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

            }

        }

    }

return d[sx][sy];

}

int main()

{

    int t;

    while(cin>>n>>m>>t)

    {

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

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

       {

           cin>>maze[i][j];

           if(maze[i][j]=='S')sx=i,sy=j;

       }



       int res=0;

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

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

       }

        cout<<res<<endl;

    }

    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: 12.73614427391744x):
#include <iostream>

#include <queue>

using namespace std;

int w, h, n;

char map[1024][1024];

// 各点到当前工厂的距?

int d[1024][1024];

const int direction[4][2] = {

	{ -1, 0 },

	{ 1, 0 },

	{ 0, -1 },

	{ 0, 1 },

};

 

int factory[16][2];

typedef pair<int, int> P;



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

{

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

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

	{

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

		{

			d[j][i] = -1;

		}

	}

	queue<P> que;

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

	d[sx][sy] = 0;

	while (que.size())

	{

		const P p = que.front(); que.pop();

		// 如果是?点就?束

		if (p.first == gx && p.second == gy)

		{

			break;

		}

 

		// 四方向漫游

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

		{

			int nx = p.first + direction[i][0];

			int ny = p.second + direction[i][1];

			// 是否可以移?，并且?点没有???

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

			{

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

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

			}

		}

	}



	return d[gx][gy];

}

 

///////////////////////////SubMain//////////////////////////////////

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

{

 

	cin >> h >> w >> n;

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

	{

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

		{

			cin >> map[j][i];

		}

	}

 

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

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

		{

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

		{

				factory[0][0] = j;

				factory[0][1] = i;

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

			}

			else if (isdigit(map[j][i]))

			{

				int index = map[j][i] - '0';

				factory[index][0] = j;

				factory[index][1] = i;

			}

		}

	}

 

	int step = 0;

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

	{

		// 按?序吃遍中?

		step += bfs(factory[i][0], factory[i][1], factory[i + 1][0], factory[i + 1][1]);

	}

	

	cout << step << endl;

 

	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: 6.125123763172162x):
#include<iostream>

#include<queue>

#include<cstdio>

#include<cstring>

#include<utility>

using namespace std;

struct dir

{

	int x,y;

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

char hole[1005][1005];

int d[1005][1005],h,w,factory[10][2];

typedef pair<int,int> P;

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

{

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

	{

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

		{

			d[i][j] = -1;

		}

	}

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

	queue<P> que;

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

	d[sx][sy]=0;

	while(que.size())

	{

		P p=que.front();

		que.pop();	

		if(p.first==gx&&p.second==gy)

		break;

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

		{

		int nx=p.first+dir[i].x;

		int ny=p.second+dir[i].y;

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

		{

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

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

		}

		}

	}

	return d[gx][gy];

}

int main()

{

	int sx,sy,i,j,step=0,n;

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

	//cin>>h>>w>>n;

	for(i=0;i<h;i++,getchar())

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

		{

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

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

			{

				factory[0][0]=i;

				factory[0][1]=j;

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

			}

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

			{

				int index=hole[i][j]-'0';

			factory[index][0]=i;

			factory[index][1]=j;

			}

		}

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

	step+=bfs(factory[i][0],factory[i][1],factory[i+1][0],factory[i+1][1]);

	//cout<<step<<endl;

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

	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: 12.883174555773746x):
#include<iostream>

#include<queue>

#include<cstdio>

#include<cstring>

#include<utility>

using namespace std;

struct dir

{

	int x,y;

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

char hole[1005][1005];

int d[1005][1005],h,w,factory[10][2];

typedef pair<int,int> P;

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

{

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

	{

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

		{

			d[i][j] = -1;

		}

	}

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

	queue<P> que;

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

	d[sx][sy]=0;

	while(que.size())

	{

		P p=que.front();

		que.pop();	

		if(p.first==gx&&p.second==gy)

		break;

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

		{

		int nx=p.first+dir[i].x;

		int ny=p.second+dir[i].y;

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

		{

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

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

		}

		}

	}

	return d[gx][gy];

}

int main()

{

	int sx,sy,i,j,step=0,n;

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

	//cin>>h>>w>>n;

	for(i=0;i<h;i++,getchar())

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

		{

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

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

			{

				factory[0][0]=i;

				factory[0][1]=j;

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

			}

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

			{

				int index=hole[i][j]-'0';

			factory[index][0]=i;

			factory[index][1]=j;

			}

		}

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

	step+=bfs(factory[i][0],factory[i][1],factory[i+1][0],factory[i+1][1]);

	//cout<<step<<endl;

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

	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: 2.426340469750984x):
#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;

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

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

}