Language: C++

#include <iostream>
#include <queue>
#include <algorithm>
#define N 1005
#define INFINITY 2000000000
using namespace std;
void bfs();
int h,w,n,num[N][N],d[N*N],M[N*N][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++;
    }
  }
  m=max(h,w);
  for(i=0;i<=m;i++)
    for(j=0;j<=m;j++)
      M[i][j]=0;
  for(i=1;i<=h;i++){
    for(j=1;j<=w;j++){
      if(num[i][j]!=-1&&num[i+1][j]!=-1)
	M[num[i][j]][num[i+1][j]]=M[num[i+1][j]][num[i][j]]=1;
      if(num[i][j]!=-1&&num[i][j+1]!=-1)
	M[num[i][j]][num[i][j+1]]=M[num[i][j+1]][num[i][j]]=1;
    }
  }
  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++;
    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(M[u][num[i][j]]&&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;
    if(x>n) break;
  }
  cout << t << endl;
}

Compiler output:
/tmp/ccgxHeUm.o: in function `main':
a.cc:(.text+0xc7): relocation truncated to fit: R_X86_64_PC32 against symbol `in[abi:cxx11]' defined in .bss section in /tmp/ccgxHeUm.o
a.cc:(.text+0xef): relocation truncated to fit: R_X86_64_PC32 against symbol `in[abi:cxx11]' defined in .bss section in /tmp/ccgxHeUm.o
a.cc:(.text+0x116): relocation truncated to fit: R_X86_64_PC32 against symbol `in[abi:cxx11]' defined in .bss section in /tmp/ccgxHeUm.o
a.cc:(.text+0x173): relocation truncated to fit: R_X86_64_PC32 against symbol `in[abi:cxx11]' defined in .bss section in /tmp/ccgxHeUm.o
a.cc:(.text+0x203): relocation truncated to fit: R_X86_64_PC32 against symbol `m' defined in .bss section in /tmp/ccgxHeUm.o
a.cc:(.text+0x24a): relocation truncated to fit: R_X86_64_PC32 against symbol `m' defined in .bss section in /tmp/ccgxHeUm.o
a.cc:(.text+0x259): relocation truncated to fit: R_X86_64_PC32 against symbol `m' defined in .bss section in /tmp/ccgxHeUm.o
/tmp/ccgxHeUm.o: in function `bfs()':
a.cc:(.text+0x62e): relocation truncated to fit: R_X86_64_PC32 against symbol `color[abi:cxx11]' defined in .bss section in /tmp/ccgxHeUm.o
a.cc:(.text+0x6d3): relocation truncated to fit: R_X86_64_PC32 against symbol `in[abi:cxx11]' defined in .bss section in /tmp/ccgxHeUm.o
a.cc:(.text+0x72e): relocation truncated to fit: R_X86_64_PC32 against symbol `color[abi:cxx11]' defined in .bss section in /tmp/ccgxHeUm.o
a.cc:(.text+0x7f7): additional relocation overflows omitted from the output
collect2: error: ld returned 1 exit status