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<queue>

using namespace std;

int h, w, n;

char map[1001][1001];

int vis[1001][1001];

const int v[2][4] = { {1,0,-1,0},{0,-1,0,1} };

queue<pair<int ,int>> Pos;
queue <pair<int, pair< int, int > >> que;//?????°?????§?¨?


int main() {
	cin >> h >> w >> n;
	for (int i = 1; i <= h; i++) {
		for (int j = 1; j <= w; j++) {
			cin >> map[i][j];
			if (map[i][j] == 'S')que.push(make_pair(0,make_pair(i,j)));
		}
	}
	for (int i = 1; i <= n; i++) {
		for (int j = 1; j <= h; j++) {
			for (int k = 1; k <= w; k++) {
				if (map[j][k] - '0' == i)Pos.push(make_pair(j,k));
			}
		}
	}
	int ct = 0;
	while (Pos.size()) {
		pair<int, int > P = Pos.front();
		hPos.pop();
		while (que.size()) {
			pair<int,pair< int,int > > a = que.front();
			que.pop();
			if (vis[a.second.first][a.second.second] != 0)continue;
			vis[a.second.first][a.second.second] = a.first + 1;
			if (a.second.first == P.first&&a.second.second == P.second) {
				ct += vis[a.second.first][a.second.second];
				for (int i = 1; i <= h; i++) {
					for (int j = 1; j <= w; j++) {
						vis[i][j] = 0;
					}
				}
				while (que.size())que.pop();
				if(Pos.size())que.push(make_pair(0,make_pair(a.second.first,a.second.second)));
				ct -= 1;
				break;
			}
			for (int i = 0; i < 4; i++) {
				int px= a.second.first + v[0][i], py= a.second.second + v[1][i];
				if (1 <= px&&px <= h && 1 <= py&&py <= w&&vis[px][py] == 0 && map[px][py] != 'S'&&map[px][py]!='X') {
					que.push(make_pair(vis[a.second.first][a.second.second], make_pair(px, py)));
				}
			}
		}
	}
	cout << ct << endl;
	return 0;
}

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) {
    cin>>H>>W>>N;
    P s,e;
    for (int i = 0; i<H; i++)
        cin>>Maps[i];
    for (int i = 0; i<H; i++)
        for (int j = 0; j<W; j++)
            if (Maps[i][j]=='S') s.first = i, s.second = j;
    
    
    for (int i = 1; i<=N; i++) {
        clear();
        s = bfs(i, s);
    }
    
    cout<<ans<<endl;
    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<iostream>
#include<queue>
#include<string.h>
using namespace std;
char map[1005][1005];
char map1[1005][1005];
int a[1005][1005];
int m,n,q,num=0;
int main()
{
	int x,y;
	cin>>m>>n>>q;
	for(int i=0;i<m;i++)
	{
		for(int j=0;j<n;j++)
		{
			cin>>map[i][j];
			if(map[i][j]=='S')
				{
					x=i;
					y=j;
				 map[x][y]='0';
				}
		}
	}
	for(int i=0;i<m;i++)
		{
			for(int j=0;j<n;j++)
			{
				map1[i][j]=map[i][j];

			}
		}

	for(int k=1;k<=q;k++)
	{
			queue<int> ac;
			queue<int> ak;

		for(int i=0;i<m;i++)
		{
			for(int j=0;j<n;j++)
			{
				map[i][j]=map1[i][j];
				if(map1[i][j]-'0'==k-1)
				{
					x=i;
					y=j;
					map[x][y]='X';
				}
			}
		}
		memset(a,0,sizeof(a));
		ac.push(x);
		ak.push(y);
		while(!ac.empty())
		{
			x=ac.front();
			y=ak.front();
			ac.pop();
			ak.pop();
			if(x+1<m)
			{
				if(map[x+1][y]!='X')
				{
					if(map[x+1][y]-'0'==k)
					{
						num+=a[x][y];
						num+=1;
						break;
					}
						map[x+1][y]='X';
						ac.push(x+1);
						ak.push(y);
						a[x+1][y]=a[x][y]+1;
				}
			}
				if(x-1>=0)
				{
					if(map[x-1][y]!='X')
					{

						if(map[x-1][y]-'0'==k)
						{
							num+=a[x][y]+1;
							break;
						}
						map[x-1][y]='X';
						ac.push(x-1);
						ak.push(y);
						a[x-1][y]=a[x][y]+1;
					}
				}
			if(y+1<n)
			{
				if(map[x][y+1]!='X')
				{

					if(map[x][y+1]-'0'==k)
					{
						num+=a[x][y]+1;
						break;
					}

						map[x][y+1]='X';
						ac.push(x);
						ak.push(y+1);
						a[x][y+1]=a[x][y]+1;
				}
			}
			if(y-1>=0)
			{
				if(map[x][y-1]!='X')
				{

					if(map[x][y-1]-'0'==k)
					{
						num+=a[x][y]+1;
						break;
					}

						map[x][y-1]='X';
						ac.push(x);
						ak.push(y-1);
						a[x][y-1]=a[x][y]+1;
				}
			}
		}
	}
	cout<<num<<endl;
	return 0;
}