Problem p03436 (python)

Fixed submission:
H,W = map(int, input().rstrip().split())

map_list = []
black = 0
for i in range(H):
    map_list.append(input())
    black += map_list[i].count('#')

queue = [(0,0)]
check = [(0,0)]
C = [0,0]
count = 0
hNow,wNow = 0,0

while 1:
    count += 1
    if len(queue) != 0:
        if queue[0] == (H-1,W-1):
            print(H*W - black -1 - max(C))
            break # ゴールか判定
    if len(queue) == 0:
        print(-1)
        break
    hNow,wNow = queue[0][0],queue[0][1]
    queue.pop(0)
    if hNow < H-1:
        if map_list[hNow+1][wNow] != "#":
            if (hNow+1,wNow) not in check:
                queue.append((hNow+1,wNow))
                check.append((hNow+1,wNow))
                C.append(C[count]+1)
    if wNow < W-1:
        if map_list[hNow][wNow+1] != "#":
            if (hNow,wNow+1) not in check:
                queue.append((hNow,wNow+1))
                check.append((hNow,wNow+1))
                C.append(C[count]+1)
    if hNow > 0:
        if map_list[hNow-1][wNow] != "#":
            if (hNow-1,wNow) not in check:
                queue.append((hNow-1,wNow))
                check.append((hNow-1,wNow))
                C.append(C[count]+1)
    if wNow > 0:
        if map_list[hNow][wNow-1] != "#":
            if (hNow,wNow-1) not in check:
                queue.append((hNow,wNow-1))
                check.append((hNow,wNow-1)) #近いやつを入れる
                C.append(C[count]+1)
    #print(queue)
#print(count)
#print(C)
#print(black)

Buggy submission (verdict: Runtime Error):
H,W = map(int, input().rstrip().split())

map_list = []
black = 0
for i in range(H):
    map_list.append(input())
    black += map_list[i].count('#')

queue = [(0,0)]
check = [(0,0)]
C = [0,0]
count = 0
hNow,wNow = 0,0

while 1:
    count += 1
    if len(queue) != 0:
        if queue[0] == (H-1,W-1):
            print(H*W - black -1 - max(C))
            break # ゴールか判定
    if len(check) == H * W:
        print(-1)
        break
    hNow,wNow = queue[0][0],queue[0][1]
    queue.pop(0)
    if hNow < H-1:
        if map_list[hNow+1][wNow] != "#":
            if (hNow+1,wNow) not in check:
                queue.append((hNow+1,wNow))
                check.append((hNow+1,wNow))
                C.append(C[count]+1)
    if wNow < W-1:
        if map_list[hNow][wNow+1] != "#":
            if (hNow,wNow+1) not in check:
                queue.append((hNow,wNow+1))
                check.append((hNow,wNow+1))
                C.append(C[count]+1)
    if hNow > 0:
        if map_list[hNow-1][wNow] != "#":
            if (hNow-1,wNow) not in check:
                queue.append((hNow-1,wNow))
                check.append((hNow-1,wNow))
                C.append(C[count]+1)
    if wNow > 0:
        if map_list[hNow][wNow-1] != "#":
            if (hNow,wNow-1) not in check:
                queue.append((hNow,wNow-1))
                check.append((hNow,wNow-1)) #近いやつを入れる
                C.append(C[count]+1)
    #print(queue)
#print(count)
#print(C)
#print(black)

Buggy submission (verdict: Runtime Error):
H,W = map(int, input().rstrip().split())

map_list = []
black = 0
for i in range(H):
    map_list.append(input())
    black += map_list[i].count('#')

queue = [(0,0)]
check = [(0,0)]
C = [0,0]
count = 0
hNow,wNow = 0,0

while 1:
    count += 1
    if len(queue) != 0:
        if queue[0] == (H-1,W-1):
            break # ゴールか判定

    hNow,wNow = queue[0][0],queue[0][1]
    queue.pop(0)
    if hNow < H-1:
        if map_list[hNow+1][wNow] != "#":
            if (hNow+1,wNow) not in check:
                queue.append((hNow+1,wNow))
                check.append((hNow+1,wNow))
                C.append(C[count]+1)
    if wNow < W-1:
        if map_list[hNow][wNow+1] != "#":
            if (hNow,wNow+1) not in check:
                queue.append((hNow,wNow+1))
                check.append((hNow,wNow+1))
                C.append(C[count]+1)
    if hNow > 0:
        if map_list[hNow-1][wNow] != "#":
            if (hNow-1,wNow) not in check:
                queue.append((hNow-1,wNow))
                check.append((hNow-1,wNow))
                C.append(C[count]+1)
    if wNow > 0:
        if map_list[hNow][wNow-1] != "#":
            if (hNow,wNow-1) not in check:
                queue.append((hNow,wNow-1))
                check.append((hNow,wNow-1)) #近いやつを入れる
                C.append(C[count]+1)
    #print(queue)
#print(count)
#print(C)
#print(black)
print(H*W - black -1 - max(C))