Language: C++

  for( k = 0 ; k < h ; k++ ){
    for( l = 0 ; l < w ; l++){
      if(s[k][l] == 'S'){
	x[0] = l; y[0] = k;
      }
      if('1' <= s[k][l] && s[k][l] <= '9'){
	v = s[k][l]-'1'+1;
	x[v] = l; y[v] = k;
      }
    }
  }
  for (k = 1 ; k <= n ; k++){
   
    result += bfs(x[k-1],y[k-1],x[k],y[k]);
  }
  printf("%d\n", result);
  return 0;
}

int bfs(int sx, int sy, int gx, int gy){
  queue<int> Q;
  int i, j;
  int dx[4]={0, 1, 0, -1};
  int dy[4]={1, 0, -1, 0};
  

  for( i =  0 ; i < h ; i++ ){
    for( j = 0 ; j < w ; j++ ){
      d[i][j] = 100000;
    }
  }

  Q.push(sy*w+sx);
  d[sy][sx]=0; /*スタート地点の初期化*/

  while( !Q.empty() ){
    int u = Q.front(); Q.pop();
    int pi = u/w;
    int pj = u%w;
    if( pi == gy && pj == gx )return d[gy][gx];
    for ( int r = 0; r < 4; r++ ){
      int ni = pi + dy[r];
      int nj = pj + dx[r];
      if ( ni < 0 || nj < 0 || ni >= h || nj >= w ) continue;
      if ( s[ni][nj] == 'X' ) continue;
      if( d[ni][nj] > d[pi][pj]+1 ){
      d[ni][nj] = d[pi][pj] + 1;
      Q.push( ni * w + nj );
      }

    }


  }
  //  printf("%d\n",u);
  return -1;
}

Compiler output:
a.cc:1:3: error: expected unqualified-id before 'for'
    1 |   for( k = 0 ; k < h ; k++ ){
      |   ^~~
a.cc:1:16: error: 'k' does not name a type
    1 |   for( k = 0 ; k < h ; k++ ){
      |                ^
a.cc:1:24: error: 'k' does not name a type
    1 |   for( k = 0 ; k < h ; k++ ){
      |                        ^
a.cc:12:3: error: expected unqualified-id before 'for'
   12 |   for (k = 1 ; k <= n ; k++){
      |   ^~~
a.cc:12:16: error: 'k' does not name a type
   12 |   for (k = 1 ; k <= n ; k++){
      |                ^
a.cc:12:25: error: 'k' does not name a type
   12 |   for (k = 1 ; k <= n ; k++){
      |                         ^
a.cc:16:9: error: expected constructor, destructor, or type conversion before '(' token
   16 |   printf("%d\n", result);
      |         ^
a.cc:17:3: error: expected unqualified-id before 'return'
   17 |   return 0;
      |   ^~~~~~
a.cc:18:1: error: expected declaration before '}' token
   18 | }
      | ^
a.cc: In function 'int bfs(int, int, int, int)':
a.cc:21:3: error: 'queue' was not declared in this scope
   21 |   queue<int> Q;
      |   ^~~~~
a.cc:21:9: error: expected primary-expression before 'int'
   21 |   queue<int> Q;
      |         ^~~
a.cc:27:21: error: 'h' was not declared in this scope
   27 |   for( i =  0 ; i < h ; i++ ){
      |                     ^
a.cc:28:22: error: 'w' was not declared in this scope
   28 |     for( j = 0 ; j < w ; j++ ){
      |                      ^
a.cc:29:7: error: 'd' was not declared in this scope; did you mean 'dy'?
   29 |       d[i][j] = 100000;
      |       ^
      |       dy
a.cc:33:3: error: 'Q' was not declared in this scope
   33 |   Q.push(sy*w+sx);
      |   ^
a.cc:33:13: error: 'w' was not declared in this scope
   33 |   Q.push(sy*w+sx);
      |             ^
a.cc:34:3: error: 'd' was not declared in this scope; did you mean 'dy'?
   34 |   d[sy][sx]=0; /*スタート地点の初期化*/
      |   ^
      |   dy
a.cc:44:38: error: 'h' was not declared in this scope
   44 |       if ( ni < 0 || nj < 0 || ni >= h || nj >= w ) continue;
      |                                      ^
a.cc:45:12: error: 's' was not declared in this scope
   45 |       if ( s[ni][nj] == 'X' ) continue;
      |            ^