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 LOCAL 
#include <iostream>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <string>
#include <algorithm>
#include <vector>
#include <list>
#include <stack>
#include <queue>
#include <set>
#include <map>
#include <cmath>
#include <numeric>
#include <ctype.h>
#include <sstream>
#include <iomanip>
#include <fstream>   
#include <cassert>
#include <climits>

#define min(a,b) ((a)<(b) ? (a):(b))
#define max(a,b) ((a)>(b) ? (a):(b))
#define abs(a) ((a)>0 ? (a):-(a)) 

#define pb push_back
#define mp make_pair
#define st first
#define nd second

//#define ALL(x) x.begin(), x.end() 
//#define INS(x) inserter(x, x.begin())
#define REP(i,n) for(int i = 0; i < (n); i++)
#define MST(a,b) memset(a,b,sizeof(a))

const int INF = 0x3f3f3f3f;
const int N = 1010;
using namespace std;
typedef long long LL;

#define mp make_pair
#define st first
#define nd second
typedef pair<int, int> PII;

int h, w, n;
char g[N][N]; 

void read() {
	scanf("%d%d%d", &h, &w, &n);
	MST(g, 'X');
	for (int i = 1; i <= h; i++) {
		getchar();
		for (int j = 1; j <= w; j++) 
			g[i][j] = getchar();
	}
//	for (int i = 1; i <= h; i++) {
//		for (int j = 1; j <= w; j++) 
//			cout << g[i][j];
//		cout << endl;
//	}
}

int setp(int x, int y) {
	return (x-1)*w+y-1;
}

void getp(int xy, int& x, int& y) {
	x = xy/w+1;
	y = xy%w+1;
}

int pos[10], d[N][N];
int dx[] = {0, 1, 0, -1};
int dy[] = {1, 0, -1, 0};
int bfs(int s) {
	MST(d, INF);
	queue<int> q;
	int sx, sy;
	getp(pos[s], sx, sy);
	q.push(pos[s]);
	d[sx][sy] = 0;
	while (q.size()) {
		int t = q.front(); q.pop();
		int tx, ty;
		getp(t, tx, ty);
//		cout << tx << " " << ty << endl;
		if (t == pos[s+1]) return d[tx][ty];
		
		for (int k = 0; k < 4; k++) {
			int txx = tx+dx[k], tyy = ty+dy[k];
			if (g[txx][tyy] == 'X') continue;
			int tt = setp(txx, tyy); 
			if (d[txx][tyy] == INF) {
				d[txx][tyy] = d[tx][ty] + 1;
				q.push(tt);
//				cout << "\t" << txx << " " << tyy << endl;
			}
		}
	}
}

int solve() {
	for (int i = 1; i <= h; i++)
		for (int j = 1; j <= w; j++) {
			if (g[i][j] == 'S') pos[0] = setp(i, j);
			if (isdigit(g[i][j])) pos[g[i][j]-'0'] = setp(i, j);
		}
	
//	for (int i = 0; i <= n; i++) {
//		int x, y;
//		getp(pos[i], x, y);
//		cout << pos[i] << " " << x << " " << y << endl;
//	}
	
	int ans = 0;
	for (int i = 0; i < n; i++)
		ans += bfs(i);
	return ans;
}

int main() {
	#ifdef LOCAL
		freopen("in.txt", "r", stdin);
		freopen("out.txt", "w", stdout);
	#endif
	read();
	cout << solve() << endl;
	return 0;
}

C++: #include <bits/stdc++.h>
using namespace std;
int main() {
    int Y, X, n;
    int x[10];
    int y[10];
    int ans = 0;
    cin >> Y >> X >> n;
    string a[Y];
    for(int i = 0; i < Y; i++) {
        cin >> a[i];
    }
    int step[Y + 2][X + 2];

    for(int i = 0; i < X + 2; i++) {
        step[0][i] = -2;
    }
    for(int i = 1; i <= Y; i++) {
        step[i][0] = -2;
        for(int j = 1; j <= X; j++) {
            step[i][j] = -1;
            if(a[i - 1][j - 1] == 'S') {
                x[0] = j;
                y[0] = i;
                step[i][j] = 0;
            }
            if(a[i - 1][j - 1] == '.') {
                step[i][j] = -1;
            }
            if(a[i - 1][j - 1] == 'X') {
                step[i][j] = -2;
            }
            if(a[i - 1][j - 1] == '1') {
                x[1] = j;
                y[1] = i;
            }
            if(a[i - 1][j - 1] == '2') {
                x[2] = j;
                y[2] = i;
            }
            if(a[i - 1][j - 1] == '3') {
                x[3] = j;
                y[3] = i;
            }
            if(a[i - 1][j - 1] == '4') {
                x[4] = j;
                y[4] = i;
            }
            if(a[i - 1][j - 1] == '5') {
                x[5] = j;
                y[5] = i;
            }
            if(a[i - 1][j - 1] == '6') {
                x[6] = j;
                y[6] = i;
            }
            if(a[i - 1][j - 1] == '7') {
                x[7] = j;
                y[7] = i;
            }
            if(a[i - 1][j - 1] == '8') {
                x[8] = j;
                y[8] = i;
            }
            if(a[i - 1][j - 1] == '9') {
                x[9] = j;
                y[9] = i;
            }
        }
        step[i][X + 1] = -2;
    }
    for(int i = 0; i < X + 2; i++) {
        step[Y + 1][i] = -2;
    }

    queue<pair<int, int> > q;

    q.push(make_pair(y[0], x[0]));

    for(int i = 0; i < n; i++) {
        while(q.empty() == false) {
            if(q.front().first == y[i + 1] && q.front().second == x[i + 1]) {
                ans += step[y[i + 1]][x[i + 1]];
                for(int j = 1; j <= Y; j++) {
                    for(int k = 1; k <= X; k++) {
                        if(step[j][k] != -2)
                            step[j][k] = -1;
                    }
                }
                step[y[i + 1]][x[i + 1]] = 0;
                while(!q.empty()) {
                    q.pop();
                }
                q.push(make_pair(y[i + 1], x[i + 1]));
                break;
            }
            if(step[q.front().first + 1][q.front().second] == -1) {
                step[q.front().first + 1][q.front().second] =
                    step[q.front().first][q.front().second] + 1;
                q.push(make_pair(q.front().first + 1, q.front().second));
            }
            if(step[q.front().first - 1][q.front().second] == -1) {
                step[q.front().first - 1][q.front().second] =
                    step[q.front().first][q.front().second] + 1;
                q.push(make_pair(q.front().first - 1, q.front().second));
            }
            if(step[q.front().first][q.front().second + 1] == -1) {
                step[q.front().first][q.front().second + 1] =
                    step[q.front().first][q.front().second] + 1;
                q.push(make_pair(q.front().first, q.front().second + 1));
            }
            if(step[q.front().first][q.front().second - 1] == -1) {
                step[q.front().first][q.front().second - 1] =
                    step[q.front().first][q.front().second] + 1;
                q.push(make_pair(q.front().first, q.front().second - 1));
            }
            // for(int j = 1; j <= Y; j++) {
            //     for(int k = 1; k <= X; k++) {
            //         if(step[j][k] > -1)
            //             cout << " ";
            //         cout << step[j][k] << " ";
            //     }
            //     cout << endl;
            // }
            // cout << endl;
            q.pop();
        }
    }
    cout << ans << endl;
    return 0;
}