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 <vector>
#include <set>
using namespace std;
int H, W, N;

char world[1002][1002];

pair<int, int> S;


vector<pair<int, int> > getNeighbors(pair<int, int> xs){
    vector<pair<int, int> > v;
    int dx[4] = {1, 0, -1, 0};
    int dy[4] = {0, 1, 0, -1};
    for(int i = 0; i < 4; i++){
        int nx = dx[i] + xs.first;
        int ny = dy[i] + xs.second;
        if (world[nx][ny] != 'X') {
            v.push_back(pair<int, int>(nx, ny));
        }
    }
    return v;
}

bool isAbnormal(vector<vector<int> >& trails, const pair<int, int>& xs){
    if (trails[xs.first][xs.second]){
        return false;
    }else{
        trails[xs.first][xs.second] = 1;
        return true;
    }
}

pair<int, pair<int, int> > solve(char goal, const vector<pair<int, int> >& v, int depth){
    vector<pair<int, int> > v2;
    vector<vector<int> > trails = vector<vector<int> >(H + 2, vector<int>(W + 2, 0));
    
    for(auto target: v){
        vector<pair<int, int> > neighbors = getNeighbors(target);
        for(auto x: neighbors){
            if (world[x.first][x.second] == goal) {
                return pair<int, pair<int, int> >(depth + 1, x);
            }
            if (isAbnormal(trails, x)){
                v2.push_back(x);
            }
        }
    }
    return solve(goal, v2, depth + 1);
}

void input(){
    cin >> H >> W >> N;
    for(int i = 1; i < H + 1; i++){
        for (int j = 1; j < W + 1; j++) {
            cin >> world[i][j];
            if (world[i][j] == 'S') {
                S = pair<int, int>(i, j);
            }
        }
    }
}

void init(){
    for (int j = 0; j < W + 2; j++) {
        world[0][j] = 'X';
        world[H + 1][j] = 'X';
    }
    for (int i = 0; i < H + 2; i++) {
        world[i][0] = 'X';
        world[i][W + 1] = 'X';
    }
}

int main (int argc, char* argv[]){
    input();

    init();
    
    int sum = 0;
    for (char g = '1'; g < '1' + N; g++){
        pair<int, pair<int, int> > res = solve(g, vector<pair<int, int> >(1, S), 0);
        sum += res.first;
        S = res.second;
    }

    cout << sum << endl;

    return 0;
}