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<map>
#include<queue>
#include<string>
using namespace std;
int main(){
  int h,w,n,px,py,ans;
  int field[1000][1000];
  int ch[1000][1000];
  while(cin >>h>>w>>n){
    ans = 0;
    string s;
    for(int i=0; i<h; i++){
      cin >>s;
      for(int j=0; j<w; j++){
	if(s[j] == 'S'){px = j;py = i;field[j][i] = 0;}
	else if(s[j]>='1' && s[j]<='9'){field[j][i] = s[j]-'0';}
	else if(s[j] == 'X'){field[j][i] = -1;}
	else{field[j][i] = 0;}
      }
    }
    for(int i=0; i<n; i++){
      queue<pair<int,int> > play;
      play.push(make_pair(px,py));
      for(int j=0; j<h; j++){
	for(int k=0; k<w; k++){
	  ch[k][j] = 0;
	}
      }
      ch[px][py] = 1;
      for(;;){
	int x = play.front().first;
	int y = play.front().second;
	if(field[x][y] == i+1){ans+=ch[x][y]-1; px = x; py = y;break;}
	if(x>0){if(field[x-1][y] != -1){if(ch[x-1][y] == 0){play.push(make_pair(x-1,y)); ch[x-1][y] = ch[x][y]+1;}}}
	if(y>0){if(field[x][y-1] != -1){if(ch[x][y-1] == 0){play.push(make_pair(x,y-1)); ch[x][y-1] = ch[x][y]+1;}}}
	if(x<w-1){if(field[x+1][y] != -1){if(ch[x+1][y] == 0){play.push(make_pair(x+1,y)); ch[x+1][y] = ch[x][y]+1;}}}
	if(y<h-1){if(field[x][y+1] != -1){if(ch[x][y+1] == 0){play.push(make_pair(x,y+1)); ch[x][y+1] = ch[x][y]+1;}}}
	play.pop();
      }
    }
    cout <<ans<<endl;
  }
  return 0;
}

C++: 
//include
//------------------------------------------
#include <vector>
#include <list>
#include <map>
#include <set>
#include <deque>
#include <queue>
#include <stack>
#include <bitset>
#include <algorithm>
#include <functional>
#include <numeric>
#include <utility>
#include <sstream>
#include <iostream>
#include <iomanip>
#include <cstdio>
#include <cmath>
#include <cstdlib>
#include <cctype>
#include <string>
#include <cstring>
#include <ctime>

using namespace std;

//conversion
//------------------------------------------
inline int toInt(string s) {int v; istringstream sin(s);sin>>v;return v;}
template<class T> inline string toString(T x) {ostringstream sout;sout<<x;return sout.str();}

//math
//-------------------------------------------
template<class T> inline T sqr(T x) {return x*x;}

//typedef
//------------------------------------------
typedef vector<int> VI;
typedef vector<VI> VVI;
typedef vector<string> VS;
typedef pair<int, int> PII;
typedef long long LL;

//container util
//------------------------------------------
#define ALL(a)  (a).begin(),(a).end()
#define RALL(a) (a).rbegin(), (a).rend()
#define PB push_back
#define MP make_pair
#define SZ(a) int((a).size())
#define EACH(i,c) for(typeof((c).begin()) i=(c).begin(); i!=(c).end(); ++i)
#define EXIST(s,e) ((s).find(e)!=(s).end())
#define SORT(c) sort((c).begin(),(c).end())

//repetition
//------------------------------------------
#define FOR(i,a,b) for(int i=(a);i<(b);++i)
#define REP(i,n)  FOR(i,0,n)

//constant
//--------------------------------------------
const double EPS = 1e-10;
const double PI  = acos(-1.0);
const int DX[] = {0, 1, 0, -1};
const int DY[] = {-1, 0, 1, 0};


//clear memory
#define CLR(a) memset((a), 0 ,sizeof(a))

//debug
#define dump(x)  cerr << #x << " = " << (x) << endl;
#define debug(x) cerr << #x << " = " << (x) << " (L" << __LINE__ << ")" << " " << __FILE__ << endl;

int main(int argc, char const *argv[])
{
	cin.tie(0);
	ios::sync_with_stdio(false);

	int h, w, n;
	cin >> h >> w >> n;
	VVI table(h, VI(w + 1));
	queue<PII> q;
	vector<PII> goals(n);
	PII start;
	REP(i, h){
		string value_line;
		cin >> value_line;
		REP(j, w){
			char value_str = value_line[j];
			int value = 0;
			if(value_str == 'S'){
				start = MP(i, j);
			}else if('1' <= value_str && value_str <= '9'){
				goals[value_str - '0' - 1] = MP(i, j);
			}else if(value_str == 'X'){
				value = -1;
			}
			table[i][j] = value;
		}
	}

	q.push(start);
	int cnt = 0;
	for(auto it = goals.begin() ; it != goals.end(); it++){
		while(!q.empty()){
			PII p = q.front();
			q.pop();
			int y = p.first, x = p.second;
			REP(i, 4){
				int dx = x + DX[i];
				int dy = y + DY[i];
				int cv = table[y][x];
				if(dy >= 0 && dy < h && dx >= 0 && dx < w
					&& (table[dy][dx] > cv + 1 || table[dy][dx] == 0)){
					table[dy][dx] = cv + 1;
					q.push(MP(dy, dx));
				}
			}
		}

		PII goal = *it;
		int gx = goal.second, gy = goal.first;
		cnt += table[gy][gx];

		REP(i, h)
			REP(j, w)
				table[i][j] = table[i][j] >= 0 ? 0 : -1;
		q.push(goal);
	}

	cout << cnt << endl;
	
	return 0;
}

C++: #include <iostream>
#include <queue>
#include <climits>
#define REP(i,n) for(int i=0;i<(n);i++)
using namespace std;

typedef pair<int,int> pii;

int main()
{
  int H, W, N;
  const int dx[] = {1, 0, -1, 0}, dy[] = {0, 1, 0, -1};
  cin >> H >> W >> N;
  char map[H][W];
  int d[H][W], hp = 1;
  fill(d[0],d[H],INT_MAX);
  queue<pii> q;
  REP(i,H){
    REP(j,W){
      cin >> map[i][j];
      if(map[i][j] == 'S'){
        q.push({i,j});
        d[i][j] = 0;
      }
    }
  }
  while(!q.empty()){
    pii p = q.front();
    q.pop();
    if(map[p.first][p.second] >= '1' && map[p.first][p.second] <= '9' && hp == map[p.first][p.second]-'0') hp++;
    if(hp == N+1){
      cout << d[p.first][p.second] << endl;
      return 0;
    }
    REP(i,4){
      pii next_p = {p.first+dx[i],p.second+dy[i]};
      if(next_p.first < 0 || next_p.first >= H || next_p.second < 0 || next_p.second >= W || map[next_p.first][next_p.second] == 'X') continue;
      if(d[next_p.first][next_p.second] > d[p.first][p.second]+1){
        d[next_p.first][next_p.second] = d[p.first][p.second]+1;
        q.push(next_p);
      }
    }
  }
}