Buggy python submission (verdict: Time Limit Exceeded):
from collections import deque
#
# H, W = 10, 37
#
# ARR = [
#     ".....................................",
#     "...#...####...####..###...###...###..",
#     "..#.#..#...#.##....#...#.#...#.#...#.",
#     "..#.#..#...#.#.....#...#.#...#.#...#.",
#     ".#...#.#..##.#.....#...#.#.###.#.###.",
#     ".#####.####..#.....#...#..##....##...",
#     ".#...#.#...#.#.....#...#.#...#.#...#.",
#     ".#...#.#...#.##....#...#.#...#.#...#.",
#     ".#...#.####...####..###...###...###..",
#     ".....................................",
# ]

#

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

for i in range(H):
    ARR.append(list(input()))


def calculateDistance(h, w, arr):
    hist = [[0 for j in range(W)] for i in range(H)]

    dist = [[0 for j in range(W)] for i in range(H)]

    dist[0][0] = 1
    q = deque()

    q.append((0, 0))

    while q.__len__() > 0:
        x, y = q.popleft()
        hist[y][x] = 1

        if x == w - 1 and y == h - 1:
            distance = dist[y][x]
            return calculate(h, w,distance, arr)

        # left
        if x > 0 and (hist[y][x - 1] == 0) and (arr[y][x - 1] == "."):
            dist[y][x-1] = dist[y][x] + 1
            q.append((x - 1, y))

        # right
        if x < w - 1 and (hist[y][x + 1] == 0) and (arr[y][x + 1] == "."):
            dist[y][x+1] = dist[y][x] + 1
            q.append((x + 1, y))

        # up
        if y > 0 and (hist[y - 1][x] == 0) and (arr[y - 1][x] == "."):
            dist[y-1][x] = dist[y][x] + 1
            q.append((x, y - 1))

        # down
        if y < h - 1 and (hist[y + 1][x] == 0) and (arr[y + 1][x] == "."):
            dist[y+1][x] = dist[y][x] + 1
            q.append((x, y + 1))

    return -1


def calculate(h, w, distance, arr):
    total = h * w
    alreadyPaiting = 0

    for s in arr:
        for ss in s:
            if ss == "#":
                alreadyPaiting += 1

    return total - distance - alreadyPaiting


res = calculateDistance(H, W, ARR)

print(res)


Fixed submission:
from collections import deque

H, W = 10, 37
ARR = [
    ".....................................",
    "...#...####...####..###...###...###..",
    "..#.#..#...#.##....#...#.#...#.#...#.",
    "..#.#..#...#.#.....#...#.#...#.#...#.",
    ".#...#.#..##.#.....#...#.#.###.#.###.",
    ".#####.####..#.....#...#..##....##...",
    ".#...#.#...#.#.....#...#.#...#.#...#.",
    ".#...#.#...#.##....#...#.#...#.#...#.",
    ".#...#.####...####..###...###...###..",
    ".....................................",
]

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

for i in range(H):
    ARR.append(input())


def calculate(h, w, arr):
    q = deque()

    q.append((0, 0))

    dx = [0, 1, 0, -1]
    dy = [-1, 0, 1, 0]

    distance = [[0 for i in range(w)] for j in range(h)]
    distance[0][0] = 1
    while q.__len__() > 0:
        y, x = q.popleft()
        for i in range(4):
            my = y + dy[i]
            mx = x + dx[i]
            if (my >= 0) and (mx >= 0) and (my <= (h - 1)) and (mx <= (w - 1)) and (distance[my][mx] == 0) and (
                    arr[my][mx] == "."):
                distance[my][mx] = distance[y][x] + 1
                q.append((my, mx))

    mmk = 0
    for j in range(h):
        mmk += arr[j].count("#")

    if distance[h - 1][w - 1] == 0:
        print(-1)
    else:
        print(h * w - distance[h - 1][w - 1] - mmk)


calculate(H, W, ARR)

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

H, W = 10, 37

ARR = [
    ".....................................",
    "...#...####...####..###...###...###..",
    "..#.#..#...#.##....#...#.#...#.#...#.",
    "..#.#..#...#.#.....#...#.#...#.#...#.",
    ".#...#.#..##.#.....#...#.#.###.#.###.",
    ".#####.####..#.....#...#..##....##...",
    ".#...#.#...#.#.....#...#.#...#.#...#.",
    ".#...#.#...#.##....#...#.#...#.#...#.",
    ".#...#.####...####..###...###...###..",
    ".....................................",
]



#

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

for i in range(H):
    ARR.append(list(input()))


def calculateDistance(h, w, arr):
    hist = [[0 for j in range(W)] for i in range(H)]

    q = deque()

    q.append((0, 0, 1))

    while q.__len__() > 0:
        x, y,currentStep = q.popleft()
        hist[y][x] = 1
        if x == w - 1 and y == h - 1:
            return calculate(h, w, currentStep, arr)

        # left
        if x > 0 and (hist[y][x - 1] == 0) and (arr[y][x - 1] == "."):
            q.append((x - 1, y, currentStep+1))

        # right
        if x < w - 1 and (hist[y][x + 1] == 0) and (arr[y][x + 1] == "."):
            q.append((x + 1, y, currentStep+1))

        # up
        if y > 0 and (hist[y - 1][x] == 0) and (arr[y - 1][x] == "."):
            q.append((x, y - 1, currentStep+1))

        # down
        if y < h - 1 and (hist[y + 1][x] == 0) and (arr[y + 1][x] == "."):
            q.append((x, y + 1, currentStep+1))


    return -1


def calculate(h, w, distance, arr):
    total = h * w
    alreadyPaiting = 0

    for s in arr:
        for ss in s:
            if ss == "#":
                alreadyPaiting += 1

    return total - distance - alreadyPaiting


res = calculateDistance(H, W, ARR)

print(res)


Fixed submission:
from collections import deque

H, W = 10, 37
ARR = [
    ".....................................",
    "...#...####...####..###...###...###..",
    "..#.#..#...#.##....#...#.#...#.#...#.",
    "..#.#..#...#.#.....#...#.#...#.#...#.",
    ".#...#.#..##.#.....#...#.#.###.#.###.",
    ".#####.####..#.....#...#..##....##...",
    ".#...#.#...#.#.....#...#.#...#.#...#.",
    ".#...#.#...#.##....#...#.#...#.#...#.",
    ".#...#.####...####..###...###...###..",
    ".....................................",
]

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

for i in range(H):
    ARR.append(input())


def calculate(h, w, arr):
    q = deque()

    q.append((0, 0))

    dx = [0, 1, 0, -1]
    dy = [-1, 0, 1, 0]

    distance = [[0 for i in range(w)] for j in range(h)]
    distance[0][0] = 1
    while q.__len__() > 0:
        y, x = q.popleft()
        for i in range(4):
            my = y + dy[i]
            mx = x + dx[i]
            if (my >= 0) and (mx >= 0) and (my <= (h - 1)) and (mx <= (w - 1)) and (distance[my][mx] == 0) and (
                    arr[my][mx] == "."):
                distance[my][mx] = distance[y][x] + 1
                q.append((my, mx))

    mmk = 0
    for j in range(h):
        mmk += arr[j].count("#")

    if distance[h - 1][w - 1] == 0:
        print(-1)
    else:
        print(h * w - distance[h - 1][w - 1] - mmk)


calculate(H, W, ARR)

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

#
# H, W = 10, 37
#
# ARR = [
#     ".....................................",
#     "...#...####...####..###...###...###..",
#     "..#.#..#...#.##....#...#.#...#.#...#.",
#     "..#.#..#...#.#.....#...#.#...#.#...#.",
#     ".#...#.#..##.#.....#...#.#.###.#.###.",
#     ".#####.####..#.....#...#..##....##...",
#     ".#...#.#...#.#.....#...#.#...#.#...#.",
#     ".#...#.#...#.##....#...#.#...#.#...#.",
#     ".#...#.####...####..###...###...###..",
#     ".....................................",
# ]


#

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

for i in range(H):
    ARR.append(list(input()))


def calculateDistance(h, w, arr):
    hist = [[0 for j in range(W)] for i in range(H)]

    q = deque()

    q.append([0, 0, 0, hist])

    distances = []

    while q.__len__() > 0:

        l = q.popleft()
        x = l[0]
        y = l[1]
        currentStep = l[2]
        hist = l[3]
        hist[y][x] = 1


        if x == w - 1 and y == h - 1:
            distances.append(currentStep + 1)

        # left
        if x > 0 and (hist[y][x - 1] == 0) and (arr[y][x - 1] == "."):
            q.append([x - 1, y, currentStep + 1, hist])

        # right
        if x < w - 1 and (hist[y][x + 1] == 0) and (arr[y][x + 1] == "."):
            q.append([x + 1, y, currentStep + 1, hist])

        # up
        if y > 0 and (hist[y - 1][x] == 0) and (arr[y - 1][x] == "."):
            q.append([x, y - 1, currentStep + 1, hist])

        # down
        if y < h - 1 and (hist[y + 1][x] == 0) and (arr[y + 1][x] == "."):
            q.append([x, y + 1, currentStep + 1, hist])

    if distances.__len__() == 0:
        return -1
    else:
        return calculate(h, w, min(distances), arr)


def calculate(h, w, distance, arr):
    total = h * w
    alreadyPaiting = 0

    for s in arr:
        for ss in s:
            if ss == "#":
                alreadyPaiting += 1

    return total - distance - alreadyPaiting


res = calculateDistance(H, W, ARR)

print(res)


Fixed submission:
from collections import deque

H, W = 10, 37
ARR = [
    ".....................................",
    "...#...####...####..###...###...###..",
    "..#.#..#...#.##....#...#.#...#.#...#.",
    "..#.#..#...#.#.....#...#.#...#.#...#.",
    ".#...#.#..##.#.....#...#.#.###.#.###.",
    ".#####.####..#.....#...#..##....##...",
    ".#...#.#...#.#.....#...#.#...#.#...#.",
    ".#...#.#...#.##....#...#.#...#.#...#.",
    ".#...#.####...####..###...###...###..",
    ".....................................",
]

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

for i in range(H):
    ARR.append(input())


def calculate(h, w, arr):
    q = deque()

    q.append((0, 0))

    dx = [0, 1, 0, -1]
    dy = [-1, 0, 1, 0]

    distance = [[0 for i in range(w)] for j in range(h)]
    distance[0][0] = 1
    while q.__len__() > 0:
        y, x = q.popleft()
        for i in range(4):
            my = y + dy[i]
            mx = x + dx[i]
            if (my >= 0) and (mx >= 0) and (my <= (h - 1)) and (mx <= (w - 1)) and (distance[my][mx] == 0) and (
                    arr[my][mx] == "."):
                distance[my][mx] = distance[y][x] + 1
                q.append((my, mx))

    mmk = 0
    for j in range(h):
        mmk += arr[j].count("#")

    if distance[h - 1][w - 1] == 0:
        print(-1)
    else:
        print(h * w - distance[h - 1][w - 1] - mmk)


calculate(H, W, ARR)

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

#
# H, W = 10, 37
#
# ARR = [
#     ".....................................",
#     "...#...####...####..###...###...###..",
#     "..#.#..#...#.##....#...#.#...#.#...#.",
#     "..#.#..#...#.#.....#...#.#...#.#...#.",
#     ".#...#.#..##.#.....#...#.#.###.#.###.",
#     ".#####.####..#.....#...#..##....##...",
#     ".#...#.#...#.#.....#...#.#...#.#...#.",
#     ".#...#.#...#.##....#...#.#...#.#...#.",
#     ".#...#.####...####..###...###...###..",
#     ".....................................",
# ]


#

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

for i in range(H):
    ARR.append(list(input()))


#
# H, W = 20, 37
#
# ARR = [
#     ".....................................",
#     "...#...####...####..###...###...###..",
#     "..#.#..#...#.##....#...#.#...#.#...#.",
#     "..#.#..#...#.#.....#...#.#...#.#...#.",
#     ".#...#.#..##.#.....#...#.#.###.#.###.",
#     ".#####.####..#.....#...#..##....##...",
#     ".#...#.#...#.#.....#...#.#...#.#...#.",
#     ".#...#.#...#.##....#...#.#...#.#...#.",
#     ".#...#.####...####..###...###...###..",
#     ".....................................",
#     ".....................................",
#     ".....................................",
#     ".....................................",
#     ".....................................",
#     ".....................................",
#     ".....................................",
#     ".....................................",
#     ".....................................",
#     ".....................................",
#     ".....................................",
# ]



def calculateDistance(h, w, arr):
    hist = [[0 for j in range(W)] for i in range(H)]

    q = deque()

    q.append((0, 0, 0))

    distances = []

    while q.__len__() > 0:
        x, y, currentStep = q.popleft()
        hist[y][x] = 1

        if x == w - 1 and y == h - 1:
            distances.append(currentStep + 1)

            if currentStep + 1 <= h + w:
                break

        # left
        if x > 0 and (hist[y][x - 1] == 0) and (arr[y][x - 1] == "."):
            q.append((x - 1, y, currentStep + 1))

        # right
        if x < w - 1 and (hist[y][x + 1] == 0) and (arr[y][x + 1] == "."):
            q.append((x + 1, y, currentStep + 1))

        # up
        if y > 0 and (hist[y - 1][x] == 0) and (arr[y - 1][x] == "."):
            q.append((x, y - 1, currentStep + 1))

        # down
        if y < h - 1 and (hist[y + 1][x] == 0) and (arr[y + 1][x] == "."):
            q.append((x, y + 1, currentStep + 1))

    if distances.__len__() == 0:
        return -1
    else:
        return calculate(h, w, min(distances), arr)


def calculate(h, w, distance, arr):
    total = h * w
    alreadyPaiting = 0

    for s in arr:
        for ss in s:
            if ss == "#":
                alreadyPaiting += 1

    return total - distance - alreadyPaiting


res = calculateDistance(H, W, ARR)

print(res)


Fixed submission:
from collections import deque

H, W = 10, 37
ARR = [
    ".....................................",
    "...#...####...####..###...###...###..",
    "..#.#..#...#.##....#...#.#...#.#...#.",
    "..#.#..#...#.#.....#...#.#...#.#...#.",
    ".#...#.#..##.#.....#...#.#.###.#.###.",
    ".#####.####..#.....#...#..##....##...",
    ".#...#.#...#.#.....#...#.#...#.#...#.",
    ".#...#.#...#.##....#...#.#...#.#...#.",
    ".#...#.####...####..###...###...###..",
    ".....................................",
]

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

for i in range(H):
    ARR.append(input())


def calculate(h, w, arr):
    q = deque()

    q.append((0, 0))

    dx = [0, 1, 0, -1]
    dy = [-1, 0, 1, 0]

    distance = [[0 for i in range(w)] for j in range(h)]
    distance[0][0] = 1
    while q.__len__() > 0:
        y, x = q.popleft()
        for i in range(4):
            my = y + dy[i]
            mx = x + dx[i]
            if (my >= 0) and (mx >= 0) and (my <= (h - 1)) and (mx <= (w - 1)) and (distance[my][mx] == 0) and (
                    arr[my][mx] == "."):
                distance[my][mx] = distance[y][x] + 1
                q.append((my, mx))

    mmk = 0
    for j in range(h):
        mmk += arr[j].count("#")

    if distance[h - 1][w - 1] == 0:
        print(-1)
    else:
        print(h * w - distance[h - 1][w - 1] - mmk)


calculate(H, W, ARR)

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

#
# H, W = 10, 37
#
# ARR = [
#     ".....................................",
#     "...#...####...####..###...###...###..",
#     "..#.#..#...#.##....#...#.#...#.#...#.",
#     "..#.#..#...#.#.....#...#.#...#.#...#.",
#     ".#...#.#..##.#.....#...#.#.###.#.###.",
#     ".#####.####..#.....#...#..##....##...",
#     ".#...#.#...#.#.....#...#.#...#.#...#.",
#     ".#...#.#...#.##....#...#.#...#.#...#.",
#     ".#...#.####...####..###...###...###..",
#     ".....................................",
# ]


#

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

for i in range(H):
    ARR.append(list(input()))


def calculateDistance(h, w, arr):
    hist = [[0 for j in range(W)] for i in range(H)]

    q = deque()

    q.append((0, 0, 0, hist))

    distances = []

    while q.__len__() > 0:
        x, y, currentStep, hist = q.popleft()
        hist[y][x] = 1


        if x == w - 1 and y == h - 1:
            distances.append(currentStep + 1)

        # left
        if x > 0 and (hist[y][x - 1] == 0) and (arr[y][x - 1] == "."):
            q.append((x - 1, y, currentStep + 1, hist))

        # right
        if x < w - 1 and (hist[y][x + 1] == 0) and (arr[y][x + 1] == "."):
            q.append((x + 1, y, currentStep + 1, hist))

        # up
        if y > 0 and (hist[y - 1][x] == 0) and (arr[y - 1][x] == "."):
            q.append((x, y - 1, currentStep + 1, hist))

        # down
        if y < h - 1 and (hist[y + 1][x] == 0) and (arr[y + 1][x] == "."):
            q.append((x, y + 1, currentStep + 1, hist))

    if distances.__len__() == 0:
        return -1
    else:
        return calculate(h, w, min(distances), arr)


def calculate(h, w, distance, arr):
    total = h * w
    alreadyPaiting = 0

    for s in arr:
        for ss in s:
            if ss == "#":
                alreadyPaiting += 1

    return total - distance - alreadyPaiting


res = calculateDistance(H, W, ARR)

print(res)


Fixed submission:
from collections import deque

H, W = 10, 37
ARR = [
    ".....................................",
    "...#...####...####..###...###...###..",
    "..#.#..#...#.##....#...#.#...#.#...#.",
    "..#.#..#...#.#.....#...#.#...#.#...#.",
    ".#...#.#..##.#.....#...#.#.###.#.###.",
    ".#####.####..#.....#...#..##....##...",
    ".#...#.#...#.#.....#...#.#...#.#...#.",
    ".#...#.#...#.##....#...#.#...#.#...#.",
    ".#...#.####...####..###...###...###..",
    ".....................................",
]

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

for i in range(H):
    ARR.append(input())


def calculate(h, w, arr):
    q = deque()

    q.append((0, 0))

    dx = [0, 1, 0, -1]
    dy = [-1, 0, 1, 0]

    distance = [[0 for i in range(w)] for j in range(h)]
    distance[0][0] = 1
    while q.__len__() > 0:
        y, x = q.popleft()
        for i in range(4):
            my = y + dy[i]
            mx = x + dx[i]
            if (my >= 0) and (mx >= 0) and (my <= (h - 1)) and (mx <= (w - 1)) and (distance[my][mx] == 0) and (
                    arr[my][mx] == "."):
                distance[my][mx] = distance[y][x] + 1
                q.append((my, mx))

    mmk = 0
    for j in range(h):
        mmk += arr[j].count("#")

    if distance[h - 1][w - 1] == 0:
        print(-1)
    else:
        print(h * w - distance[h - 1][w - 1] - mmk)


calculate(H, W, ARR)

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

#
# H, W = 10, 37
#
# ARR = [
#     ".....................................",
#     "...#...####...####..###...###...###..",
#     "..#.#..#...#.##....#...#.#...#.#...#.",
#     "..#.#..#...#.#.....#...#.#...#.#...#.",
#     ".#...#.#..##.#.....#...#.#.###.#.###.",
#     ".#####.####..#.....#...#..##....##...",
#     ".#...#.#...#.#.....#...#.#...#.#...#.",
#     ".#...#.#...#.##....#...#.#...#.#...#.",
#     ".#...#.####...####..###...###...###..",
#     ".....................................",
# ]


#

ALREADYPAITING = 0
H, W = map(int, input().split())
ARR = []

for i in range(H):
    l = input()
    ALREADYPAITING += l.count("#")
    ARR.append(l)


def calculateDistance(h, w, arr):
    hist = [[0 for j in range(W)] for i in range(H)]

    q = deque()

    q.append((0, 0, 0, hist))

    distances = []

    while q.__len__() > 0:
        x, y, currentStep, hist = q.popleft()

        if x == w - 1 and y == h - 1:
            distances.append(currentStep + 1)

        hist[y][x] = 1

        # left
        if x > 0 and (hist[y][x - 1] == 0) and (arr[y][x - 1] == "."):
            q.append((x - 1, y, currentStep + 1, hist))

        # right
        if x < w - 1 and (hist[y][x + 1] == 0) and (arr[y][x + 1] == "."):
            q.append((x + 1, y, currentStep + 1, hist))

        # up
        if y > 0 and (hist[y - 1][x] == 0) and (arr[y - 1][x] == "."):
            q.append((x, y - 1, currentStep + 1, hist))

        # down
        if y < h - 1 and (hist[y + 1][x] == 0) and (arr[y + 1][x] == "."):
            q.append((x, y + 1, currentStep + 1, hist))

    if distances.__len__() == 0:
        return -1
    else:
        return calculate(h, w, min(distances))


def calculate(h, w, distance):
    total = h * w

    # for s in arr:
    #     for ss in s:
    #         if ss == "#":
    #             alreadyPaiting += 1

    return total - distance - ALREADYPAITING


res = calculateDistance(H, W, ARR)

print(res)


Fixed submission:
from collections import deque

H, W = 10, 37
ARR = [
    ".....................................",
    "...#...####...####..###...###...###..",
    "..#.#..#...#.##....#...#.#...#.#...#.",
    "..#.#..#...#.#.....#...#.#...#.#...#.",
    ".#...#.#..##.#.....#...#.#.###.#.###.",
    ".#####.####..#.....#...#..##....##...",
    ".#...#.#...#.#.....#...#.#...#.#...#.",
    ".#...#.#...#.##....#...#.#...#.#...#.",
    ".#...#.####...####..###...###...###..",
    ".....................................",
]

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

for i in range(H):
    ARR.append(input())


def calculate(h, w, arr):
    q = deque()

    q.append((0, 0))

    dx = [0, 1, 0, -1]
    dy = [-1, 0, 1, 0]

    distance = [[0 for i in range(w)] for j in range(h)]
    distance[0][0] = 1
    while q.__len__() > 0:
        y, x = q.popleft()
        for i in range(4):
            my = y + dy[i]
            mx = x + dx[i]
            if (my >= 0) and (mx >= 0) and (my <= (h - 1)) and (mx <= (w - 1)) and (distance[my][mx] == 0) and (
                    arr[my][mx] == "."):
                distance[my][mx] = distance[y][x] + 1
                q.append((my, mx))

    mmk = 0
    for j in range(h):
        mmk += arr[j].count("#")

    if distance[h - 1][w - 1] == 0:
        print(-1)
    else:
        print(h * w - distance[h - 1][w - 1] - mmk)


calculate(H, W, ARR)

Buggy python submission (verdict: Time Limit Exceeded):
from collections import deque
#
# H, W = 10, 37
#
# ARR = [
#     ".....................................",
#     "...#...####...####..###...###...###..",
#     "..#.#..#...#.##....#...#.#...#.#...#.",
#     "..#.#..#...#.#.....#...#.#...#.#...#.",
#     ".#...#.#..##.#.....#...#.#.###.#.###.",
#     ".#####.####..#.....#...#..##....##...",
#     ".#...#.#...#.#.....#...#.#...#.#...#.",
#     ".#...#.#...#.##....#...#.#...#.#...#.",
#     ".#...#.####...####..###...###...###..",
#     ".....................................",
# ]


#
H, W = map(int, input().split())
ARR = []

for i in range(H):
    ARR.append(input())


def calculateDistance(h, w, arr):
    hist = [[0 for j in range(W)] for i in range(H)]

    q = deque()

    q.append((0, 0, 0, hist))

    distances = []

    while q.__len__() > 0:
        x, y, currentStep, hist = q.popleft()

        if x == w-1 and y == h - 1:
            distances.append(currentStep + 1)

        hist[y][x] = 1

        # left
        if x > 0 and (hist[y][x - 1] == 0) and (arr[y][x - 1] == "."):
            q.append((x - 1, y, currentStep + 1, hist))

        # right
        if x < w - 1 and (hist[y][x + 1] == 0) and (arr[y][x + 1] == "."):
            q.append((x + 1, y, currentStep + 1, hist))

        # up
        if y > 0 and (hist[y - 1][x] == 0) and (arr[y - 1][x] == "."):
            q.append((x, y - 1, currentStep + 1, hist))

        # down
        if y < h - 1 and (hist[y + 1][x] == 0) and (arr[y + 1][x] == "."):
            q.append((x, y + 1, currentStep + 1, hist))

    if distances.__len__() == 0:
        return -1
    else:
        return calculate(h,w,min(distances),arr)


def calculate(h, w, distance, arr):
    total = h * w
    alreadyPaiting = 0

    for s in arr:
        for ss in s:
            if ss == "#":
                alreadyPaiting += 1

    return total - distance - alreadyPaiting

res = calculateDistance(H, W, ARR)

print(res)

Fixed submission:
from collections import deque

H, W = 10, 37
ARR = [
    ".....................................",
    "...#...####...####..###...###...###..",
    "..#.#..#...#.##....#...#.#...#.#...#.",
    "..#.#..#...#.#.....#...#.#...#.#...#.",
    ".#...#.#..##.#.....#...#.#.###.#.###.",
    ".#####.####..#.....#...#..##....##...",
    ".#...#.#...#.#.....#...#.#...#.#...#.",
    ".#...#.#...#.##....#...#.#...#.#...#.",
    ".#...#.####...####..###...###...###..",
    ".....................................",
]

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

for i in range(H):
    ARR.append(input())


def calculate(h, w, arr):
    q = deque()

    q.append((0, 0))

    dx = [0, 1, 0, -1]
    dy = [-1, 0, 1, 0]

    distance = [[0 for i in range(w)] for j in range(h)]
    distance[0][0] = 1
    while q.__len__() > 0:
        y, x = q.popleft()
        for i in range(4):
            my = y + dy[i]
            mx = x + dx[i]
            if (my >= 0) and (mx >= 0) and (my <= (h - 1)) and (mx <= (w - 1)) and (distance[my][mx] == 0) and (
                    arr[my][mx] == "."):
                distance[my][mx] = distance[y][x] + 1
                q.append((my, mx))

    mmk = 0
    for j in range(h):
        mmk += arr[j].count("#")

    if distance[h - 1][w - 1] == 0:
        print(-1)
    else:
        print(h * w - distance[h - 1][w - 1] - mmk)


calculate(H, W, ARR)

Buggy python submission (verdict: Runtime Error):

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

for i in range(H):
    ARR.append(input())


def calculate(h, w, arr):
    total = h * w
    distance = h + w - 1
    alreadyPaiting = 0

    for s in arr:
        a = Counter(list(s))
        b = a.get("#")

        if b != None:
            alreadyPaiting += b

    return total - distance - alreadyPaiting


result = calculate(H, W, ARR)

print(result)

Fixed submission:
from collections import deque

H, W = 10, 37
ARR = [
    ".....................................",
    "...#...####...####..###...###...###..",
    "..#.#..#...#.##....#...#.#...#.#...#.",
    "..#.#..#...#.#.....#...#.#...#.#...#.",
    ".#...#.#..##.#.....#...#.#.###.#.###.",
    ".#####.####..#.....#...#..##....##...",
    ".#...#.#...#.#.....#...#.#...#.#...#.",
    ".#...#.#...#.##....#...#.#...#.#...#.",
    ".#...#.####...####..###...###...###..",
    ".....................................",
]

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

for i in range(H):
    ARR.append(input())


def calculate(h, w, arr):
    q = deque()

    q.append((0, 0))

    dx = [0, 1, 0, -1]
    dy = [-1, 0, 1, 0]

    distance = [[0 for i in range(w)] for j in range(h)]
    distance[0][0] = 1
    while q.__len__() > 0:
        y, x = q.popleft()
        for i in range(4):
            my = y + dy[i]
            mx = x + dx[i]
            if (my >= 0) and (mx >= 0) and (my <= (h - 1)) and (mx <= (w - 1)) and (distance[my][mx] == 0) and (
                    arr[my][mx] == "."):
                distance[my][mx] = distance[y][x] + 1
                q.append((my, mx))

    mmk = 0
    for j in range(h):
        mmk += arr[j].count("#")

    if distance[h - 1][w - 1] == 0:
        print(-1)
    else:
        print(h * w - distance[h - 1][w - 1] - mmk)


calculate(H, W, ARR)