Language: C++

#include<iostream>
#include<queue>
using namespace std;

int flg[1000][1000];
void ini(){
	for(int i=0;i<1000;i++){
		for(int j=0;j<1000;j++){
			flg[i][j]=-1;
		}
	}
}

int main(){
	int x,y,n;
	int map[1001][1001];
	int cnt=0;
	pair<int,int> p;
	ntoa[10]="123456789";
	
	cin>>y>>x>>n;
	for(int i=0i<y;i++){
		for(int j=0;j<x;j++){
			cin>>map[i][j];
			if(map[i][j]=='S'){
				p = make_pair(i,j);
			}
		}
	}
	for(int i=0;i<n;i++){
		ini();
		queue<pair<int,int>> q;
		q.push(p);
		flg[p.first][p.second]=0;
		//幅優先
		while(!q,empty()){
			pair<int,int> temp=q.front();
			q.pop();
			int dx[4]={0,0,-1,1},dy[4]={1,-1,0,0};
			for(int i2=0;i2<4;i2++){
				if( temp.first+dy[i2]<y && temp.first+dy[i2]>-1 && temp.second+dx[i2]<x && temp.second+dx[i2]>-1){
					if(map[temp.first+dy[i2]][temp.second+dx[i2]] != 'X' && flg[temp.first+dy[i2]][temp.second+dx[i2]] == -1){
						q.push(make_pair(temp.first+dy[i2],temp.second+dx[i2]));
						flg[temp.first+dy[i2]][temp.second+dx[i2]] = flg[temp.first][temp.second]+1;
					}
				}
				if(map[temp.first][temp.second] == ntoa[i]){
					cnt+=flg[temp.first][temp.second];
					p=temp;
				}
			}
		}
		//	
	}
	cout<<cnt;
	return 0;
}

Compiler output:
a.cc: In function 'int main()':
a.cc:19:9: error: 'ntoa' was not declared in this scope
   19 |         ntoa[10]="123456789";
      |         ^~~~
a.cc:22:21: error: invalid operands of types '__complex__ int' and 'int' to binary 'operator<'
   22 |         for(int i=0i<y;i++){
a.cc:22:27: error: expected ';' before ')' token
   22 |         for(int i=0i<y;i++){
      |                           ^
      |                           ;
a.cc:36:23: error: no match for 'operator!' (operand type is 'std::queue<std::pair<int, int> >')
   36 |                 while(!q,empty()){
      |                       ^~
a.cc:36:23: note: candidate: 'operator!(bool)' (built-in)
a.cc:36:23: note:   no known conversion for argument 1 from 'std::queue<std::pair<int, int> >' to 'bool'
a.cc:36:31: error: no matching function for call to 'empty()'
   36 |                 while(!q,empty()){
      |                          ~~~~~^~
In file included from /usr/include/c++/14/string:53,
                 from /usr/include/c++/14/bits/locale_classes.h:40,
                 from /usr/include/c++/14/bits/ios_base.h:41,
                 from /usr/include/c++/14/ios:44,
                 from /usr/include/c++/14/ostream:40,
                 from /usr/include/c++/14/iostream:41,
                 from a.cc:1:
/usr/include/c++/14/bits/range_access.h:282:5: note: candidate: 'template<class _Container> constexpr decltype (__cont.empty()) std::empty(const _Container&)'
  282 |     empty(const _Container& __cont) noexcept(noexcept(__cont.empty()))
      |     ^~~~~
/usr/include/c++/14/bits/range_access.h:282:5: note:   candidate expects 1 argument, 0 provided
/usr/include/c++/14/bits/range_access.h:292:5: note: candidate: 'template<class _Tp, long unsigned int _Nm> constexpr bool std::empty(const _Tp (&)[_Nm])'
  292 |     empty(const _Tp (&)[_Nm]) noexcept
      |     ^~~~~
/usr/include/c++/14/bits/range_access.h:292:5: note:   candidate expects 1 argument, 0 provided
/usr/include/c++/14/bits/range_access.h:302:5: note: candidate: 'template<class _Tp> constexpr bool std::empty(initializer_list<_Tp>)'
  302 |     empty(initializer_list<_Tp> __il) noexcept
      |     ^~~~~
/usr/include/c++/14/bits/range_access.h:302:5: note:   candidate expects 1 argument, 0 provided