Language: C++

#include <iostream>
#include <vector>
#include <queue>
#include <algorithm>

#define rep(i,a,b) for(int i=(a);i<(b);i++)
#define pb push_back
#define mp(a,b) make_pair(a,b)
#define all(a) a.begin(),a.end()
#define pii pair<int, int>
#define ll long long
using namespace std; 

#define MAX 10
#define SIZE 1000
#define INF 1<<30

int H, W, N;
char sec[SIZE][SIZE+1];
int time = INF;
int all_time = 0;
int dy[4] = {-1, 0, 1, 0};
int dx[4] = {0, 1, 0, -1};
int facy[10] = {};
int facx[10] = {};

struct state {
	int sy, sx, gy, gx, cnt;
	state() {}
	state(int _sy, int _sx, int _gy, int _gx, int _cnt) :
		sy(_sy), sx(_sx), gy(_gy), gx(_gx), cnt(_cnt) {}
};
queue<state> que;

void bfs()
{
	int **tsec = new int*[SIZE];
	rep(i,0,SIZE) tsec[i] = new int[SIZE+1];
	rep(i,0,H) rep(j,0,W) tsec[i][j] = (int)sec[i][j];
	//rep(i,0,H) { rep(j,0,W) cout << tsec[i][j] << " "; cout << endl;}

	while (!que.empty()) {
		state ts = que.front(); que.pop();
		//printf("sy=%d sx=%d gy=%d gx=%d cnt=%d\n",ts.sy,ts.sx,ts.gy,ts.gx,ts.cnt);
		if (ts.cnt > time) return;
		if (ts.sy == ts.gy && ts.sx == ts.gx) time = min(time, ts.cnt);
		tsec[ts.sy][ts.sx] = ts.cnt;

		rep(i, 0, 4) {
			int ny = ts.sy + dy[i];
			int nx = ts.sx + dx[i];
			if (ny < 0 || ny >= H) continue;
			if (nx < 0 || nx >= W) continue;
			if (sec[ny][nx] == 'X') continue;
			if (tsec[ny][nx] < ts.cnt) continue; 
			state ns(ny, nx, ts.gy, ts.gx, ts.cnt + 1);
			que.push(ns);
		}
	//rep(i,0,H) { rep(j,0,W) cout << tsec[i][j] << " "; cout << endl;}
	}

	rep(i,0,SIZE) delete[] tsec[i];
	delete[] tsec;
}

void solve()
{
	// rep(i,0,N+1) printf("%d y=%d x=%d\n",i,facy[i],facx[i]);

	rep(i,0,N) {
		time = INF;
		while(!que.empty()) que.pop();
		state s(facy[i], facx[i], facy[i+1], facx[i+1], 0);
		que.push(s);
		//printf("\n bfs start !! \n");
		bfs();
		all_time += time;
	}

	cout << all_time << endl;
}

void input()
{
	cin >> H >> W >> N;
	rep(i,0,H) { 
		rep(j,0,W) {
			cin >> sec[i][j];
			if (sec[i][j] == 'S') { facy[0] = i; facx[0] = j; }
			if (sec[i][j] >= 49 && 57 >= sec[i][j]) {
				facy[sec[i][j]-48] = i;
				facx[sec[i][j]-48] = j;
			}
		}
	}
	solve();
}

int main(void)
{
	input();
	
	return 0;
}

Compiler output:
a.cc:20:5: error: 'int time' redeclared as different kind of entity
   20 | int time = INF;
      |     ^~~~
In file included from /usr/include/pthread.h:23,
                 from /usr/include/x86_64-linux-gnu/c++/14/bits/gthr-default.h:35,
                 from /usr/include/x86_64-linux-gnu/c++/14/bits/gthr.h:157,
                 from /usr/include/c++/14/ext/atomicity.h:35,
                 from /usr/include/c++/14/bits/ios_base.h:39,
                 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/time.h:76:15: note: previous declaration 'time_t time(time_t*)'
   76 | extern time_t time (time_t *__timer) __THROW;
      |               ^~~~
a.cc: In function 'void bfs()':
a.cc:45:28: error: ISO C++ forbids comparison between pointer and integer [-fpermissive]
   45 |                 if (ts.cnt > time) return;
      |                     ~~~~~~~^~~~~~
a.cc:46:65: error: no matching function for call to 'min(time_t (&)(time_t*) noexcept, int&)'
   46 |                 if (ts.sy == ts.gy && ts.sx == ts.gx) time = min(time, ts.cnt);
      |                                                              ~~~^~~~~~~~~~~~~~
In file included from /usr/include/c++/14/string:51,
                 from /usr/include/c++/14/bits/locale_classes.h:40,
                 from /usr/include/c++/14/bits/ios_base.h:41:
/usr/include/c++/14/bits/stl_algobase.h:233:5: note: candidate: 'template<class _Tp> constexpr const _Tp& std::min(const _Tp&, const _Tp&)'
  233 |     min(const _Tp& __a, const _Tp& __b)
      |     ^~~
/usr/include/c++/14/bits/stl_algobase.h:233:5: note:   template argument deduction/substitution failed:
a.cc:46:65: note:   deduced conflicting types for parameter 'const _Tp' ('long int(long int*) noexcept' and 'int')
   46 |                 if (ts.sy == ts.gy && ts.sx == ts.gx) time = min(time, ts.cnt);
      |                                                              ~~~^~~~~~~~~~~~~~
/usr/include/c++/14/bits/stl_algobase.h:281:5: note: candidate: 'template<class _Tp, class _Compare> constexpr const _Tp& std::min(const _Tp&, const _Tp&, _Compare)'
  281 |     min(const _Tp& __a, const _Tp& __b, _Compare __comp)
      |     ^~~
/usr/include/c++/14/bits/stl_algobase.h:281:5: note:   candidate expects 3 arguments, 2 provided
In file included from /usr/include/c++/14/algorithm:61,
                 from a.cc:4:
/usr/include/c++/14/bits/stl_algo.h:5686:5: note: candidate: 'template<class _Tp> constexpr _Tp std::min(initializer_list<_Tp>)'
 5686 |     min(initializer_list<_Tp> __l)
      |     ^~~
/usr/include/c++/14/bits/stl_algo.h:5686:5: note:   candidate expects 1 argument, 2 provided
/usr/include/c++/14/bits/stl_algo.h:5696:5: note: candidate: 'template<class _Tp, class _Compare> constexpr _Tp std::min(initializer_list<_Tp>, _Compare)'
 5696 |     min(initializer_list<_Tp> __l, _Compare __comp)
      |     ^~~
/usr/include/c++/14/bits/stl_algo.h:5696:5: note:   template argument deduction/substitution failed:
a.cc:46:65: note:   mismatched types 'std::initializer_list<_Tp>' and 'time_t (*)(time_t*) noexcept' {aka 'long int (*)(long int*) noexcept'}
   46 |                 if (ts.sy == ts.gy && ts.sx == ts.gx) time = min(time, ts.cnt);
      |                                                              ~~~^~~~~~~~~~~~~~
a.cc: In function 'void solve()':
a.cc:71:22: error: assignment of function 'time_t time(time_t*)'
   71 |                 time = INF;
      |                      ^
a.cc:77:26: warning: pointer to a function used in arithmetic [-Wpointer-arith]
   77 |                 all_time += time;
      |                 ~~~~~~~~~^~~~~~~
a.cc:77:26: error: invalid conversion from 'time_t (*)(time_t*) noexcept' {aka 'long int (*)(long int*) noexcept'} to 'int' [-fpermissive]
   77 |                 all_time += time;
      |                 ~~~~~~~~~^~~~~~~
      |                          |
      |                          time_t (*)(time_t*) noexcept {aka long int (*)(long int*) noexcept}