Buggy python submission (verdict: Time Limit Exceeded):
import sys
import copy

h, w = map(int, sys.stdin.readline().split())

maps = [list(sys.stdin.readline().rstrip()) for _ in range(h)]

def calcdist(maps, start, end, h, w):
    nextlist = []
    nextlist.append(start)
    count = 1
    while True:
        nexnex = []
        for i in nextlist:
            if i[0] == end[0] and i[1] == end[1]:
                return count
            if i[0] > 0 and maps[i[0] -1][i[1]] == '.':
                maps[i[0] -1][i[1]] = '#'
                nexnex.append([i[0] -1, i[1]])
            if i[0] < h-1 and maps[i[0] +1][i[1]] == '.':
                maps[i[0] +1][i[1]] = '#'
                nexnex.append([i[0] +1, i[1]])
            if i[1] > 0 and maps[i[0]][i[1]-1] == '.':
                maps[i[0]][i[1]-1] = '#'
                nexnex.append([i[0], i[1]-1])
            if i[1] < w-1 and maps[i[0]][i[1]+1] == '.':
                maps[i[0]][i[1]+1] = '#'
                nexnex.append([i[0], i[1]+1])
        nextlist = nexnex
        count += 1
    return count

kabe = 0

for i in range(h):
    for j in range(w):
        if maps[i][j] == '#':
            kabe += 1

total = 0
dist = calcdist(maps,[0, 0], [h-1, w-1], h, w)

print(h*w - dist - kabe)


Fixed submission:
import sys
import copy

h, w = map(int, sys.stdin.readline().split())

maps = [list(sys.stdin.readline().rstrip()) for _ in range(h)]

def calcdist(maps, start, end, h, w):
    nextlist = []
    nextlist.append(start)
    count = 1
    while True:
        nexnex = []
        for i in nextlist:
            if i[0] == end[0] and i[1] == end[1]:
                return count
            if i[0] > 0 and maps[i[0] -1][i[1]] == '.':
                maps[i[0] -1][i[1]] = '#'
                nexnex.append([i[0] -1, i[1]])
            if i[0] < h-1 and maps[i[0] +1][i[1]] == '.':
                maps[i[0] +1][i[1]] = '#'
                nexnex.append([i[0] +1, i[1]])
            if i[1] > 0 and maps[i[0]][i[1]-1] == '.':
                maps[i[0]][i[1]-1] = '#'
                nexnex.append([i[0], i[1]-1])
            if i[1] < w-1 and maps[i[0]][i[1]+1] == '.':
                maps[i[0]][i[1]+1] = '#'
                nexnex.append([i[0], i[1]+1])
        nextlist = nexnex
        count += 1
        if len(nexnex) == 0:
            return -1
    return count

kabe = 0

for i in range(h):
    for j in range(w):
        if maps[i][j] == '#':
            kabe += 1

total = 0
dist = calcdist(maps,[0, 0], [h-1, w-1], h, w)
if dist == -1:
    print(-1)
else:
    print(h*w - dist - kabe)

Buggy python submission (verdict: Time Limit Exceeded):
import sys
import copy

h, w = map(int, sys.stdin.readline().split())

maps = [list(sys.stdin.readline().rstrip()) for _ in range(h)]

tmaps = [[0] * w for _ in range(h)]

def calcdist(tmaps, start, end, h, w):
    nextlist = []
    nextlist.append(start)
    count = 1
    while True:
        nexnex = []
        for i in nextlist:
            tmaps[i[0]][i[1]] = count
            if i[0] == end[0] and i[1] == end[1]:
                return count
        for i in nextlist:
            if i[0] > 0 and tmaps[i[0] -1][i[1]] == 0:
                nexnex.append([i[0] -1, i[1]])
            if i[0] < h-1 and tmaps[i[0] +1][i[1]] == 0:
                nexnex.append([i[0] +1, i[1]])
            if i[1] > 0 and tmaps[i[0]][i[1]-1] == 0:
                nexnex.append([i[0], i[1]-1])
            if i[1] < w-1 and tmaps[i[0]][i[1]+1] == 0:
                nexnex.append([i[0], i[1]+1])
        nextlist = list(map(list, set(map(tuple, nexnex))))
        count += 1
    return count

kabe = 0

for i in range(h):
    for j in range(w):
        if maps[i][j] == '#':
            tmaps[i][j] = -1
            kabe += 1

total = 0
dist = calcdist(copy.deepcopy(tmaps),[0, 0], [h-1, w-1], h, w)

print(h*w - dist - kabe)


Fixed submission:
import sys
import copy

h, w = map(int, sys.stdin.readline().split())

maps = [list(sys.stdin.readline().rstrip()) for _ in range(h)]

def calcdist(maps, start, end, h, w):
    nextlist = []
    nextlist.append(start)
    count = 1
    while True:
        nexnex = []
        for i in nextlist:
            if i[0] == end[0] and i[1] == end[1]:
                return count
            if i[0] > 0 and maps[i[0] -1][i[1]] == '.':
                maps[i[0] -1][i[1]] = '#'
                nexnex.append([i[0] -1, i[1]])
            if i[0] < h-1 and maps[i[0] +1][i[1]] == '.':
                maps[i[0] +1][i[1]] = '#'
                nexnex.append([i[0] +1, i[1]])
            if i[1] > 0 and maps[i[0]][i[1]-1] == '.':
                maps[i[0]][i[1]-1] = '#'
                nexnex.append([i[0], i[1]-1])
            if i[1] < w-1 and maps[i[0]][i[1]+1] == '.':
                maps[i[0]][i[1]+1] = '#'
                nexnex.append([i[0], i[1]+1])
        nextlist = nexnex
        count += 1
        if len(nexnex) == 0:
            return -1
    return count

kabe = 0

for i in range(h):
    for j in range(w):
        if maps[i][j] == '#':
            kabe += 1

total = 0
dist = calcdist(maps,[0, 0], [h-1, w-1], h, w)
if dist == -1:
    print(-1)
else:
    print(h*w - dist - kabe)