Buggy python submission (verdict: Time Limit Exceeded):
# coding: utf-8

# https://atcoder.jp/contests/abc088/tasks/abc088_d
# 19:01-20:04 chudan
# 20:17-20:30 done


def main():
    H, W = map(int, input().split())

    INF = 10**10
    n_white = 0
    s = [[-1] * (W+2) for _ in range(H+2)]
    for i in range(H):
        for j, x in enumerate(input()):
            # white => 1, black => -1
            s[i+1][j+1] = INF if x == "." else -1
            n_white += 1 if x == "." else 0
    s[1][1] = 0

    fixed = set([])
    while True:
        # 最短から貪欲に伸ばして行く
        x, y = H, W  # init
        for i in range(1, H+1):
            for j in range(1, W+1):
                if s[i][j] != -1 and s[i][j] <= s[x][y] and "{}_{}".format(i, j) not in fixed:
                    x, y = i, j
                    
        fixed.add("{}_{}".format(x, y))
        
        if x == H and y == W:
            shortest = s[x][y]
            break

        if s[x][y-1] != -1:
            s[x][y-1] = min(s[x][y]+1, s[x][y-1])
            # fixed.add("{}_{}".format(x, y-1))
        if s[x][y+1] != -1:
            s[x][y+1] = min(s[x][y]+1, s[x][y+1])
            # fixed.add("{}_{}".format(x, y+1))
        if s[x-1][y] != -1:
            s[x-1][y] = min(s[x][y]+1, s[x-1][y])
            # fixed.add("{}_{}".format(x-1, y))
        if s[x+1][y] != -1:
            s[x+1][y] = min(s[x][y]+1, s[x+1][y])
            # fixed.add("{}_{}".format(x+1, y))
    
    return n_white - (shortest + 1) if shortest < H*W else -1


print(main())


Fixed submission:
# coding: utf-8

# https://atcoder.jp/contests/abc088/tasks/abc088_d
# 19:01-20:04 chudan
# 20:17-20:53 done


def main():
    H, W = map(int, input().split())

    INF = 10**10
    n_white = 0
    s = [[-1] * (W+2) for _ in range(H+2)]
    for i in range(H):
        for j, x in enumerate(input()):
            # white => 1, black => -1
            s[i+1][j+1] = INF if x == "." else -1
            n_white += 1 if x == "." else 0
    s[1][1] = 0

    queue = [(1, 1)]
    queue_size = 1
    queue_idx = 0
    while True:
        # 最短から貪欲に伸ばして行く
        if queue_idx == queue_size:
            # cannot arrive
            return -1
        
        x, y = queue[queue_idx]
        queue_idx += 1
        
        if x == H and y == W:
            shortest = s[x][y]
            break

        if s[x][y-1] != -1 and s[x][y-1] > s[x][y]+1:
            s[x][y-1] = s[x][y]+1
            queue.append((x, y-1))
            queue_size += 1
        if s[x][y+1] != -1 and s[x][y+1] > s[x][y]+1:
            s[x][y+1] = s[x][y]+1
            queue.append((x, y+1))
            queue_size += 1
        if s[x-1][y] != -1 and s[x-1][y] > s[x][y]+1:
            s[x-1][y] = s[x][y]+1
            queue.append((x-1, y))
            queue_size += 1
        if s[x+1][y] != -1 and s[x+1][y] > s[x][y]+1:
            s[x+1][y] = s[x][y]+1
            queue.append((x+1, y))
            queue_size += 1
    
    return n_white - (shortest + 1)


print(main())

Buggy python submission (verdict: Runtime Error):
# coding: utf-8

# https://atcoder.jp/contests/abc088/tasks/abc088_d
# 19:01-20:04 chudan
# 20:17-20:30 done


def main():
    H, W = map(int, input().split())

    INF = 10**10
    n_white = 0
    s = [[-1] * (W+2) for _ in range(H+2)]
    for i in range(H):
        for j, x in enumerate(input()):
            # white => 1, black => -1
            s[i+1][j+1] = INF if x == "." else -1
            n_white += 1 if x == "." else 0
    s[1][1] = 0

    fixed = set([])
    while True:
        # 最短から貪欲に伸ばして行く
        x, y = H, W  # init
        for i in range(1, H+1):
            for j in range(1, W+1):
                if s[i][j] != -1 and s[i][j] <= s[x][y] and "{}_{}".format(i, j) not in fixed:
                    x, y = i, j
                    
        fixed.add("{}_{}".format(x, y))
        
        if x == H and y == W:
            shortest = s[x][y]
            break

        if s[x][y-1] != -1:
            s[x][y-1] = min(s[x][y]+1, s[x][y-1])
            # fixed.add("{}_{}".format(x, y-1))
        if s[x][y+1] != -1:
            s[x][y+1] = min(s[x][y]+1, s[x][y+1])
            # fixed.add("{}_{}".format(x, y+1))
        if s[x-1][y] != -1:
            s[x-1][y] = min(s[x][y]+1, s[x-1][y])
            # fixed.add("{}_{}".format(x-1, y))
        if s[x+1][y] != -1:
            s[x+1][y] = min(s[x][y]+1, s[x+1][y])
            # fixed.add("{}_{}".format(x+1, y))
    
    return n_white - (shortest + 1) if shortest < H*W else


print(main())


Fixed submission:
# coding: utf-8

# https://atcoder.jp/contests/abc088/tasks/abc088_d
# 19:01-20:04 chudan
# 20:17-20:53 done


def main():
    H, W = map(int, input().split())

    INF = 10**10
    n_white = 0
    s = [[-1] * (W+2) for _ in range(H+2)]
    for i in range(H):
        for j, x in enumerate(input()):
            # white => 1, black => -1
            s[i+1][j+1] = INF if x == "." else -1
            n_white += 1 if x == "." else 0
    s[1][1] = 0

    queue = [(1, 1)]
    queue_size = 1
    queue_idx = 0
    while True:
        # 最短から貪欲に伸ばして行く
        if queue_idx == queue_size:
            # cannot arrive
            return -1
        
        x, y = queue[queue_idx]
        queue_idx += 1
        
        if x == H and y == W:
            shortest = s[x][y]
            break

        if s[x][y-1] != -1 and s[x][y-1] > s[x][y]+1:
            s[x][y-1] = s[x][y]+1
            queue.append((x, y-1))
            queue_size += 1
        if s[x][y+1] != -1 and s[x][y+1] > s[x][y]+1:
            s[x][y+1] = s[x][y]+1
            queue.append((x, y+1))
            queue_size += 1
        if s[x-1][y] != -1 and s[x-1][y] > s[x][y]+1:
            s[x-1][y] = s[x][y]+1
            queue.append((x-1, y))
            queue_size += 1
        if s[x+1][y] != -1 and s[x+1][y] > s[x][y]+1:
            s[x+1][y] = s[x][y]+1
            queue.append((x+1, y))
            queue_size += 1
    
    return n_white - (shortest + 1)


print(main())

Buggy python submission (verdict: Time Limit Exceeded):
# coding: utf-8

# https://atcoder.jp/contests/abc088/tasks/abc088_d
# 19:01-20:04 chudan
# 20:17-20:30 done


def main():
    H, W = map(int, input().split())

    INF = 10**10
    n_white = 0
    s = [[-1] * (W+2) for _ in range(H+2)]
    for i in range(H):
        for j, x in enumerate(input()):
            # white => 1, black => -1
            s[i+1][j+1] = INF if x == "." else -1
            n_white += 1 if x == "." else 0
    s[1][1] = 0

    fixed = set([])
    while True:
        # 最短から貪欲に伸ばして行く
        x, y = H, W  # init
        for i in range(1, H+1):
            for j in range(1, W+1):
                if s[i][j] != -1 and s[i][j] <= s[x][y] and "{}_{}".format(i, j) not in fixed:
                    x, y = i, j
                    
        fixed.add("{}_{}".format(x, y))
        
        if x == H and y == W:
            shortest = s[x][y]
            break

        if s[x][y-1] != -1:
            s[x][y-1] = min(s[x][y]+1, s[x][y-1])
            # fixed.add("{}_{}".format(x, y-1))
        if s[x][y+1] != -1:
            s[x][y+1] = min(s[x][y]+1, s[x][y+1])
            # fixed.add("{}_{}".format(x, y+1))
        if s[x-1][y] != -1:
            s[x-1][y] = min(s[x][y]+1, s[x-1][y])
            # fixed.add("{}_{}".format(x-1, y))
        if s[x+1][y] != -1:
            s[x+1][y] = min(s[x][y]+1, s[x+1][y])
            # fixed.add("{}_{}".format(x+1, y))
    
    return n_white - (shortest + 1) if shortest < H*W else -1


print(main())


Fixed submission:
# coding: utf-8

# https://atcoder.jp/contests/abc088/tasks/abc088_d
# 19:01-20:04 chudan
# 20:17-20:53 done


def main():
    H, W = map(int, input().split())

    INF = 10**10
    n_white = 0
    s = [[-1] * (W+2) for _ in range(H+2)]
    for i in range(H):
        for j, x in enumerate(input()):
            # white => 1, black => -1
            s[i+1][j+1] = INF if x == "." else -1
            n_white += 1 if x == "." else 0
    s[1][1] = 0

    queue = [(1, 1)]
    queue_size = 1
    queue_idx = 0
    while True:
        # 最短から貪欲に伸ばして行く
        if queue_idx == queue_size:
            # cannot arrive
            return -1
        
        x, y = queue[queue_idx]
        queue_idx += 1
        
        if x == H and y == W:
            shortest = s[x][y]
            break

        if s[x][y-1] != -1 and s[x][y-1] > s[x][y]+1:
            s[x][y-1] = s[x][y]+1
            queue.append((x, y-1))
            queue_size += 1
        if s[x][y+1] != -1 and s[x][y+1] > s[x][y]+1:
            s[x][y+1] = s[x][y]+1
            queue.append((x, y+1))
            queue_size += 1
        if s[x-1][y] != -1 and s[x-1][y] > s[x][y]+1:
            s[x-1][y] = s[x][y]+1
            queue.append((x-1, y))
            queue_size += 1
        if s[x+1][y] != -1 and s[x+1][y] > s[x][y]+1:
            s[x+1][y] = s[x][y]+1
            queue.append((x+1, y))
            queue_size += 1
    
    return n_white - (shortest + 1)


print(main())

Buggy python submission (verdict: Runtime Error):
# coding: utf-8

# https://atcoder.jp/contests/abc088/tasks/abc088_d
# 19:01-20:04 chudan
# 20:17-20:30 done


def main():
    H, W = map(int, input().split())

    INF = 10**10
    n_white = 0
    s = [[-1] * (W+2) for _ in range(H+2)]
    for i in range(H):
        for j, x in enumerate(input()):
            # white => 1, black => -1
            s[i+1][j+1] = INF if x == "." else -1
            n_white += 1 if x == "." else 0
    s[1][1] = 0

    fixed = set([])
    while True:
        # 最短から貪欲に伸ばして行く
        x, y = H, W  # init
        for i in range(1, H+1):
            for j in range(1, W+1):
                if s[i][j] != -1 and s[i][j] <= s[x][y] and "{}_{}".format(i, j) not in fixed:
                    x, y = i, j
                    
        fixed.add("{}_{}".format(x, y))
        
        if x == H and y == W:
            shortest = s[x][y]
            break

        if s[x][y-1] != -1:
            s[x][y-1] = min(s[x][y]+1, s[x][y-1])
            # fixed.add("{}_{}".format(x, y-1))
        if s[x][y+1] != -1:
            s[x][y+1] = min(s[x][y]+1, s[x][y+1])
            # fixed.add("{}_{}".format(x, y+1))
        if s[x-1][y] != -1:
            s[x-1][y] = min(s[x][y]+1, s[x-1][y])
            # fixed.add("{}_{}".format(x-1, y))
        if s[x+1][y] != -1:
            s[x+1][y] = min(s[x][y]+1, s[x+1][y])
            # fixed.add("{}_{}".format(x+1, y))
    
    return n_white - (shortest + 1) if shortest < H*W else


print(main())


Fixed submission:
# coding: utf-8

# https://atcoder.jp/contests/abc088/tasks/abc088_d
# 19:01-20:04 chudan
# 20:17-20:53 done


def main():
    H, W = map(int, input().split())

    INF = 10**10
    n_white = 0
    s = [[-1] * (W+2) for _ in range(H+2)]
    for i in range(H):
        for j, x in enumerate(input()):
            # white => 1, black => -1
            s[i+1][j+1] = INF if x == "." else -1
            n_white += 1 if x == "." else 0
    s[1][1] = 0

    queue = [(1, 1)]
    queue_size = 1
    queue_idx = 0
    while True:
        # 最短から貪欲に伸ばして行く
        if queue_idx == queue_size:
            # cannot arrive
            return -1
        
        x, y = queue[queue_idx]
        queue_idx += 1
        
        if x == H and y == W:
            shortest = s[x][y]
            break

        if s[x][y-1] != -1 and s[x][y-1] > s[x][y]+1:
            s[x][y-1] = s[x][y]+1
            queue.append((x, y-1))
            queue_size += 1
        if s[x][y+1] != -1 and s[x][y+1] > s[x][y]+1:
            s[x][y+1] = s[x][y]+1
            queue.append((x, y+1))
            queue_size += 1
        if s[x-1][y] != -1 and s[x-1][y] > s[x][y]+1:
            s[x-1][y] = s[x][y]+1
            queue.append((x-1, y))
            queue_size += 1
        if s[x+1][y] != -1 and s[x+1][y] > s[x][y]+1:
            s[x+1][y] = s[x][y]+1
            queue.append((x+1, y))
            queue_size += 1
    
    return n_white - (shortest + 1)


print(main())