Language: C++

#include<algorithm>
#include<iostream>
#include<queue>
#include<ctype.h>
#include<limits.h>

int h, w, n;
int sx, sy;
int gx, gy;
char map[1002][1002];
int d[1001][1001];
bool visited[1001][1001];
int dx[] = { 0, 1, 0, -1 }, dy[] = { -1, 0, 1, 0 };
int ans;

bool compare( const data &d1, const data &d2 )
{
	return d1.d > d2.d;
}

void bfs()
{
	d[sx][sy] = 0;
	visited[sx][sy] = true;
	std::queue<std::pair<int,int> > q;
	q.push( std::make_pair<int,int>( sx, sy ) );
	
	while( !q.empty() ){
		std::pair<int,int> p = q.front(); q.pop();
		int x = p.first;
		int y = p.second;
		
		for( int i = 0; i < 4; i++ ){
			int ix = x + dx[i];
			int iy = y + dy[i];
			
			if( ix >= 1 && ix <= w && iy >= 1 && iy <= h && map[ix][iy] != 'X' && !visited[ix][iy] ){
			 	d[ix][iy] = d[x][y] + 1;
				q.push( std::make_pair<int,int>( ix, iy ) );
				visited[ix][iy] = true;
			}
		}	
	}

	return;
}

int main()
{
	std::cin >> h >> w >> n;
	
	for( int i = 1; i <= h; i++ ){
	 	for( int j = 1; j <= w; j++ ){
			std::cin >> map[j][i];
			if( map[j][i] == 'S' ){
			 	sx = j;
				sy = i;
			}
		}
	}
	
	for( int k = 1; k <= n; k++ ){
		for( int i = 1; i <= h; i++ ){
		 	for( int j = 1; j <= w; j++ ){
			 	d[j][i] = INT_MAX / 4;
				visited[j][i] = false;
			}
		}	
		
		bfs();
		
		for( int i = 1; i <= h; i++ ){
		 	for( int j = 1; j <= w; j++ ){
				if( map[j][i] == k + '0' ){
				 	gx = j;
					gy = i;
				}
			}
		}
		
		ans += d[gx][gy];
		
		sx = gx;
		sy = gy;
	}
	
	std::cout << ans << std::endl;

	return 0;
}

Compiler output:
a.cc:16:21: error: 'data' does not name a type
   16 | bool compare( const data &d1, const data &d2 )
      |                     ^~~~
a.cc:16:37: error: 'data' does not name a type
   16 | bool compare( const data &d1, const data &d2 )
      |                                     ^~~~
a.cc: In function 'bool compare(const int&, const int&)':
a.cc:18:19: error: request for member 'd' in 'd1', which is of non-class type 'const int'
   18 |         return d1.d > d2.d;
      |                   ^
a.cc:18:26: error: request for member 'd' in 'd2', which is of non-class type 'const int'
   18 |         return d1.d > d2.d;
      |                          ^
a.cc: In function 'void bfs()':
a.cc:26:42: error: cannot bind rvalue reference of type 'int&&' to lvalue of type 'int'
   26 |         q.push( std::make_pair<int,int>( sx, sy ) );
      |                                          ^~
In file included from /usr/include/c++/14/bits/stl_algobase.h:64,
                 from /usr/include/c++/14/algorithm:60,
                 from a.cc:1:
/usr/include/c++/14/bits/stl_pair.h:1132:21: note:   initializing argument 1 of 'constexpr std::pair<typename std::__strip_reference_wrapper<typename std::decay<_Tp>::type>::__type, typename std::__strip_reference_wrapper<typename std::decay<_Tp2>::type>::__type> std::make_pair(_T1&&, _T2&&) [with _T1 = int; _T2 = int; typename __strip_reference_wrapper<typename decay<_Tp>::type>::__type = int; typename decay<_Tp>::type = int; typename __strip_reference_wrapper<typename decay<_Tp2>::type>::__type = int; typename decay<_Tp2>::type = int]'
 1132 |     make_pair(_T1&& __x, _T2&& __y)
      |               ~~~~~~^~~
a.cc:39:66: error: cannot bind rvalue reference of type 'int&&' to lvalue of type 'int'
   39 |                                 q.push( std::make_pair<int,int>( ix, iy ) );
      |                                                                  ^~
/usr/include/c++/14/bits/stl_pair.h:1132:21: note:   initializing argument 1 of 'constexpr std::pair<typename std::__strip_reference_wrapper<typename std::decay<_Tp>::type>::__type, typename std::__strip_reference_wrapper<typename std::decay<_Tp2>::type>::__type> std::make_pair(_T1&&, _T2&&) [with _T1 = int; _T2 = int; typename __strip_reference_wrapper<typename decay<_Tp>::type>::__type = int; typename decay<_Tp>::type = int; typename __strip_reference_wrapper<typename decay<_Tp2>::type>::__type = int; typename decay<_Tp2>::type = int]'
 1132 |     make_pair(_T1&& __x, _T2&& __y)
      |               ~~~~~~^~~