Buggy python submission (verdict: Time Limit Exceeded):
# coding: utf-8 
# Your code here! 
H,W=map(int,input().split()) 
S=[] 

ku=0
for _ in range(H): 
    S.append(list(input()))
    ku+=S[-1].count(".")


def bfs(h,w,num):
    if h==H-1 and w==W-1:
        print(ku-num-1)
        exit() 
    else:
        S[h][w]="#"

    if h!=0: 
        if S[h-1][w]!="#": 
            temp.append([h-1,w]) 

    if w!=0: 
        if S[h][w-1]!="#": 
            temp.append([h,w-1]) 

    if h!=H-1: 
        if S[h+1][w]!="#": 
            temp.append([h+1,w]) 

    if w!=W-1: 
        if S[h][w+1]!="#": 
            temp.append([h,w+1]) 
        
q=[[0,0]] 
count=-1
while True: 
    count+=1
    temp=[]
    while q:
        l=q.pop(0)
        bfs(l[0],l[1],count)
        
    q=temp[:]
    if len(q)==0:
        print(-1)
        exit()




    

Fixed submission:
# coding: utf-8 
# Your code here! 
H,W=map(int,input().split()) 
S=[] 

ku=0
for _ in range(H): 
    S.append(list(input()))
    ku+=S[-1].count(".")


def bfs(h,w,num):
    if h==H-1 and w==W-1:
        print(ku-num-1)
        exit() 
    else:
        pass

    if h!=0: 
        if S[h-1][w]!="#": 
            temp.append([h-1,w])
            S[h-1][w]="#"

    if w!=0: 
        if S[h][w-1]!="#": 
            temp.append([h,w-1]) 
            S[h][w-1]="#"

    if h!=H-1: 
        if S[h+1][w]!="#": 
            temp.append([h+1,w]) 
            S[h+1][w]="#"

    if w!=W-1: 
        if S[h][w+1]!="#": 
            temp.append([h,w+1]) 
            S[h][w+1]="#"
        
q=[[0,0]] 
count=-1
while True: 
    count+=1
    #print(q)
    temp=[]
    while q:
        l=q.pop(0)
        bfs(l[0],l[1],count)
        
    q=temp[:]
    if len(q)==0:
        print(-1)
        exit()

Buggy python submission (verdict: Time Limit Exceeded):
# coding: utf-8 
# Your code here! 
H,W=map(int,input().split()) 
S=[] 

ku=0
for _ in range(H): 
    S.append(list(input()))
    ku+=S[-1].count(".")


def bfs(h,w,num):
    if h==H-1 and w==W-1:
        print(ku-num-1)
        exit() 
    else:
        S[h][w]="#"

    if h!=0: 
        if S[h-1][w]!="#": 
            temp.append([h-1,w]) 

    if w!=0: 
        if S[h][w-1]!="#": 
            temp.append([h,w-1]) 

    if h!=H-1: 
        if S[h+1][w]!="#": 
            temp.append([h+1,w]) 

    if w!=W-1: 
        if S[h][w+1]!="#": 
            temp.append([h,w+1]) 
        
q=[[0,0]] 
count=-1
while True: 
    count+=1
    temp=[]
    while q:
        l=q.pop(0)
        bfs(l[0],l[1],count)
        
    q=temp[:]
    if len(q)==0:
        print(-1)
        exit()




    

Fixed submission:
# coding: utf-8 
# Your code here! 
H,W=map(int,input().split()) 
S=[] 

ku=0
for _ in range(H): 
    S.append(list(input()))
    ku+=S[-1].count(".")


def bfs(h,w,num):
    if h==H-1 and w==W-1:
        print(ku-num-1)
        exit() 
    else:
        pass

    if h!=0: 
        if S[h-1][w]!="#": 
            temp.append([h-1,w])
            S[h-1][w]="#"

    if w!=0: 
        if S[h][w-1]!="#": 
            temp.append([h,w-1]) 
            S[h][w-1]="#"

    if h!=H-1: 
        if S[h+1][w]!="#": 
            temp.append([h+1,w]) 
            S[h+1][w]="#"

    if w!=W-1: 
        if S[h][w+1]!="#": 
            temp.append([h,w+1]) 
            S[h][w+1]="#"
        
q=[[0,0]] 
count=-1
while True: 
    count+=1
    #print(q)
    temp=[]
    while q:
        l=q.pop(0)
        bfs(l[0],l[1],count)
        
    q=temp[:]
    if len(q)==0:
        print(-1)
        exit()

Buggy python submission (verdict: Time Limit Exceeded):
# coding: utf-8 
# Your code here! 
H,W=map(int,input().split()) 
S=[] 

ku=0
for _ in range(H): 
    S.append(list(input()))
    ku+=S[-1].count(".")


def bfs(h,w,num):
    if h==H-1 and w==W-1:
        print(ku-num-1)
        exit() 
    else:
        S[h][w]="#"

    if h!=0: 
        if S[h-1][w]!="#": 
            temp.append([h-1,w]) 

    if w!=0: 
        if S[h][w-1]!="#": 
            temp.append([h,w-1]) 

    if h!=H-1: 
        if S[h+1][w]!="#": 
            temp.append([h+1,w]) 

    if w!=W-1: 
        if S[h][w+1]!="#": 
            temp.append([h,w+1]) 
        
q=[[0,0]] 
count=-1
while True: 
    count+=1
    temp=[]
    while q:
        l=q.pop(0)
        bfs(l[0],l[1],count)
        
    q=temp[:]




    

Fixed submission:
# coding: utf-8 
# Your code here! 
H,W=map(int,input().split()) 
S=[] 

ku=0
for _ in range(H): 
    S.append(list(input()))
    ku+=S[-1].count(".")


def bfs(h,w,num):
    if h==H-1 and w==W-1:
        print(ku-num-1)
        exit() 
    else:
        pass

    if h!=0: 
        if S[h-1][w]!="#": 
            temp.append([h-1,w])
            S[h-1][w]="#"

    if w!=0: 
        if S[h][w-1]!="#": 
            temp.append([h,w-1]) 
            S[h][w-1]="#"

    if h!=H-1: 
        if S[h+1][w]!="#": 
            temp.append([h+1,w]) 
            S[h+1][w]="#"

    if w!=W-1: 
        if S[h][w+1]!="#": 
            temp.append([h,w+1]) 
            S[h][w+1]="#"
        
q=[[0,0]] 
count=-1
while True: 
    count+=1
    #print(q)
    temp=[]
    while q:
        l=q.pop(0)
        bfs(l[0],l[1],count)
        
    q=temp[:]
    if len(q)==0:
        print(-1)
        exit()

Buggy python submission (verdict: Runtime Error):
# coding: utf-8
# Your code here!
H,W=map(int,input().split())
S=[]
        
for _ in range(H):
    S.append(list(input()))

def bfs(h,w,num):
    if h==H-1 and w==W-1:
        print(num)
        exit()
    if h!=0:
        if S[h-1][w]!="#":
            temp.append([h-1,w])
    if w!=0:
        if S[h][w-1]!="#":
            temp.append([h,w-1])
    if h!=H:
        if S[h+1][w]!="#":
            temp.append([h+1,w])
    if w!=W:
        if S[h][w+1]!="#":
            temp.append([h,w+1])
    
q=[[0,0]]
temp=[]

while True:
    while q:
        bfs(q.pop(0))
    q=temp[:]




Fixed submission:
# coding: utf-8 
# Your code here! 
H,W=map(int,input().split()) 
S=[] 

ku=0
for _ in range(H): 
    S.append(list(input()))
    ku+=S[-1].count(".")


def bfs(h,w,num):
    if h==H-1 and w==W-1:
        print(ku-num-1)
        exit() 
    else:
        pass

    if h!=0: 
        if S[h-1][w]!="#": 
            temp.append([h-1,w])
            S[h-1][w]="#"

    if w!=0: 
        if S[h][w-1]!="#": 
            temp.append([h,w-1]) 
            S[h][w-1]="#"

    if h!=H-1: 
        if S[h+1][w]!="#": 
            temp.append([h+1,w]) 
            S[h+1][w]="#"

    if w!=W-1: 
        if S[h][w+1]!="#": 
            temp.append([h,w+1]) 
            S[h][w+1]="#"
        
q=[[0,0]] 
count=-1
while True: 
    count+=1
    #print(q)
    temp=[]
    while q:
        l=q.pop(0)
        bfs(l[0],l[1],count)
        
    q=temp[:]
    if len(q)==0:
        print(-1)
        exit()