Go: ///
// File:  0558.go
// Author: ymiyamoto
//
// Created on Sun Mar  4 20:58:45 2018
//
package main

import (
	"fmt"
	"strconv"
	"strings"
)

type pos struct {
	x, y int
}

var H, W, N int
var graph [][]string

func wfs(p pos, index int) (int, pos) {
	var q []pos
	dists := make([][]int, H)
	for i := range dists {
		dists[i] = make([]int, W)
	}

	q = append(q, p)
	for len(q) > 0 {
		p := q[0]
		dist := dists[p.x][p.y]
		q = q[1:]

		for _, d := range []pos{{-1, 0}, {1, 0}, {0, -1}, {0, 1}} {
			x := p.x + d.x
			y := p.y + d.y
			if 0 <= x && x < H && 0 <= y && y < W && graph[x][y] != "X" && dists[x][y] == 0 {
				dists[x][y] = dist + 1
				q = append(q, pos{x, y})
			}
		}
	}

	for i := range graph {
		for j := range graph[i] {
			if graph[i][j] == strconv.Itoa(index) {
				return dists[i][j], pos{i, j}
			}
		}
	}

	panic("not found")
}

func main() {
	fmt.Scan(&H, &W, &N)

	var start pos

	graph = make([][]string, H)
	for i := range graph {
		var s string
		fmt.Scan(&s)
		graph[i] = strings.Split(s, "")
		for j := range graph[i] {
			if graph[i][j] == "S" {
				start.x = i
				start.y = j
			}
		}
	}

	count := 0
	for i := 1; i <= N; i++ {
		c, s := wfs(start, i)
		start = s
		count += c
	}

	fmt.Println(count)
}

C++: // ?????¬???????????¬?????? (define int long long ?????????)

#include <iostream>
#include <iomanip>
#include <cstdio>
#include <string>
#include <cstring>
#include <deque>
#include <list>
#include <queue>
#include <stack>
#include <vector>
#include <utility>
#include <algorithm>
#include <map>
#include <set>
#include <complex>
#include <cmath>
#include <limits>
#include <cfloat>
#include <climits>
#include <ctime>
#include <cassert>
#include <numeric>
#include <functional>
using namespace std;

#define rep(i,a,n) for(int (i)=(a); (i)<(n); (i)++)
#define repq(i,a,n) for(int (i)=(a); (i)<=(n); (i)++)
#define repr(i,a,n) for(int (i)=(a); (i)>=(n); (i)--)

template<typename T> void chmax(T &a, T b) {a = max(a, b);}
template<typename T> void chmin(T &a, T b) {a = min(a, b);}
template<typename T> void chadd(T &a, T b) {a = a + b;}

typedef pair<int, int> pii;
typedef long long ll;

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


const int INF = 1 << 25;

int H, W, N;
char board[1010][1010];
int dist[1010][1010];
int x[10], y[10];

int bfs(int sx, int sy, int tx, int ty) {
    rep(i,0,H) rep(j,0,W) dist[i][j] = INF;
    dist[sx][sy] = 0;

    queue<pii> que;
    que.push(make_pair(sx, sy));

    while(que.size()) {
        pii cur = que.front(); que.pop();
        int cx, cy;
        tie(cx, cy) = cur;

        rep(k,0,4) {
            int nx = cx + dx[k], ny = cy + dy[k];
            if(nx < 0 || nx >= H || ny < 0 || ny >= W) continue;
            if(board[nx][ny] == 'X') continue;
            if(dist[nx][ny] > dist[cx][cy] + 1) {
                dist[nx][ny] = dist[cx][cy] + 1;
                que.push(make_pair(nx, ny));
            }
        }
    }

    return dist[tx][ty];
}

int main() {
    cin >> H >> W >> N;
    rep(i,0,H) rep(j,0,W) {
        cin >> board[i][j];
        if(board[i][j] == 'S') board[i][j] = '0';
        if(isdigit(board[i][j])) {
            int val = board[i][j] - '0';
            x[val] = i, y[val] = j;
        }
    }

    int ans = 0;
    rep(i,0,N) {
        ans += bfs(x[i], y[i], x[i+1], y[i+1]);
    }
    cout << ans << endl;
    return 0;
}

C++: #include<stdio.h>
#include<iostream>
#include<queue>
using namespace std;
int m[1002][1002]={0},p[1002][1002]={0};
int main(){
int h,w,n,i,j,a,b[2];
b[0]=1;
b[1]=-1;
char c[2001];
pair<int,int> g,l;
queue<pair<int,int> > q,e;
cin>>h>>w>>n;
for(i=0;i<=h+1;i++){
m[i][0]=-1;
m[i][w+1]=-1;
}
for(i=0;i<=w+1;i++){
m[0][i]=-1;
m[h+1][i]=-1;
}
for(i=1;i<=h;i++){
	scanf("%s",c);
	for(j=1;j<=w;j++){
		a=c[j-1];
		if(a>=49&&a<=57){m[i][j]=a-48;}
		if(c[j-1]=='X'){m[i][j]=-1;}
		if(c[j-1]=='S'){m[i][j]=99;
		g.first=i;
		g.second=j;
		q.push(g);
		l.first=0;
		l.second=1;
		e.push(l);
		}
	}
}
while(!q.empty()){
	g=q.front();
	q.pop();
	l=e.front();
	e.pop();
	if(m[g.first][g.second]==l.second){
		l.second++;
		if(l.second==n+1){
			break;
		}
		while(!q.empty()){
			q.pop();
			e.pop();
		}
	}
	p[g.first][g.second]=l.second;
l.first++;
	for(i=0;i<2;i++){
		if(m[g.first+b[i]][g.second]!=-1 && p[g.first+b[i]][g.second]!=l.second){
			g.first+=b[i];
			q.push(g);
			e.push(l);
			g.first-=b[i];
		}
		if(m[g.first][g.second+b[i]]!=-1 && p[g.first][g.second+b[i]]!=l.second){
			g.second+=b[i];
			q.push(g);
			e.push(l);
			g.second-=b[i];
		}
	}
}
cout<<l.first<<endl;
return 0;
}

C++: #include <cstdio>
#include <cstdlib>
#include <cmath>
#include <iostream>
#include <sstream>
#include <vector>
#include <string>
#include <queue>
#include <set>
#include <map>
#include <utility>
#include <algorithm>
#include <functional>

using namespace std;

#define rep(i,n) for((i)=0;(i)<(int)(n);(i)++)
#define foreach(itr,c) for(__typeof((c).begin()) itr=(c).begin();itr!=(c).end();itr++)

int N,H,W;
char field[1010][1010];
bool usd[1010][1010];
const int dx[4] = {0,1,0,-1}, dy[4] = {1,0,-1,0};

struct state {
  int y,x,dis;

  state(int a, int b, int c){
    y = a;
    x = b;
    dis = c;
  }
};

state bfs(state arg, int obj){
  int i,j;
  queue<state> q;

  rep(i,H) rep(j,W) usd[i][j] = false;

  q.push(arg);
  while(!q.empty()){
    state t = q.front(); q.pop();

    if(field[t.y][t.x] == (char)(obj + '0')) return t;
    rep(i,4){
      int my = t.y + dy[i];
      int mx = t.x + dx[i];
      if(my < 0 || mx < 0 || H <= my || W <= mx) continue;
      if(field[my][mx] != 'X' && !usd[my][mx]){
        q.push(state(my,mx,t.dis+1));
        usd[my][mx] = true;
      }
    }
  }
}

int main(){
  int i,j;

  scanf("%d %d %d",&H,&W,&N);
  rep(i,H) scanf("%s",&field[i]);

  int res = 0;
  state tmp(0,0,0);
  rep(i,H) rep(j,W) if(field[i][j] == 'S'){ tmp.y = i; tmp.x = j; }// cout << field[tmp.y][tmp.x] << endl;
  for(i = 1; i <= N; i++) tmp = bfs(tmp,i);

  printf("%d\n",tmp.dis);

  return 0;
}