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 <queue>
#define min(x,y) ((x)<(y)?(x):(y))
#define INFI 65535

using namespace std;
typedef pair<int, int> P;

int Strength = 1;
char Maps[1010][1010];
int dist[1010][1010];
int H, W, N;
int hor[] = {1,0,-1,0};
int ver[] = {0,-1,0,1};
int ans;


void clear();
void show();
P bfs(int cheese,P n);

int main(void) {
    scanf("%d %d %d", &H, &W, &N);
    P s,e;
    for (int i = 0; i<H; i++) {
        getchar();
        for (int j = 0; j<W; j++) {
            Maps[i][j] = getchar();
            if (Maps[i][j]=='S') s.first = i, s.second = j;
        }
    }

    Maps[s.first][s.second] = '.';
    
    for (int i = 1; i<=N; i++) {
        clear();
        s = bfs(i, s);
    }
    
    printf("%d\n", ans);
    return 0;
}

P bfs(int cheese,P n) {
    queue<P> que;
    que.push(n);
    dist[n.first][n.second] = 0;
    
    P top;
    while (que.size()) {
        top = que.front();
        if (Maps[top.first][top.second]-'0'==cheese) {
            ans += dist[top.first][top.second];
            break;
        }
        
        que.pop();
        for (int i = 0; i<4; i++) {
            int nv = top.first+ver[i], nh = top.second+hor[i];
            if (nv>=0 && nv<H && nh>=0 && nh<W && dist[nv][nh]==INFI && Maps[nv][nh]!='X') {
                que.push(P(nv,nh));
                dist[nv][nh] = min(dist[nv][nh], dist[top.first][top.second]+1);
            }
        }
    }
    return top;
}

void clear() {
    for (int i = 0; i<H; i++)
        for (int j = 0; j<W; j++)
            dist[i][j] = INFI;
}

C++: #include<cstdio>
#include <iostream>
#include<algorithm>
#include<string>
#include<queue>
#include<vector>
#include<functional>
#include<cmath>
#include<map>
#include<stack>
#include<list>
#include<set>
#include<numeric>

#define MOD 1000000007

using namespace std;
typedef long long ll;
typedef pair<int, int> P;

int H, W, N;
int d[1010][1010];
char field[1010][1010];

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

int bfs(char s, char g) {
	queue<P> que;
	fill(d[0], d[100], INF);
	for (int i = 0; i < H; i++) {
		for (int j = 0; j < W; j++) {
			if (field[i][j] == s) {
				que.push(P(i, j));
				d[i][j] = 0;
			}
		}
	}

	while (!que.empty()) {
		P p = que.front(); que.pop();
		if (field[p.first][p.second] == g)return d[p.first][p.second];

		for (int i = 0; i < 4; i++) {
			int ny = p.first + dy[i], nx = p.second + dx[i];
			if (0 <= nx && nx < W && 0 <= ny && ny < H && field[ny][nx] != 'X' &&
				d[ny][nx] == INF) {
				que.push(P(ny, nx));
				d[ny][nx] = d[p.first][p.second] + 1;
			}
		}
	}
	return -1;
}
int main()
{
	cin >> H >> W >> N;
	for (int i = 0; i < H; i++) {
		for (int j = 0; j < W; j++) {
			cin >> field[i][j];
		}
	}
	int ans = bfs('S','1');
	for (int i = 1; i < N; i++)
		ans += bfs(i + '0', i + 1 + '0');
	cout << ans << endl;
	return 0;
}

C++: #include<iostream>
#include<string>
#include<sstream>
#include<vector>
#include<queue>
using namespace std;
int d[1001][1001]={0};
bool visited[1001][1001] = {false};
int main()
{
	int H,W,N,sy,sx,gy,gx,out=0;
	queue<int> qx;
	queue<int> qy;

	cin >> H >> W >> N;
	vector<string> zahyou(H,"");
	for(int i=0;i<H;i++)
	{
		cin >> zahyou[i];
	}
	for(int n=0;n<N;n++)
	{	
		for(int i=0;i<H;i++)
		{
			for(int j=0;j<W;j++)
			{
				if(n==0)
				{
					if(zahyou[i][j]=='S') {sy=i;sx=j;}
					if(zahyou[i][j]=='1') {gy=i;gx=j;}
				}
				else
				{	
					if(zahyou[i][j]==n+'0') {sy=i;sx=j;}
					if(zahyou[i][j]==n+'1') {gy=i;gx=j;}
				}
			}
		}
		qx.push(sx);
		qy.push(sy);

		int dx[4]={-1,0,1,0};
		int dy[4]={0,1,0,-1};
		
		
		while(!qx.empty()&&!qy.empty())
		{
			int x = qx.front();qx.pop();
			int y = qy.front();qy.pop();
			
			for(int k=0;k<4;k++)
			{
				int nx = dx[k];
				int ny = dy[k];

				if(visited[ny][nx]==false&&zahyou[ny][nx]!='X')
				{
					visited[ny][nx]=true;
					d[ny][nx]=d[y][x]+1;
					qx.push(nx);
					qy.push(ny);
				}
			}
		}
		out += d[gy][gx];
	}
	cout << out <<endl;
}