Problem:
今年も JOI 町のチーズ工場がチーズの生産を始め，ねずみが巣から顔を出した．JOI 町は東西南北に区画整理されていて，各区画は巣，チーズ工場，障害物，空き地のいずれかである．ねずみは巣から出発して全てのチーズ工場を訪れチーズを 1 個ずつ食べる．  この町には，N 個のチーズ工場があり，どの工場も１種類のチーズだけを生産している．チーズの硬さは工場によって異なっており，硬さ 1 から N までのチーズを生産するチーズ工場がちょうど 1 つずつある．  ねずみの最初の体力は 1 であり，チーズを 1 個食べるごとに体力が 1 増える．ただし，ねずみは自分の体力よりも硬いチーズを食べることはできない．  ねずみは，東西南北に隣り合う区画に 1 分で移動することができるが，障害物の区画には入ることができない．チーズ工場をチーズを食べずに通り過ぎることもできる．すべてのチーズを食べ終えるまでにかかる最短時間を求めるプログラムを書け．ただし，ねずみがチーズを食べるのにかかる時間は無視できる．

Incorrect submission (verdict: Wrong Answer):
field = [[4,5,2],[".","X",".",".",1],[".",".",".",".","X"],[".","X","X",".","S"],[".",2,".","X","."]]

for i in range(len(field) - 1):
    for j in range(len(field[1])):
        if field[i+1][j] == "S":
            start_x = j
            start_y = i + 1
            break
print(start_x,start_y)

def proceed(field,start_x,start_y,start_n,start_N,goal_N):
    # print("start[x]",start_x)
    # print("start[y]",start_y)
    x = [-1,0,1,0]
    y = [0,-1,0,1]
    next_x = []
    next_y = []
    next_N  = []
    # print("s_parameters",start_x,start_y,start_N,start_n,goal_N)

    for s_x,s_y,s_N in zip(start_x,start_y,start_N):
        for p_x,p_y in zip(x,y):
            if 1 <= s_y + p_y < len(field):
                #print("y",s_y + p_y)
                if 0 <= s_x + p_x < len(field[1]):
                    #print("x",s_x + p_x)
                    if field[s_y + p_y][s_x + p_x] != "X":
                        # print("x",s_x + p_x)
                        # print("y",s_y + p_y)
                        next_x.append(s_x + p_x)
                        next_y.append(s_y + p_y)
                        # print("[x]",next_x)
                        # print("[y]",next_y)
                        if field[s_y + p_y][s_x + p_x] == goal_N:
                            # print("GOAL_STATE",s_N)
                            next_N.append(s_N)
                            if s_N == goal_N:
                                # print("check",s_N,goal_N,start_N)
                                print("result",start_n + 1)
                                return  
                        elif field[s_y + p_y][s_x + p_x] == s_N:
                                next_N.append(s_N + 1)
                                # print("EAT CHEESE",next_N)
                        else:
                            next_N.append(s_N)
    # print("next_N",next_N)
    next_n = start_n + 1
    proceed(field,next_x,next_y,next_n,next_N,goal_N)

proceed(field,[start_x],[start_y],0,[1],field[0][2])

Fixed, accepted submission:
def bfs(field,H,W,start_x,start_y,tmp_N):
    direction = [[-1,0],[1,0],[0,-1],[0,1]]
    que = []
    que.append([start_x,start_y])
    INF = 1000000
    min_path = [[INF] * W for i in range(H)]
    min_path[start_y][start_x] = 0
    while len(que) != 0:
        current = que.pop(0)
        for d in direction:
            nx = current[0] + d[0]
            ny = current[1] + d[1]
            if 0 <= nx < W and 0 <= ny < H and field[ny][nx] != "X" and min_path[ny][nx] == INF:
                min_path[ny][nx] = min_path[current[1]][current[0]] + 1
                if field[ny][nx] == tmp_N:
                    return nx,ny,min_path[ny][nx]
                else:
                    que.append([nx,ny])

def getField():
    matrix = []
    first = input().strip()
    H,W,N = map(int,first.split())
    for i in range(H):
        row = list(input().strip())
        for j in range(W):
            if row[j].isdigit():
                row[j] = int(row[j])
        matrix.append(row)
    return matrix,H,W,N

def getStart(field):
    for y in range(len(field)):
        for x in range(len(field[0])):
            if field[y][x] == "S":
                return x,y

def main():
    matrix,H,W,N = getField()
    sx,sy = getStart(matrix)
    distance = 0
    tmp_x,tmp_y,tmp_dist = bfs(matrix,H,W,sx,sy,1)
    distance += tmp_dist
    tmps = [tmp_x,tmp_y]
    for k in range(N-1):
        tmp_x,tmp_y,tmp_dist = bfs(matrix,H,W,tmps[0],tmps[1],k+2)
        distance += tmp_dist
        tmps = [tmp_x,tmp_y]
    print(distance)

if __name__=='__main__':
    main()