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++: 
//include
//------------------------------------------
#include <vector>
#include <list>
#include <map>
#include <set>
#include <deque>
#include <queue>
#include <stack>
#include <bitset>
#include <algorithm>
#include <functional>
#include <numeric>
#include <utility>
#include <sstream>
#include <iostream>
#include <iomanip>
#include <cstdio>
#include <cmath>
#include <cstdlib>
#include <cctype>
#include <string>
#include <cstring>
#include <ctime>

using namespace std;

//conversion
//------------------------------------------
inline int toInt(string s) {int v; istringstream sin(s);sin>>v;return v;}
template<class T> inline string toString(T x) {ostringstream sout;sout<<x;return sout.str();}

//math
//-------------------------------------------
template<class T> inline T sqr(T x) {return x*x;}

//typedef
//------------------------------------------
typedef vector<int> VI;
typedef vector<VI> VVI;
typedef vector<string> VS;
typedef pair<int, int> PII;
typedef long long LL;

//container util
//------------------------------------------
#define ALL(a)  (a).begin(),(a).end()
#define RALL(a) (a).rbegin(), (a).rend()
#define PB push_back
#define MP make_pair
#define SZ(a) int((a).size())
#define EACH(i,c) for(typeof((c).begin()) i=(c).begin(); i!=(c).end(); ++i)
#define EXIST(s,e) ((s).find(e)!=(s).end())
#define SORT(c) sort((c).begin(),(c).end())

//repetition
//------------------------------------------
#define FOR(i,a,b) for(int i=(a);i<(b);++i)
#define REP(i,n)  FOR(i,0,n)

//constant
//--------------------------------------------
const double EPS = 1e-10;
const double PI  = acos(-1.0);
const int DX[] = {0, 1, 0, -1};
const int DY[] = {-1, 0, 1, 0};


//clear memory
#define CLR(a) memset((a), 0 ,sizeof(a))

//debug
#define dump(x)  cerr << #x << " = " << (x) << endl;
#define debug(x) cerr << #x << " = " << (x) << " (L" << __LINE__ << ")" << " " << __FILE__ << endl;

int main(int argc, char const *argv[])
{
	cin.tie(0);
	ios::sync_with_stdio(false);

	int h, w, n;
	cin >> h >> w >> n;
	VVI table(h, VI(w + 1));
	queue<PII> q;
	vector<PII> goals(n);
	PII start;
	REP(i, h){
		string value_line;
		cin >> value_line;
		REP(j, w){
			char value_str = value_line[j];
			int value = 0;
			if(value_str == 'S'){
				start = MP(i, j);
			}else if('1' <= value_str && value_str <= '9'){
				goals[value_str - '0' - 1] = MP(i, j);
			}else if(value_str == 'X'){
				value = -1;
			}
			table[i][j] = value;
		}
	}

	q.push(start);
	int cnt = 0;
	for(auto it = goals.begin() ; it != goals.end(); it++){
		while(!q.empty()){
			PII p = q.front();
			q.pop();
			int y = p.first, x = p.second;
			REP(i, 4){
				int dx = x + DX[i];
				int dy = y + DY[i];
				int cv = table[y][x];
				if(dy >= 0 && dy < h && dx >= 0 && dx < w
					&& (table[dy][dx] > cv + 1 || table[dy][dx] == 0)){
					table[dy][dx] = cv + 1;
					q.push(MP(dy, dx));
				}
			}
		}

		PII goal = *it;
		int gx = goal.second, gy = goal.first;
		cnt += table[gy][gx];

		REP(i, h)
			REP(j, w)
				table[i][j] = table[i][j] >= 0 ? 0 : -1;
		q.push(goal);
	}

	cout << cnt << endl;
	
	return 0;
}

C++: #include<iostream>
#include<queue>
using namespace std;

typedef pair<int,int> P;

const int MAX = 1000, INF = 100000000;
char map[MAX][MAX+1];
int H, W, N;
int sx, sy;
int gx, gy;
int steps;
int d[MAX][MAX];

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

int bfs(){
	for(int i = 0; i < H; i++)
		for(int j = 0; j < W; j++) d[i][j] = INF;
	
	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 + dx[i], ny = p.second + dy[i];
			if(nx >= 0 && nx < H && ny >= 0 && ny < W && map[nx][ny] != 'X' 
			&& d[nx][ny] == INF){
				que.push(P(nx,ny));
				d[nx][ny] = d[p.first][p.second] + 1;
			}
		}
	}
		
	return d[gx][gy];
}

int main(){
	cin >> H >> W >> N;
	for(int i = 0; i < H; i++)
		cin >> map[i];
	
	steps = 0;
	int n = 0;
	while(n < N){
		for(int i = 0; i < H; i++)
			for(int j = 0; j < W; j++){
				if(map[i][j] == 'S' || map[i][j] == n + '0'){
					sx = i; sy = j;
				}
				if(map[i][j] == n + 1 + '0'){
					gx = i; gy = j;
				}
			}
		
		steps += bfs();
		n++;
	}
	cout << steps;
	return 0;
}

C++: #include<iostream>
#include<queue>
using namespace std;
#define Max 10000
char field[Max][Max];
const int INF=10000000;
typedef pair<int,int> P;
int N,M,sx,sy,gx,gy,H;
int d[Max][Max];
int dx[4]={1,0,-1,0};
int dy[4]={0,1,0,-1};
int goal[Max]{};
int bfs()
{
	int anw=0;
	deque<P>Q;
	for(int c=0;c<H;c++){
		for(int i=0;i<N;i++)
		{
			for(int j=0;j<M;j++)
			{
				d[i][j]=INF;
			}
		}
	Q.push_back(P(sx,sy));
	d[sx][sy]=0;
	while(Q.size()){
		int x=Q.front().first;
		int y=Q.front().second;
		Q.pop_front();
		if((int)field[x][y]==49+c){
			goal[c]=d[x][y];
			sx=x;
			sy=y;
			break;
		}
		
		for(int i=0;i<4;i++){
			int nx=x+dx[i];
			int ny=y+dy[i];
			if(0<=nx&&nx<N&&0<=ny&&ny<M&&field[nx][ny]!='X'&&d[nx][ny]==INF){
				Q.push_back(P(nx,ny));
				d[nx][ny]=d[x][y]+1;
			}
		}
		
	}
	Q.clear();
	anw=anw+goal[c];
	}
	return anw;
}
int main()
{
	cin>>N>>M>>H;
	for(int i=0;i<N;i++)
		for(int j=0;j<M;j++){
			cin>>field[i][j];
			if(field[i][j]=='S')
				sx=i; sy=j;
				//field[sx][sy]='0';
		}
	printf("%d\n",bfs());
	return 0;
}