Language: C++

import sys
sys.setrecursionlimit(100000000)


def getStart(field):
    search_field = field[1:]     # cut the first [H,W,N] for min_path matrix
    for y in range(len(search_field)):
        for x in range(len(search_field[0])):
            if search_field[y][x] == "S":
                return x,y+1


def bfs(field,start_x,start_y,goal_N):
    direction = [[-1,0],[1,0],[0,-1],[0,1]]
    gotten_cheese = 1
    distance = 0
    que = []
    que.append([start_x,start_y,gotten_cheese,distance])
    INF = 1
    min_path = [[INF for j in range(field[0][1])] for i in range(field[0][0])]
    while len(que) != 0:
        current = que.pop(0)
        print("pop",current)
        for d in direction:
            nx = current[0] + d[0]
            ny = current[1] + d[1]
            if 0 <= nx < field[0][1] and 1 <= ny < field[0][0]+1 and field[ny][nx] != "X" and min_path[ny-1][nx] == INF:
                if field[ny][nx] == current[2]: # at the same cheese-power number
                    if field[ny][nx] == goal_N: # goal
                        print(current[3]+1)
                        return
                    else: # not goal, but could eat cheese
                        que = []
                        que.append([nx,ny,current[2]+1,current[3]+1])
                        min_path = [[INF for j in range(field[0][1])] for i in range(field[0][0])]
                        break
                else:
                    que.append([nx,ny,current[2],current[3]+1])
                    min_path[ny-1][nx] = 0


def main(field):

    # field = [[10,10,9],
    # [".","X",".",".",".","X",".","S",".","X"],
    # [6,".",".",5,"X",".",".","X",1,"X"],
    # [".",".",".","X","X","X","X",".",".","X"],
    # ["X",".",".",9,"X",".",".",".","X","."],
    # [8,".","X",2,"X",".",".","X",3,"X"],
    # [".",".",".","X","X",".","X",4,".","."],
    # ["X","X",".",".",".",".",7,"X",".","."],
    # ["X",".",".","X",".",".","X","X",".","."],
    # ["X",".",".",".","X",".","X","X",".","."],
    # [".",".","X",".",".",".",".",".",".","."]]

    sx,sy = getStart(field)
    bfs(field,sx,sy,field[0][2])

matrix = []
while True:
    row = input().split()
    if len(row) == 1:
        re_row = []
        for char in row[0]:
            if char.isdigit():
                re_row.append(int(char))
            else:
                re_row.append(char)
        matrix.append(re_row)
    else:
        matrix.append(row)
    if len(matrix) == int(matrix[0][0]) + 1:
        main()
        break

Compiler output:
a.cc:6:34: error: stray '#' in program
    6 |     search_field = field[1:]     # cut the first [H,W,N] for min_path matrix
      |                                  ^
a.cc:28:49: error: stray '#' in program
   28 |                 if field[ny][nx] == current[2]: # at the same cheese-power number
      |                                                 ^
a.cc:29:49: error: stray '#' in program
   29 |                     if field[ny][nx] == goal_N: # goal
      |                                                 ^
a.cc:32:27: error: stray '#' in program
   32 |                     else: # not goal, but could eat cheese
      |                           ^
a.cc:44:7: error: invalid preprocessing directive #field
   44 |     # field = [[10,10,9],
      |       ^~~~~
a.cc:45:7: error: invalid preprocessing directive #[
   45 |     # [".","X",".",".",".","X",".","S",".","X"],
      |       ^
a.cc:46:7: error: invalid preprocessing directive #[
   46 |     # [6,".",".",5,"X",".",".","X",1,"X"],
      |       ^
a.cc:47:7: error: invalid preprocessing directive #[
   47 |     # [".",".",".","X","X","X","X",".",".","X"],
      |       ^
a.cc:48:7: error: invalid preprocessing directive #[
   48 |     # ["X",".",".",9,"X",".",".",".","X","."],
      |       ^
a.cc:49:7: error: invalid preprocessing directive #[
   49 |     # [8,".","X",2,"X",".",".","X",3,"X"],
      |       ^
a.cc:50:7: error: invalid preprocessing directive #[
   50 |     # [".",".",".","X","X",".","X",4,".","."],
      |       ^
a.cc:51:7: error: invalid preprocessing directive #[
   51 |     # ["X","X",".",".",".",".",7,"X",".","."],
      |       ^
a.cc:52:7: error: invalid preprocessing directive #[
   52 |     # ["X",".",".","X",".",".","X","X",".","."],
      |       ^
a.cc:53:7: error: invalid preprocessing directive #[
   53 |     # ["X",".",".",".","X",".","X","X",".","."],
      |       ^
a.cc:54:7: error: invalid preprocessing directive #[
   54 |     # [".",".","X",".",".",".",".",".",".","."]]
      |       ^
a.cc:1:1: error: 'import' does not name a type
    1 | import sys
      | ^~~~~~
a.cc:1:1: note: C++20 'import' only available with '-fmodules-ts'