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++: from : http://judge.u-aizu.ac.jp/onlinejudge/review.jsp?rid=1811250#1

#include<iostream>
#include<vector>
#include<queue>
 
using namespace std;

#define DEBUG(x) cout<<(#x)<<": "<<(x)<<endl
 
int nowS = 0;
int x, y, n;
int mp[1000][1000] = {};
int k[4] = {1, -1, 0, 0};
int h[4] = {0, 0, 1, -1};
int cx[9] = {};
int cy[9] = {};
queue<int> bufX, bufY, bufD;
 
bool search(int xx, int yy, int dd) {
    bufX.pop();
    bufY.pop();
    bufD.pop();
 
    for (int i = 0; i < 4; i++) {
        int nx = xx+k[i];
        int ny = yy+h[i];
        if (nx < 0 || nx >= x || ny < 0 || ny >= y)//?????????
            continue;
        if (mp[ny][nx] == -1) {//?????
            continue;
        }
        if (cx[nowS] == nx && cy[nowS] == ny) {//??????
            nowS++;
            while (!bufX.empty()) {
                bufX.pop();
                bufY.pop();
                bufD.pop();
            }
            if (nowS == n) {//?????????????????????
                cout << dd + 1 << endl;
                return true;
            }
            else {//
                bufX.push(nx);
                bufY.push(ny);
                bufD.push(dd + 1);
                mp[ny][nx] = nowS + 1;
            }
            return false;
        }
        if(mp[ny][nx] >= 0 && mp[ny][nx] < nowS + 1){
            bufX.push(nx);
            bufY.push(ny);
            bufD.push(dd + 1);
            mp[ny][nx] = nowS + 1;
        }
    }
    return false;
}
 
int main() {
    int ch = 0;
    cin >> y >> x >> n;
    for (int i = 0; i < y; i++) {//??\???
        char c[1001];
        cin >> c;
        for (int j = 0; j < x; j++) {
            if(c[j] == 'X')
                mp[i][j] = -1;
            else if(c[j] == 'S'){
                bufX.push(j);
                bufY.push(i);
                bufD.push(0);
                mp[i][j] = 1;
            }   
            else {
                mp[i][j] = 0;
            }
            if (c[j] >= 48 && c[j] <= 57) {
                int m;
                m = c[j] - 49;
                cx[m] = j;
                cy[m] = i;      
                ch++;
            }
        }
    }
     
 
    while(!search(bufX.front(), bufY.front(), bufD.front())){}//???????????????§?????????
 
    return 0;
}

C++: #include <algorithm>
#include <complex>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <cctype>
#include <cmath>
#include <cassert>
#include <iostream>
#include <map>
#include <queue>
#include <sstream>
#include <string>
#include <stack>
#include <set>
#include <vector>

using namespace std;

#define all(c) ((c).begin()), ((c).end())
#define dump(c) cerr << "> " << #c << " = " << (c) << endl;
#define iter(c) __typeof((c).begin())
#define tr(i, c) for (iter(c) i = (c).begin(); i != (c).end(); i++)
#define rep(i, n) for (int i = 0; i < (int)(n); i++)
#define repi(i, a, b) for (int i = (int)(a); i <= (int)(b); i++)

#define mp make_pair
#define fst first
#define snd second
#define pb push_back

typedef unsigned int uint;
typedef long long ll;
typedef unsigned long long ull;
typedef vector<int> vi;
typedef vector<vi> vvi;
typedef vector<double> vd;
typedef vector<vd> vvd;
typedef vector<string> vs;
typedef pair<int,int> pii;

const int INF = 1 << 29;
const double EPS = 1e-10;

template<typename T1,typename T2>
ostream& operator<<(ostream& os,const pair<T1,T2>& p){
    return os<<'('<<p.first<<','<<p.second<<')';
}
template<typename T>
ostream& operator<<(ostream& os,const vector<T>& a){
    os<<'[';
    rep(i,a.size()) os<<(i?" ":"")<<a[i];
    return os<<']';
}

typedef pair<int, pii> State;

int H, W, N;
char m[1010][1010];

int bfs(pii s, int dest, pii &ns) {
	queue<pair<int, pii> > q;
	q.push(mp(0, s));
	map<pii, bool> visited;
	
	while(!q.empty()) {
		pair<int, pii> p = q.front();
		q.pop();
		if (visited[p.snd]) continue;
		visited[p.snd] = true;
		
		if (m[p.snd.snd][p.snd.fst] == dest + '0') {
			ns = p.snd;
			return p.fst;
		}
		
		static const int dx[] = {1, 0, -1, 0};
		static const int dy[] = {0, 1, 0, -1};
		rep(d, 4) {
			int tx = p.snd.fst + dx[d];
			int ty = p.snd.snd + dy[d];
			if (tx < 0 || tx >= W || ty < 0 || ty >= H) continue;
			if (m[ty][tx] == 'X') continue;
			q.push(mp(p.fst + 1, mp(tx, ty)));
		}
	}
}

int main() {
	cin >> H >> W >> N;
	pii s;
	rep(i, H) rep(j, W) {
		cin >> m[i][j];
		if (m[i][j] == 'S') {
			s.fst = j;
			s.snd = i;
		}
	}
	
	int ans = 0;
	rep(i, N) ans += bfs(s, i + 1, s);
	
	cout << ans << endl;
    
    return 0;
}