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 <iostream>
# include <algorithm>
#include <array>
# include <cassert>
#include <cctype>
#include <climits>
#include <numeric>
# include <vector>
# include <string>
# include <set>
# include <map>
# include <cmath>
# include <iomanip>
# include <functional>
# include <tuple>
# include <utility>
# include <stack>
# include <queue>
# include <list>
# include <bitset>
# include <complex>
# include <chrono>
# include <random>
# include <limits.h>
# include <unordered_map>
# include <unordered_set>
# include <deque>
# include <cstdio>
# include <cstring>
#include <stdio.h>
#include <stdlib.h>
#include <cstdint>
#include <cfenv>
//#include <bits/stdc++.h>
using namespace std;
using LL = long long;
using ULL = unsigned long long;
constexpr long long MOD = 1000000000 + 7;
constexpr long long INF = std::numeric_limits<long long>::max();
const double PI = acos(-1);
#define fir first
#define sec second
#define thi third
#define debug(x) cerr<<#x<<": "<<x<<'\n'
typedef pair<LL, LL> Pll;
typedef pair<LL, pair<LL, LL>> Ppll;
typedef pair<LL, pair<LL, bitset<100001>>> Pbll;
typedef pair<LL, pair<LL, vector<LL>>> Pvll;
typedef pair<LL, LL> Vec2;
struct Tll { LL first, second, third; };
typedef pair<LL, Tll> Ptll;
#define rep(i,rept) for(LL i=0;i<rept;i++)
#define Mfor(i,mf) for(LL i=mf-1;i>=0;i--)
LL h, w, n, m, k, s, t, q, sum, last, cnt, ans;
Pll a[110];
LL d[1010][1010];
int dd[4][2] = { {1,0},{0,1},{ -1,0 },{ 0,-1 } };
string str,ss;
struct Edge { LL cost, from, to; };
vector<Edge>vec;
bool f;
char c[10000][10000];
void YN(bool f) {
	if (f)
		cout << "YES" << endl;
	else
		cout << "NO" << endl;
}
void yn(bool f) {
	if (f)
		cout << "Yes" << endl;
	else
		cout << "No" << endl;
}
LL bfs(Pll s,Pll t) {
	queue<Ppll>qu;
	d[s.fir][s.sec] = 0;
	qu.push(Ppll(d[s.first][s.second], s));
	while (!qu.empty()) {
		Ppll pp = qu.front();
		Pll pos = pp.second;
		qu.pop();
		if (pos == t)break;
		if (pp.fir > d[pos.fir][pos.sec])continue;
		rep(i, 4) {
			LL X = pos.first + dd[i][0], Y = pos.second + dd[i][1];
			if (X < h&&Y<w&&X>=0&&Y>=0&&d[X][Y]>d[pos.fir][pos.sec] + 1&&c[X][Y]== '.') {
				d[X][Y] = d[pos.first][pos.second] + 1;
				qu.push(Ppll(d[X][Y], Pll(X, Y)));
			}
		}
	}
	return d[t.fir][t.sec];
}
int main() {
	cin >> h >> w >> n;
	rep(i, h)rep(j, w) {
		cin >> c[i][j];
		if (c[i][j] == 'S' || (c[i][j] > '0'&&c[i][j] <= '9')) {
			if (c[i][j] == 'S')
				a[0] = Pll(i, j);
			if (c[i][j] > '0'&&c[i][j] <= '9')
				a[c[i][j] - '0'] = Pll(i, j);
			c[i][j] = '.';
		}
	}
	rep(i, n) {
		rep(j, 1010)rep(k, 1010)d[j][k] = 30000;
		ans += bfs(a[i],a[i+1]);
	}
	cout << ans << endl;
	return 0;
}

C++: #include <cstdio>
#include <queue>
#include <map>
#include <cctype>
#include <climits>

using namespace std;

typedef pair<int, int> P;

#define W 1005
#define H 1005
#define N 15

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

char a[H][W];
int d[H][W];
P fa[N];
int h, w, n;

int bfs(int sx, int sy, int gx, int gy){
	for (int i = 0; i < h; i++){
		for (int j = 0; j < w; j++){
			d[i][j] = INT_MAX;
		}
	}
	queue<P> que;
	d[sx][sy] = 0;
	que.push(P(sx, sy));
	
	while(!que.empty()){
		P p = que.front();
		que.pop();
		int x = p.first;
		int y = p.second;

		if (x == gx && y == gy) break;
	
		for (int i = 0; i < 4; i++){
			int nx = x + dx[i];
			int ny = y + dy[i];
			
			if (nx >= 0 && nx < h && ny >= 0 && ny < w && a[nx][ny] != 'X' && d[nx][ny] == INT_MAX){
				d[nx][ny] = d[x][y] + 1;
				que.push(P(nx, ny));
			}
		}
	}
	return d[gx][gy];
}

int main(){
	scanf("%d %d %d", &h, &w, &n);
	for (int i = 0; i < h; i++){
		getchar();
		for (int j = 0; j < w; j++){
			scanf("%c", &a[i][j]);
			if (a[i][j] == 'S'){
				fa[0] = P(i, j);
			}
			if (isdigit(a[i][j])){
				fa[a[i][j] - '0'] = P(i, j);
			}
		}
	}
	int sum = 0;
	for (int i = 0; i < n; i++){
		sum += bfs(fa[i].first, fa[i].second, fa[i + 1].first, fa[i + 1].second);
	}
	printf("%d\n", sum);
	return 0;
}