Problem p03436 (python)

Fixed submission:
## coding: UTF-8
from decimal import *
from itertools import permutations, combinations,combinations_with_replacement,product
import math



s = input().split()
p = [int(w) for w in s] #これで勝手にNまで拾ってくれます

H = p[0]
W = p[1]

a = []
for i in range(H):
    a.append(input())


#パディング
#グリッド入力と組み合わせる
#上と下に入れる行を作る
b = ''
for i in range(W+2):
    b += '#'
#print(b)
#上と下と右と左に空きマスを挿入する
a.insert(0, b)
for i in range(1, 1+H):
    a[i] = '#' + a[i] + '#'
a.append(b)











header = ' '
for i in range(len(a[0])):
    header += ('{}'.format(i))

c=[]
for i in range(H+2):
    c.append([])

for i in range(H+2):
    for j in range(W+2):
        c[i].append(a[i][j])


#print(header)
#for i in range(len(c)):
    #print('{}{}'.format(i,''.join(c[i])))



white = 0
for i in range(H+2):
    for j in range(W+2):
        if(c[i][j] == '.'):
            white += 1
#print('white:{}'.format(white))



start = [1, 1]
goal = [H, W]

c[start[0]][start[1]] = 'u'


def see_banmen():
    print(header)
    for i in range(len(c)):
        print('{}{}'.format(i,''.join(c[i])))


queue = [start]
next_list = [] #次の歩数で探すものを格納しておく
step = 0
mode = 0
answer_mode = 0
status = True #ゴール判定でゴールについていたらFalseにする
answer = -1
#see_banmen()
while status:
    if(len(queue) > 0):
        pivot = queue.pop(0) #軸足はqueueの先頭
        x = pivot[0]
        y = pivot[1]
        if(c[x+1][y] == '.'):
            c[x+1][y] = 'u'
            next_list.append([x+1, y])
        if(c[x-1][y] == '.'):
            c[x-1][y] = 'u'
            next_list.append([x-1, y])
        if(c[x][y+1] == '.'):
            c[x][y+1] = 'u'
            next_list.append([x, y+1])
        if(c[x][y-1] == '.'):
            c[x][y-1] = 'u'
            next_list.append([x, y-1])
        c[x][y] = 'd'
    else:
        #stepを進めます
        if(len(next_list) > 0):
            for i in range(len(next_list)):
                queue.append(next_list.pop(0))
            next_list = []
            #print()
            #print('step{}end'.format(step))
            if(c[goal[0]][goal[1]] == 'd' and answer_mode == 0):
                answer = step
                answer_mode = 1
                #print()
                #print('goal:step{}'.format(answer))
                #print()
            step += 1
            #see_banmen()
        elif(mode == 0):
            #もう進めない
            mode = 1
            #print()
            #print('step{}end'.format(step))
            if(c[goal[0]][goal[1]] == 'd' and answer_mode == 0):
                #goal についているならgoal歩数を登録してanswer_mode を　1にする
                answer = step
                answer_mode = 1
                #print()
                #print('goal:step{}'.format(answer))
                #print()
            #goalについていない/goalについていなかったら歩数を進める
            step += 1
            #see_banmen()
        else:
            status = False
#print(answer)

#print(white - answer - 1)
if(answer == -1):
    print(-1)
else:
    print(white - answer - 1)

Buggy submission (verdict: Runtime Error):
## coding: UTF-8
from decimal import *
from itertools import permutations, combinations,combinations_with_replacement,product
import math



s = input().split()
p = [int(w) for w in s] #これで勝手にNまで拾ってくれます

H = p[0]
W = p[1]

a = []
for i in range(H):
    a.append(input())


#パディング
#グリッド入力と組み合わせる
#上と下に入れる行を作る
b = ''
for i in range(W+2):
    b += '#'
#print(b)
#上と下と右と左に空きマスを挿入する
a.insert(0, b)
for i in range(1, 1+H):
    a[i] = '#' + a[i] + '#'
a.append(b)











header = ' '
for i in range(len(a[0])):
    header += ('{}'.format(i))

c=[]
for i in range(H+2):
    c.append([])

for i in range(H+2):
    for j in range(W+2):
        c[i].append(a[i][j])


#print(header)
#for i in range(len(c)):
    #print('{}{}'.format(i,''.join(c[i])))



white = 0
for i in range(H+2):
    for j in range(W+2):
        if(c[i][j] == '.'):
            white += 1
#print('white:{}'.format(white))



start = [1, 1]
goal = [H, W]

c[start[0]][start[1]] = 'u'


def see_banmen():
    print(header)
    for i in range(len(c)):
        print('{}{}'.format(i,''.join(c[i])))


queue = [start]
next_list = [] #次の歩数で探すものを格納しておく
step = 0
mode = 0
answer_mode = 0
status = True #ゴール判定でゴールについていたらFalseにする
#see_banmen()
while status:
    if(len(queue) > 0):
        pivot = queue.pop(0) #軸足はqueueの先頭
        x = pivot[0]
        y = pivot[1]
        if(c[x+1][y] == '.'):
            c[x+1][y] = 'u'
            next_list.append([x+1, y])
        if(c[x-1][y] == '.'):
            c[x-1][y] = 'u'
            next_list.append([x-1, y])
        if(c[x][y+1] == '.'):
            c[x][y+1] = 'u'
            next_list.append([x, y+1])
        if(c[x][y-1] == '.'):
            c[x][y-1] = 'u'
            next_list.append([x, y-1])
        c[x][y] = 'd'
    else:
        if(len(next_list) > 0):
            for i in range(len(next_list)):
                queue.append(next_list.pop(0))
            next_list = []
            #print()
            #print('step{}end'.format(step))
            if(c[goal[0]][goal[1]] == 'd' and answer_mode == 0):
                answer = step
                answer_mode = 1
                #print()
                #print('goal:step{}'.format(answer))
                #print()
            step += 1
            #see_banmen()
        elif(mode == 0):
            mode = 1
            #print()
            #print('step{}end'.format(step))
            if(c[goal[0]][goal[1]] == 'd' and answer_mode == 0):
                answer = step
                answer_mode = 1
                #print()
                #print('goal:step{}'.format(answer))
                #print()
            step += 1
            #see_banmen()
        else:
            status = False
#print(answer)

print(white - answer - 1)