stablegradients/pie-gem5-pairs huggingface dataset:
// Slower C++ solution:
#include <algorithm>

#include <climits>

#include <cmath>

#include <csignal>

#include <cstdio>

#include <cstdlib>

#include <functional>

#include <iomanip>

#include <iostream>

#include <map>

#include <queue>

#include <set>

#include <string>

#include <tuple>

#include <vector>



using Graph = std::vector<std::vector<int>>;

using WGraph = std::vector<std::vector<std::pair<int, long long>>>;

using ll = long long;

using namespace std;



constexpr ll mod = 1000000007;

 

int main() {

    int n = 0;

    cin >> n;

    vector<int> a(n);

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

        cin >> a.at(i);

    }

    

    int cnt0 = 0;

    for (int x : a) {

        if (x == 0) {

            cnt0++;

        }

    }

 

    if (cnt0 > 3) {

        cout << 0 << endl;

        return 0;

    }

 

    ll color = (cnt0 == 1) ? 3 : 6;

 

    ll ans = 1;

    vector<ll> cur(1e5, 0);

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

        if (a.at(i) == 0) {

            cur.at(0)++;

        } else {

            ans *= cur.at(a.at(i)-1);

            ans %= mod;

            cur.at(a.at(i))++;

            cur.at(a.at(i)-1)--;

        }

    }

    

    cout << ans * color % mod << endl;

 

    return 0;

}


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

#include <climits>

#include <cmath>

#include <csignal>

#include <cstdio>

#include <cstdlib>

#include <functional>

#include <iomanip>

#include <iostream>

#include <map>

#include <queue>

#include <set>

#include <string>

#include <tuple>

#include <vector>



using Graph = std::vector<std::vector<int>>;

using WGraph = std::vector<std::vector<std::pair<int, long long>>>;

using ll = long long;

using namespace std;



constexpr ll mod = 1000000007;

 

int main() {

    int n = 0;

    cin >> n;

    vector<int> a(n);

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

        cin >> a.at(i);

    }

    

    int cnt0 = 0;

    for (int x : a) {

        if (x == 0) {

            cnt0++;

        }

    }

 

    if (cnt0 > 3) {

        cout << 0 << endl;

        return 0;

    }

 

    ll color = (cnt0 == 1) ? 3 : 6;

 

    ll ans = 1;

    vector<int> cur(1e5, 0);

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

        if (a.at(i) == 0) {

            cur.at(0)++;

        } else {

            ans *= cur.at(a.at(i)-1);

            ans %= mod;

            cur.at(a.at(i))++;

            cur.at(a.at(i)-1)--;

        }

    }

    

    cout << ans * color % mod << endl;

 

    return 0;

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

#include <climits>

#include <cmath>

#include <csignal>

#include <cstdio>

#include <cstdlib>

#include <functional>

#include <iomanip>

#include <iostream>

#include <map>

#include <queue>

#include <set>

#include <string>

#include <tuple>

#include <vector>



using Graph = std::vector<std::vector<int>>;

using WGraph = std::vector<std::vector<std::pair<int, long long>>>;

using ll = long long;

using namespace std;



constexpr ll mod = 1000000007;

 

int main() {

    int n = 0;

    cin >> n;

    vector<int> a(n);

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

        cin >> a.at(i);

    }

    

    int cnt0 = 0;

    for (int x : a) {

        if (x == 0) {

            cnt0++;

        }

    }

 

    if (cnt0 > 3) {

        cout << 0 << endl;

        return 0;

    }

 

    ll color = (cnt0 == 1) ? 3 : 6;

 

    ll ans = 1;

    vector<ll> cur(1e5, 0);

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

        if (a.at(i) == 0) {

            cur.at(0)++;

        } else {

            ans *= cur.at(a.at(i)-1) % mod;

            ans %= mod;

            cur.at(a.at(i))++;

            cur.at(a.at(i)-1)--;

        }

    }

    

    cout << ans * color % mod << endl;

 

    return 0;

}


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

#include <climits>

#include <cmath>

#include <csignal>

#include <cstdio>

#include <cstdlib>

#include <functional>

#include <iomanip>

#include <iostream>

#include <map>

#include <queue>

#include <set>

#include <string>

#include <tuple>

#include <vector>



using Graph = std::vector<std::vector<int>>;

using WGraph = std::vector<std::vector<std::pair<int, long long>>>;

using ll = long long;

using namespace std;



constexpr ll mod = 1000000007;



int main() {

    int n = 0;

    cin >> n;

    vector<int> a(n);

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

        cin >> a.at(i);

    }

    

    int cnt0 = 0;

    for (int x : a) {

        if (x == 0) {

            cnt0++;

        }

    }

  

  	if (cnt0 > 3) {

      	cout << 0 << endl;

      	return 0;

    }



    ll color = 0;

    if (cnt0 == 1) {

        color = 3;

    } else {

        color = 6;

    }



    ll ans = 1;

    vector<int> cur(1e5);

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

        if (a.at(i) == 0) {

            cur.at(0)++;

        } else {

            ans *= cur.at(a.at(i)-1);

            ans %= mod;

            cur.at(a.at(i))++;

            cur.at(a.at(i)-1)--;

        }

    }

    

    cout << ans * color % mod << endl;



    return 0;

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

#include <climits>

#include <cmath>

#include <csignal>

#include <cstdio>

#include <cstdlib>

#include <functional>

#include <iomanip>

#include <iostream>

#include <map>

#include <queue>

#include <set>

#include <string>

#include <tuple>

#include <vector>



using Graph = std::vector<std::vector<int>>;

using WGraph = std::vector<std::vector<std::pair<int, long long>>>;

using ll = long long;

using namespace std;



constexpr ll mod = 1000000007;

 

int main() {

    int n = 0;

    cin >> n;

    vector<int> a(n);

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

        cin >> a.at(i);

    }

    

    int cnt0 = 0;

    for (int x : a) {

        if (x == 0) {

            cnt0++;

        }

    }

 

    if (cnt0 > 3) {

        cout << 0 << endl;

        return 0;

    }

 

    ll color = (cnt0 == 1) ? 3 : 6;

 

    ll ans = 1;

    vector<ll> cur(1e5, 0);

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

        if (a.at(i) == 0) {

            cur.at(0)++;

        } else {

            ans *= cur.at(a.at(i)-1);

            ans %= mod;

            cur.at(a.at(i))++;

            cur.at(a.at(i)-1)--;

        }

    }

    

    cout << ans * color % mod << endl;

 

    return 0;

}


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

#include <climits>

#include <cmath>

#include <csignal>

#include <cstdio>

#include <cstdlib>

#include <functional>

#include <iomanip>

#include <iostream>

#include <map>

#include <queue>

#include <set>

#include <string>

#include <tuple>

#include <vector>



using Graph = std::vector<std::vector<int>>;

using WGraph = std::vector<std::vector<std::pair<int, long long>>>;

using ll = long long;

using namespace std;



constexpr ll mod = 1000000007;



int main() {

    int n = 0;

    cin >> n;

    vector<int> a(n);

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

        cin >> a.at(i);

    }

    

    int cnt0 = 0;

    for (int x : a) {

        if (x == 0) {

            cnt0++;

        }

    }

  

  	if (cnt0 > 3) {

      	cout << 0 << endl;

      	return 0;

    }



    ll color = 0;

    if (cnt0 == 1) {

        color = 3;

    } else {

        color = 6;

    }



    ll ans = 1;

    vector<int> cur(1e5);

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

        if (a.at(i) == 0) {

            cur.at(0)++;

        } else {

            ans *= cur.at(a.at(i)-1);

            ans %= mod;

            cur.at(a.at(i))++;

            cur.at(a.at(i)-1)--;

        }

    }

    

    cout << ans * color % mod << endl;



    return 0;

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

using namespace std;

const int MX = 1e5+3, MOD = 1e9+7;

int a[MX];

vector<int> pos[MX];

long long ans = 1;

int main() {

	ios::sync_with_stdio(0);cin.tie(0);

	int n, mxp = 0;

	cin >> n;

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

		cin >> a[i];

		pos[a[i]].push_back(i);

		mxp = max(mxp, a[i]);

		if(pos[a[i]].size() > 3)

			return !(cout << 0);

	}

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

		if(pos[i].empty() || (i && pos[i-1].size() < pos[i].size()))

			return !(cout << 0);

	}

	ans = (pos[0].size() >= 2 ? 6 : 3);

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

		int p = 0;

		for(int t: pos[i]) {

			int cnt = 0;

			for(int s: pos[i-1]) {

				cnt += s < t;

			}

			if(cnt <= p)

				return !(cout << 0);

			ans *= (cnt - p);

			p++;

		}

		ans %= MOD;

	}

	cout << ans;

}

// Faster C++ solution for the same problem (measured speedup: 3.516061062340359x):
#include <bits/stdc++.h>

using namespace std;



int main() {

	ios::sync_with_stdio(0);cin.tie(0);

	int n;

	cin >> n;

	int t[3] = {0,0,0};

	long long ans = 1;

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

		int a;

		cin >> a;

		int p = -1, cnt = 0;

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

			if(t[j] == a) {

				p = j;

				cnt++;

			}

		}

		ans *= cnt;

		t[p]++;

		ans %= (int)(1e9+7);

	}

	cout << ans;

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

#include <string>

#include <algorithm>

#include <cmath>

#include <cstdlib>

#include <vector>

#include <map>

#include <queue> 

#include <stack>

#include <set>

#include <list>



using namespace std;

typedef long long ll;

#define rep(i, s, e) for (int(i) = (s); (i) <= (e); ++(i))

#define all(x) x.begin(),x.end()





int main()

{

	int n; cin >> n;

	ll ans = 1;

	vector<int> num(100100, 0);

	num[0] = 3;

	rep(i, 0, n - 1)

	{

		int a; cin >> a;

		ans *= num[a];

		ans %= 1000000007;

		--num[a];

		++num[a + 1];

	}

	cout << ans << endl;

}


// Faster C++ solution for the same problem (measured speedup: 1.4014288348754849x):
#pragma region

#include <iostream>

#include <string>

#include <algorithm>

#include <cmath>

#include <cstdlib>

#include <vector>

#include <map>

#include <queue> 

#include <stack>

#include <set>

#include <list>



using namespace std;

typedef long long ll;

//#define rep(i, s, e) for (int(i) = (s); (i) < (e); ++(i))

#define rep(i, e) for (int(i) = 0; (i) < (e); ++(i))

#define rrep(i, s) for (int(i) = (s) - 1; (i) >= 0; --(i))

#define all(x) x.begin(),x.end()

#pragma endregion





int main()

{

	int n; cin >> n;

	vector<ll> memo(3, 0);

	ll MOD = 1e9 + 7;

	ll res = 1;

	rep(i, n)

	{

		int a; cin >> a;

		ll cnt = 0;

		rep(i, 3)

		{

			if (memo[i] == a)++cnt;

		}

		res *= cnt;

		res %= MOD;

		rep(i, 3)

		{

			if (memo[i] == a)

			{

				++memo[i];

				break;

			}

		}

	}

	cout << res << endl;

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

using namespace std;

const int MAX = 100000, mod = 1e9+7;

int A[MAX];

int main(){

    int n, maxi = 0;

    long long sum = 1;

    cin >> n;

    A[0] = 3;

    

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

    {

        int a;

        cin >> a;

        sum = (sum * A[a]) % mod;

        A[a]--;

        A[a+1]++;

    }

    cout << sum << endl;

}

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

using namespace std;

const int MAX = 100000, mod = 1e9+7;

int A[MAX];

int main(){

    int n;

    long long sum = 1;

    scanf("%d", &n);

    A[0] = 3;

    

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

    {

        int a;

        scanf("%d", &a);

        sum = (sum * A[a]) % mod;

        A[a]--;

        A[a+1]++;

    }

    printf("%ld", sum);

}
// Slower C++ solution:
// E



#include <bits/stdc++.h>

using ll = long long;

using namespace std;

ll mod=1e9+7;



int main(){

  int N,A[100000];

  cin>>N;

  for (int i(0);i<N;i++) cin>>A[i];

  ll dp[100001][3] = {0};



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

    bool flag = true;

    for (int j(0);j<3;j++){

      if (dp[i-1][j] == A[i-1] && flag == true){

        dp[i][j] = dp[i-1][j]+1;

        flag = false;

      } else {

        dp[i][j] = dp[i-1][j];

      }

    }

    sort(dp[i],dp[i]+3,greater<int>());

  }



  ll cnt(1);

  for (int i(0);i<N;i++){

    int cnti(0);

    for (int j(0);j<3;j++){

      if (A[i] == dp[i][j]) cnti++;

    }

    cnt*= cnti;

    cnt%=mod;

  }

  cout << cnt << endl;

  return 0;

}


// Faster C++ solution for the same problem (measured speedup: 1.5482061126846063x):
// E



#include <bits/stdc++.h>

using ll = long long;

using namespace std;

ll mod=1e9+7;



int main(){

  int N,A[100000];

  cin>>N;

  for (int i(0);i<N;i++) cin>>A[i];

  int dp[100001][3] = {0};



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

    bool flag = true;

    for (int j(0);j<3;j++){

      if (dp[i-1][j] == A[i-1] && flag == true){

        dp[i][j] = dp[i-1][j]+1;

        flag = false;

      } else {

        dp[i][j] = dp[i-1][j];

      }

    }

    sort(dp[i],dp[i]+3,greater<int>());

  }



  ll cnt(1);

  for (int i(0);i<N;i++){

    int cnti(0);

    for (int j(0);j<3;j++){

      if (A[i] == dp[i][j]) cnti++;

    }

    cnt*= cnti;

    cnt%=mod;

  }

  cout << cnt << 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: 5.1514311830261645x):
#include<iostream>

#include<cstdio>

#include<algorithm>

#include<queue>

#include<cstring>

using namespace std;

typedef pair<int,int> P;

int N,M,L,sx,sy,gx,gy,num;

const int INF = 100000000;

char maze[1100][1100];

int d[1100][1100];

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

P s[10];

void bfs()

{

	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.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 +dx[i],ny=p.second +dy[i];

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

				{

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

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

				}   

			}

	}

}

void solve()

{

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

	{

		sx=s[i].first ,sy=s[i].second;

		gx=s[i+1].first ,gy=s[i+1].second;

		bfs();

		num+=d[gx][gy];

	}

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

}

int main()

{

	while(~scanf("%d %d %d",&N,&M,&L))

	{

		num=0;

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

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

			{

				scanf("\n%c",&maze[i][j]);

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

				s[0].first=i ,s[0].second=j ; 

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

				s[maze[i][j]-'0'].first =i,s[maze[i][j]-'0'].second =j;

			}

		solve();

	}

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

#include<cstdio>

#include<algorithm>

#include<queue>

#include<cstring>

using namespace std;

typedef pair<int,int> P;

int N,M,L,sx,sy,gx,gy,num;

const int INF = 100000000;

char maze[1100][1100];

int d[1100][1100];

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

P s[10];

void bfs()

{

	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.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 +dx[i],ny=p.second +dy[i];

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

				{

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

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

				}   

			}

	}

}

void solve()

{

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

	{

		sx=s[i].first ,sy=s[i].second;

		gx=s[i+1].first ,gy=s[i+1].second;

		bfs();

		num+=d[gx][gy];

	}

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

}

int main()

{

	while(~scanf("%d %d %d",&N,&M,&L))

	{

		num=0;

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

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

			{

				scanf("\n%c",&maze[i][j]);

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

				s[0].first=i ,s[0].second=j ; 

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

				s[maze[i][j]-'0'].first =i,s[maze[i][j]-'0'].second =j;

			}

		solve();

	}

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

#include<cstdio>

#include<algorithm>

#include<queue>

#include<cstring>

using namespace std;

typedef pair<int,int> P;

int N,M,L,sx,sy,gx,gy,num;

const int INF = 100000000;

char maze[1100][1100];

int d[1100][1100];

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

P s[10];

void bfs()

{

	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.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 +dx[i],ny=p.second +dy[i];

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

				{

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

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

				}   

			}

	}

}

void solve()

{

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

	{

		sx=s[i].first ,sy=s[i].second;

		gx=s[i+1].first ,gy=s[i+1].second;

		bfs();

		num+=d[gx][gy];

	}

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

}

int main()

{

	while(~scanf("%d %d %d",&N,&M,&L))

	{

		num=0;

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

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

			{

				scanf("\n%c",&maze[i][j]);

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

				s[0].first=i ,s[0].second=j ; 

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

				s[maze[i][j]-'0'].first =i,s[maze[i][j]-'0'].second =j;

			}

		solve();

	}

	return 0;

}