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

def bfs(H,W,S):
    qu = deque([(0,0,1)])
    while(len(qu) != 0):
        p,q,r = qu.popleft()
        S[p][q] = '#'
        for d in [(-1,0), (0,-1), (1,0), (0,1)]:
            s = p+d[0]
            t = q+d[1]
            if (s < 0 or t < 0 or s >= H or t >= W):
                continue
            if (s == (H-1) and t == (W-1) and S[s][t] == '.'):
                return r+1
            if S[s][t] == '.':
                qu.append((s,t,r+1))
    return -1

H, W = list(map(int, input().split()))
S = []

cnt = 0
for i in range(H):
    a = input()
    tmp = []
    for i in a:
        if (i == '.'):
            cnt += 1
        tmp.append(i)
    S.append(tmp)

step = bfs(H, W, S)

if (step != -1):
    print(cnt - step)
else:
    print(step)


Fixed submission:
from collections import deque

def bfs(H,W,S):
	V = [[False for i in range(W)] for j in range(H)]
	qu = deque([(0,0,1)])
	V[0][0] = True
	while(qu):
		p,q,r = qu.popleft()
		S[p][q] = '#'
		for d in [(-1,0), (0,-1), (1,0), (0,1)]:
			s = p+d[0]
			t = q+d[1]
			if s < 0 or t < 0 or s >= H or t >= W:
				continue
			if s == (H-1) and t == (W-1) and S[s][t] == '.':
				return r+1
			if S[s][t] == '.' and V[s][t] == False:
				V[s][t] = True
				qu.append((s,t,r+1))
	return -1

H, W = list(map(int, input().split()))
S = []
cnt = 0
for i in range(H):
	a = str(input())
	tmp = []
	for i in a:
		if (i == '.'):
			cnt += 1
		tmp.append(i)
	S.append(tmp)

step = bfs(H, W, S)

if (step != -1):
	print(cnt - step)
else:
	print(step)

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

def bfs(H,W,S):
    qu = deque([(0,0,1)])
    while(len(qu) != 0):
        p,q,r = qu.popleft()
        S[p][q] = '#'
        for d in [(-1,0), (0,-1), (1,0), (0,1)]:
            s = p+d[0]
            t = q+d[1]
            if (s < 0 or t < 0 or s >= H or t >= W):
                continue
            if (s == (H-1) and t == (W-1)):
                return r+1
            if S[s][t] == '.':
                qu.append((s,t,r+1))
    return -1

H, W = list(map(int, input().split()))
S = []

cnt = 0
for i in range(H):
    a = input()
    tmp = []
    for i in a:
        if (i == '.'):
            cnt += 1
        tmp.append(i)
    S.append(tmp)

step = bfs(H, W, S)

if (step != -1):
    print(cnt - step)
else:
    print(step)

Fixed submission:
from collections import deque

def bfs(H,W,S):
	V = [[False for i in range(W)] for j in range(H)]
	qu = deque([(0,0,1)])
	V[0][0] = True
	while(qu):
		p,q,r = qu.popleft()
		S[p][q] = '#'
		for d in [(-1,0), (0,-1), (1,0), (0,1)]:
			s = p+d[0]
			t = q+d[1]
			if s < 0 or t < 0 or s >= H or t >= W:
				continue
			if s == (H-1) and t == (W-1) and S[s][t] == '.':
				return r+1
			if S[s][t] == '.' and V[s][t] == False:
				V[s][t] = True
				qu.append((s,t,r+1))
	return -1

H, W = list(map(int, input().split()))
S = []
cnt = 0
for i in range(H):
	a = str(input())
	tmp = []
	for i in a:
		if (i == '.'):
			cnt += 1
		tmp.append(i)
	S.append(tmp)

step = bfs(H, W, S)

if (step != -1):
	print(cnt - step)
else:
	print(step)

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

def bfs(S):
    qu = deque([(0,0,1)])
    while(len(qu) != 0):
        a = qu.popleft()
        p,q,r = a
        S[p][q] = 'a'
        for d in [(-1,0), (0,-1), (1,0), (0,1)]:
            dp,dq = d
            s = p+dp
            t = q+dq
            if (s < 0 or t < 0 or s >= H or t >= W):
                continue

            if (s == (H-1) and t == (W-1)):
                return r+1
            if S[s][t] == '.':
                qu.append((s,t,r+1))
    return -1

H, W = list(map(int, input().split()))
S = []

cnt = 0
for i in range(H):
    a = input()
    tmp = []
    for i in a:
        if (i == '.'):
            cnt += 1
        tmp.append(i)
    S.append(tmp)

step = bfs(S)

if (step != -1):
    print(cnt - step)
else:
    print(step)

Fixed submission:
from collections import deque

def bfs(H,W,S):
	V = [[False for i in range(W)] for j in range(H)]
	qu = deque([(0,0,1)])
	V[0][0] = True
	while(qu):
		p,q,r = qu.popleft()
		S[p][q] = '#'
		for d in [(-1,0), (0,-1), (1,0), (0,1)]:
			s = p+d[0]
			t = q+d[1]
			if s < 0 or t < 0 or s >= H or t >= W:
				continue
			if s == (H-1) and t == (W-1) and S[s][t] == '.':
				return r+1
			if S[s][t] == '.' and V[s][t] == False:
				V[s][t] = True
				qu.append((s,t,r+1))
	return -1

H, W = list(map(int, input().split()))
S = []
cnt = 0
for i in range(H):
	a = str(input())
	tmp = []
	for i in a:
		if (i == '.'):
			cnt += 1
		tmp.append(i)
	S.append(tmp)

step = bfs(H, W, S)

if (step != -1):
	print(cnt - step)
else:
	print(step)