Buggy python submission (verdict: Time Limit Exceeded):
from collections import deque

h , w = map(int,input().split())
cou = 0
ma = []
for i in range(h):
    kar = list(input())
    cou += kar.count("#")
    ma.append(kar)


ans = [[-1 for i in range(w)] for j in range(h)]
ans[0][0] = 1
q = deque([(0,0)])
xy = [(1,0),(-1,0),(0,1),(0,-1)]

while q:
    x , y = q.popleft()
    ma[x][y] = "#"
    for i in range(4):
        nx = x + xy[i][0]
        ny = y + xy[i][1]
        if 0 <= nx <= h-1 and 0 <= ny <= w-1:
            if ma[nx][ny] == ".":
                q.append((nx,ny))
                ans[nx][ny] = ans[x][y] + 1
                ma[nx][ny] == "#"
        if nx == h-1 and ny == w-1:
            break

if ans[h-1][w-1] == -1:
    print(-1)
else:
    print(h*w - cou - ans[h-1][w-1])


Fixed submission:
from collections import deque
 
h , w = map(int,input().split())
cou = 0
ma = []
for i in range(h):
    kar = list(input())
    cou += kar.count("#")
    ma.append(kar)
 
 
ans = [[-1 for i in range(w)] for j in range(h)]
ans[0][0] = 1
q = deque([(0,0)])
xy = [(1,0),(-1,0),(0,1),(0,-1)]
 
while q:
    x , y = q.popleft()
    for i in range(4):
        nx = x + xy[i][0]
        ny = y + xy[i][1]
        if 0 <= nx <= h-1 and 0 <= ny <= w-1 and ans[nx][ny] == -1:
            if ma[nx][ny] == ".":
                q.append((nx,ny))
                ans[nx][ny] = ans[x][y] + 1
        if nx == h-1 and ny == w-1:
            break
 
if ans[h-1][w-1] == -1:
    print(-1)
else:
    print(h*w - cou - ans[h-1][w-1])

Buggy python submission (verdict: Time Limit Exceeded):
from collections import deque

h , w = map(int,input().split())
cou = 0
ma = []
for i in range(h):
    kar = list(input())
    cou += kar.count("#")
    ma.append(kar)


ans = [[-1 for i in range(w)] for j in range(h)]
ans[0][0] = 1
q = deque([(0,0)])
xy = [(1,0),(-1,0),(0,1),(0,-1)]

while q:
    x , y = q.popleft()
    ma[x][y] = "#"
    for i in range(4):
        nx = x + xy[i][0]
        ny = y + xy[i][1]
        if 0 <= nx <= h-1 and 0 <= ny <= w-1:
            if ma[nx][ny] == ".":
                q.append((nx,ny))
                ans[nx][ny] = ans[x][y] + 1
        if nx == h-1 and ny == w-1:
            break

if ans[h-1][w-1] == -1:
    print(-1)
else:
    print(h*w - cou - ans[h-1][w-1])

Fixed submission:
from collections import deque
 
h , w = map(int,input().split())
cou = 0
ma = []
for i in range(h):
    kar = list(input())
    cou += kar.count("#")
    ma.append(kar)
 
 
ans = [[-1 for i in range(w)] for j in range(h)]
ans[0][0] = 1
q = deque([(0,0)])
xy = [(1,0),(-1,0),(0,1),(0,-1)]
 
while q:
    x , y = q.popleft()
    for i in range(4):
        nx = x + xy[i][0]
        ny = y + xy[i][1]
        if 0 <= nx <= h-1 and 0 <= ny <= w-1 and ans[nx][ny] == -1:
            if ma[nx][ny] == ".":
                q.append((nx,ny))
                ans[nx][ny] = ans[x][y] + 1
        if nx == h-1 and ny == w-1:
            break
 
if ans[h-1][w-1] == -1:
    print(-1)
else:
    print(h*w - cou - ans[h-1][w-1])

Buggy python submission (verdict: Time Limit Exceeded):
from collections import deque

h , w = map(int,input().split())
ma = [["#"]*(w+2)]
cou = 0
for i in range(h):
    kar = ["#"]
    kar.extend(list(input()))
    cou += kar.count("#") - 1
    kar.append("#")
    ma.append(kar)
ma.append(["#"]*(w+2))

ans = [[-1 for i in range(w+2)] for j in range(h+2)]
ans[1][1] = 1
q = deque([(1,1)])
xy = [(1,0),(-1,0),(0,1),(0,-1)]

while q:
    x , y = q.popleft()
    ma[x][y] = "#"
    for i in range(4):
        nx = x + xy[i][0]
        ny = y + xy[i][1]
        if ma[nx][ny] == ".":
            q.append((nx,ny))
            ans[nx][ny] = ans[x][y] + 1
        if nx == h and ny == w:
            break

if ans[h][w] == -1:
    print(-1)
else:
    print(h*w - cou - ans[h][w])

Fixed submission:
from collections import deque
 
h , w = map(int,input().split())
cou = 0
ma = []
for i in range(h):
    kar = list(input())
    cou += kar.count("#")
    ma.append(kar)
 
 
ans = [[-1 for i in range(w)] for j in range(h)]
ans[0][0] = 1
q = deque([(0,0)])
xy = [(1,0),(-1,0),(0,1),(0,-1)]
 
while q:
    x , y = q.popleft()
    for i in range(4):
        nx = x + xy[i][0]
        ny = y + xy[i][1]
        if 0 <= nx <= h-1 and 0 <= ny <= w-1 and ans[nx][ny] == -1:
            if ma[nx][ny] == ".":
                q.append((nx,ny))
                ans[nx][ny] = ans[x][y] + 1
        if nx == h-1 and ny == w-1:
            break
 
if ans[h-1][w-1] == -1:
    print(-1)
else:
    print(h*w - cou - ans[h-1][w-1])

Buggy python submission (verdict: Time Limit Exceeded):
from collections import deque

h , w = map(int,input().split())
ma = [["#"]*(w+2)]
cou = 0
for i in range(h):
    kar = ["#"]
    kar.extend(list(input()))
    cou += kar.count(".")
    kar.append("#")
    ma.append(kar)
ma.append(["#"]*(w+2))

ans = [[-1 for i in range(w+2)] for j in range(h+2)]
ans[1][1] = 1
q = deque([(1,1)])
xy = [(1,0),(-1,0),(0,1),(0,-1)]

while q:
    x , y = q.popleft()
    ma[x][y] = "#"
    for i in range(4):
        nx = x + xy[i][0]
        ny = y + xy[i][1]
        if ma[nx][ny] == ".":
            q.append((nx,ny))
            ans[nx][ny] = ans[x][y] + 1
        if nx == h and ny == w:
            break

if ans[h][w] == -1:
    print(-1)
else:
    print(cou - ans[h][w])

Fixed submission:
from collections import deque
 
h , w = map(int,input().split())
cou = 0
ma = []
for i in range(h):
    kar = list(input())
    cou += kar.count("#")
    ma.append(kar)
 
 
ans = [[-1 for i in range(w)] for j in range(h)]
ans[0][0] = 1
q = deque([(0,0)])
xy = [(1,0),(-1,0),(0,1),(0,-1)]
 
while q:
    x , y = q.popleft()
    for i in range(4):
        nx = x + xy[i][0]
        ny = y + xy[i][1]
        if 0 <= nx <= h-1 and 0 <= ny <= w-1 and ans[nx][ny] == -1:
            if ma[nx][ny] == ".":
                q.append((nx,ny))
                ans[nx][ny] = ans[x][y] + 1
        if nx == h-1 and ny == w-1:
            break
 
if ans[h-1][w-1] == -1:
    print(-1)
else:
    print(h*w - cou - ans[h-1][w-1])

Buggy python submission (verdict: Time Limit Exceeded):
from collections import deque

h , w = map(int,input().split())
ma = [["#"]*(w+2)]
cou = 0
for i in range(h):
    kar = ["#"]
    kar.extend(list(input()))
    cou += kar.count(".")
    kar.append("#")
    ma.append(kar)
ma.append(["#"]*(w+2))

ans = [[-1 for i in range(w+2)] for j in range(h+2)]
ans[1][1] = 1
q = deque([(1,1)])

while q:
    x , y = q.popleft()
    ma[x][y] = "#"
    if ma[x-1][y] == ".":
        q.append((x-1,y))
        ans[x-1][y] = ans[x][y] + 1
    if ma[x+1][y] == ".":
        q.append((x+1,y))
        ans[x+1][y] = ans[x][y] + 1        
    if ma[x][y-1] == ".":
        q.append((x,y-1))
        ans[x][y-1] = ans[x][y] + 1        
    if ma[x][y+1] == ".":
        q.append((x,y+1))
        ans[x][y+1] = ans[x][y] + 1

if ans[h][w] == -1:
    print(-1)
else:
    print(cou - ans[h][w])


Fixed submission:
from collections import deque
 
h , w = map(int,input().split())
cou = 0
ma = []
for i in range(h):
    kar = list(input())
    cou += kar.count("#")
    ma.append(kar)
 
 
ans = [[-1 for i in range(w)] for j in range(h)]
ans[0][0] = 1
q = deque([(0,0)])
xy = [(1,0),(-1,0),(0,1),(0,-1)]
 
while q:
    x , y = q.popleft()
    for i in range(4):
        nx = x + xy[i][0]
        ny = y + xy[i][1]
        if 0 <= nx <= h-1 and 0 <= ny <= w-1 and ans[nx][ny] == -1:
            if ma[nx][ny] == ".":
                q.append((nx,ny))
                ans[nx][ny] = ans[x][y] + 1
        if nx == h-1 and ny == w-1:
            break
 
if ans[h-1][w-1] == -1:
    print(-1)
else:
    print(h*w - cou - ans[h-1][w-1])

Buggy python submission (verdict: Time Limit Exceeded):
from collections import deque

h , w = map(int,input().split())
ma = [["#"]*(w+2)]
cou = 0
for i in range(h):
    kar = ["#"]
    kar.extend(list(input()))
    cou += kar.count(".")
    kar.append("#")
    ma.append(kar)
ma.append(["#"]*(w+2))

ans = [[-1 for i in range(w+2)] for j in range(h+2)]
ans[1][1] = 1
q = deque([(1,1)])

while q:
    x , y = q.popleft()
    ma[x][y] = "#"
    if ma[x-1][y] == ".":
        q.append((x-1,y))
        ans[x-1][y] = ans[x][y] + 1
    if ma[x+1][y] == ".":
        q.append((x+1,y))
        ans[x+1][y] = ans[x][y] + 1        
    if ma[x][y-1] == ".":
        q.append((x,y-1))
        ans[x][y-1] = ans[x][y] + 1        
    if ma[x][y+1] == ".":
        q.append((x,y+1))
        ans[x][y+1] = ans[x][y] + 1

if ans[h][w] == -1:
    print(-1)
else:
    print(cou - ans[h][w])

Fixed submission:
from collections import deque
 
h , w = map(int,input().split())
cou = 0
ma = []
for i in range(h):
    kar = list(input())
    cou += kar.count("#")
    ma.append(kar)
 
 
ans = [[-1 for i in range(w)] for j in range(h)]
ans[0][0] = 1
q = deque([(0,0)])
xy = [(1,0),(-1,0),(0,1),(0,-1)]
 
while q:
    x , y = q.popleft()
    for i in range(4):
        nx = x + xy[i][0]
        ny = y + xy[i][1]
        if 0 <= nx <= h-1 and 0 <= ny <= w-1 and ans[nx][ny] == -1:
            if ma[nx][ny] == ".":
                q.append((nx,ny))
                ans[nx][ny] = ans[x][y] + 1
        if nx == h-1 and ny == w-1:
            break
 
if ans[h-1][w-1] == -1:
    print(-1)
else:
    print(h*w - cou - ans[h-1][w-1])