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 <string>
#include <vector>
#include <iterator>
#include <list>
#include <map>
#include <queue>
#include <bitset>
#include <fstream>
#include <cmath>
using namespace std;
typedef long long ll;

class CPoint
{
public:

	int x, y;

	CPoint() : x(0), y(0){};
	CPoint(int ax, int ay) : x(ax), y(ay){};

	bool operator==(const CPoint& obj);
	bool operator!=(const CPoint& obj);

	CPoint  operator+(const CPoint& obj);
	CPoint  operator-(const CPoint& obj);
	CPoint  operator*(const CPoint& obj);
	CPoint  operator/(const CPoint& obj);

	CPoint& operator+=(const CPoint& obj);
	CPoint& operator-=(const CPoint& obj);
	CPoint& operator*=(const CPoint& obj);
	CPoint& operator/=(const CPoint& obj);

	CPoint& operator=(const int obj);
	CPoint& operator+=(const int obj);
	CPoint& operator-=(const int obj);
	CPoint& operator*=(const int obj);
	CPoint& operator/=(const int obj);
};


class CPoint;

const int MAX_H = 1000;
const int MAX_W = 1000;
const ll  INF	= 1000000000;

char field[MAX_H][MAX_W];
bool used[MAX_H][MAX_W];
ll	 d[MAX_H][MAX_W];
int  H, W, N;

ll getroad(CPoint start, CPoint goal);

int main()
{
	ifstream ifs("data.txt");
	
	ifs>>H>>W>>N;
	vector<CPoint> factorys(N+1);

	for(int i = 0; i < H; i++){
		for(int j = 0; j < W; j++){
			ifs>>field[i][j];
			if(field[i][j] != 'X' && field[i][j] != '.'){
				if(field[i][j] == 'S')	factorys[0] = CPoint(i, j);
				else					factorys[field[i][j] - '0'] = CPoint(i, j);
			}
		}
	}

	ll res = 0;
	for(int i = 0; i < N; i++)
		res += getroad(factorys[i], factorys[i+1]);

	cout<<res<<endl;

	cin.sync(); cin.get();
}

ll getroad(CPoint start, CPoint goal)
{
	for(int i = 0; i < H; i++) for(int j = 0; j < W; j++) used[i][j] = false;
	for(int i = 0; i < H; i++) for(int j = 0; j < W; j++) d[i][j] = INF;
	
	d[start.x][start.y] = 0;

	while(true){

		CPoint v(-1, -1);
		for(int i = 0; i< H; i++)
			for(int j = 0; j <W; j++)
				if(!used[i][j] && field[i][j] != 'X' && (v == CPoint(-1,-1) || d[i][j] < d[v.x][v.y])) v = CPoint(i, j);

		if(v == CPoint(-1, -1)) break;

		used[v.x][v.y] = true;

		if(v.y+1 < W  && field[v.x][v.y+1] != 'X') d[v.x][v.y+1] = min(d[v.x][v.y+1], d[v.x][v.y] + 1);
		if(v.y-1 >= 0 && field[v.x][v.y-1] != 'X') d[v.x][v.y-1] = min(d[v.x][v.y-1], d[v.x][v.y] + 1);
		if(v.x+1 < H  && field[v.x+1][v.y] != 'X') d[v.x+1][v.y] = min(d[v.x+1][v.y], d[v.x][v.y] + 1);
		if(v.x-1 >= 0 && field[v.x-1][v.y] != 'X') d[v.x-1][v.y] = min(d[v.x-1][v.y], d[v.x][v.y] + 1);
	}

	return d[goal.x][goal.y];
}




























CPoint CPoint::operator+(const CPoint& obj)
{
	CPoint tmp;
	tmp.x = x + obj.x;
	tmp.y = x + obj.y;
	return tmp;
}

CPoint CPoint::operator-(const CPoint& obj)
{
	CPoint tmp;
	tmp.x = x + obj.x;
	tmp.y = x + obj.y;
	return tmp;
}

CPoint CPoint::operator*(const CPoint& obj)
{
	CPoint tmp;
	tmp.x = x + obj.x;
	tmp.y = x + obj.y;
	return tmp;
}

CPoint CPoint::operator/(const CPoint& obj)
{
	CPoint tmp;
	tmp.x = x + obj.x;
	tmp.y = x + obj.y;
	return tmp;
}


CPoint& CPoint::operator+=(const CPoint& obj)
{
	x += obj.x;
	y += obj.y;
	return *this;
}

CPoint& CPoint::operator-=(const CPoint& obj)
{
	x += obj.x;
	y += obj.y;
	return *this;
}

CPoint& CPoint::operator*=(const CPoint& obj)
{
	x += obj.x;
	y += obj.y;
	return *this;
}

CPoint& CPoint::operator/=(const CPoint& obj)
{
	x += obj.x;
	y += obj.y;
	return *this;
}

CPoint& CPoint::operator/=(const int obj)
{
	x /= obj;
	y /= obj;
	return *this;
}

CPoint& CPoint::operator=(const int obj)
{
	x = 0;
	y = 0;
	return *this;
}

bool CPoint::operator==(const CPoint& obj)
{
	return (x == obj.x && y == obj.y);
}

bool CPoint::operator!=(const CPoint& obj)
{
	return !(*this == obj);
}

C++: #include<iostream>
#include<map>
#include<queue>
using namespace std;
typedef pair<int,int> P;

int bfs();
char mas[1111][1111];
int d[1111][1111];
int dx[4]={0,0,-1,1};
int dy[4]={-1,1,0,0};
int sx,sy;
int ans=0;
char life='0';
int h,w,n;
int INF=10000000;

int main(){
  cin >> h >> w >> n;
  for(int i=0;i<h;i++){
    for(int j=0;j<w;j++){
      cin >> mas[i][j];
      if(mas[i][j] == 'S'){
	sx = i,sy = j;
      }
    }
  }
  for(int i=1;i<=n;i++){
    life++;
    ans += bfs();
  }
  cout << ans << endl;
}

int bfs(){
  queue <P> que;
  for(int i=0;i<h;i++){
    for(int j=0;j<w;j++){
      d[i][j] = INF;
    }
  }
  que.push(P(sx,sy));
  d[sx][sy] = 0;
  while(que.size()){
    P p = que.front();
    que.pop();
    if(mas[p.first][p.second] == life){
      sx = p.first,sy=p.second;
      break;
    }
    for(int i=0;i<4;i++){
      int nx=p.first + dx[i];
      int ny=p.second + dy[i];
      if(nx >= 0 && nx < h && ny >= 0 && ny < w && mas[nx][ny] != 'X' && d[nx][ny] == INF){
	que.push(P(nx,ny));
	d[nx][ny] = d[p.first][p.second] + 1;
      }
    }
  }
  return d[sx][sy];
}

C++: #include <iostream>
#include <queue>
#include <algorithm>
#define N 1002
#define INFINITY 2000000000
using namespace std;
void bfs();
int h,w,n,num[N][N],d[N*N],u,m;
string color[N*N],in[N*N];
int main(){
  int i,j,k;
  cin >> h >> w >> n;
  for(i=0;i<h+2;i++)
    for(j=0;j<w+2;j++)
      num[i][j]=-1;
  for(i=1;i<=h;i++) cin >> in[i],in[i]='X'+in[i];
  k=0;
  for(i=1;i<=h;i++){
    for(j=1;j<=w;j++){
      if(in[i][j]!='X') num[i][j]=k,k++;
    }
  }
  bfs();
  return 0;
}
void bfs(){
  int i,j,p=1,t=0,flag=0,x=1;
  char start='S',goal='1';
  queue<int> q;
  while(1){
    if(flag==1) start=x+'0',goal=x+'0'+1,x++;
    if(x>n+1) break;
    for(i=1;i<=h;i++){
      for(j=1;j<=w;j++){
	color[num[i][j]]="WHITE",d[num[i][j]]=INFINITY;
      }
    }
    for(i=1;i<=h;i++){
      for(j=1;j<=w;j++){
	if(in[i][j]==start) color[num[i][j]]="GLAY",d[num[i][j]]=0,q.push(num[i][j]);
      }
    }
    while(q.empty()!=1){
      u=q.front();
      q.pop();
      for(i=1;i<=h;i++){
	for(j=1;j<=w;j++){
	  if(num[i][j]!=-1&&(num[i-1][j]==u||num[i+1][j]==u||num[i][j+1]==u||num[i][j-1]==u)&&color[num[i][j]]=="WHITE"){
	    color[num[i][j]]="GLAY";
	    d[num[i][j]]=d[u]+1;
	    q.push(num[i][j]);
	  }
	}
      }
      color[u]="BLACK";
    }
    for(i=1;i<=h;i++){
      for(j=1;j<=w;j++){
	if(in[i][j]==goal) t+=d[num[i][j]];
      }
    }
    flag=1;
  }
  cout << t << endl;
}