Problem:
We have an H \times W grid whose squares are painted black or white. The square at the i-th row from the top and the j-th column from the left is denoted as (i, j).   Snuke would like to play the following game on this grid. At the beginning of the game, there is a character called Kenus at square (1, 1). The player repeatedly moves Kenus up, down, left or right by one square. The game is completed when Kenus reaches square (H, W) passing only white squares.   Before Snuke starts the game, he can change the color of some of the white squares to black. However, he cannot change the color of square (1, 1) and (H, W). Also, changes of color must all be carried out before the beginning of the game.   When the game is completed, Snuke's score will be the number of times he changed the color of a square before the beginning of the game. Find the maximum possible score that Snuke can achieve, or print -1 if the game cannot be completed, that is, Kenus can never reach square (H, W) regardless of how Snuke changes the color of the squares.  The color of the squares are given to you as characters s_{i, j}. If square (i, j) is initially painted by white, s_{i, j} is `.`; if square (i, j) is initially painted by black, s_{i, j} is `#`.

Incorrect submission (verdict: Wrong Answer):
H,W = map(int,input().split())
maze = []
for _ in range(H):
    l = list(input())
    maze.append(l)
import collections as col
q = col.deque()
q.append((0,0))
maze[0][0] = 0
def bfs(maze):
    while True:
        try: nowx,nowy = q.popleft()
        except IndexError:
            print("-1")
            quit()
        if nowx == (H - 1) and nowy == (W - 1): return
        for x in [1,-1]: 
            if nowx + x == H or nowy == W: continue
            if maze[nowx + x][nowy] != '.' : continue
            if 0 <= nowx + x and nowx + x <= H - 1:
                q.append((nowx+x,nowy))
                maze[nowx + x][nowy] = maze[nowx][nowy] + 1
        for y in [1,-1]: 
            if nowx == H or nowy + y== W: continue
            if maze[nowx][nowy + y] != '.' : continue
            if 0 <= nowy + y and nowy + y <= W - 1:
                q.append((nowx,nowy+y))
                maze[nowx][nowy + y] = maze[nowx][nowy] + 1
bfs(maze)
cnt = 0
for x in maze:
    for y in x:
        if y == '#':
            cnt += 1
print(H*W - maze[H-1][W-1] - cnt - 1)

print(maze)


Fixed, accepted submission:
H,W = map(int,input().split())
maze = []
for _ in range(H):
    l = list(input())
    maze.append(l)
import collections as col
q = col.deque()
q.append((0,0))
maze[0][0] = 0
def bfs(maze):
    while True:
        if len(q) == 0:
            print("-1")
            quit()
        else: nowx,nowy = q.popleft()
        if nowx == (H - 1) and nowy == (W - 1): return
        for x in [1,-1]: 
            if nowx + x == H or nowy == W or \
             maze[nowx + x][nowy] != '.' : continue
            if 0 <= nowx + x and nowx + x <= H - 1:
                q.append((nowx+x,nowy))
                maze[nowx + x][nowy] = maze[nowx][nowy] + 1
        for y in [1,-1]: 
            if nowx == H or nowy + y== W or \
             maze[nowx][nowy + y] != '.' : continue
            if 0 <= nowy + y and nowy + y <= W - 1:
                q.append((nowx,nowy+y))
                maze[nowx][nowy + y] = maze[nowx][nowy] + 1
bfs(maze)
cnt = 0
for x in maze:
    for y in x:
        if y == '#':
            cnt += 1
print(H*W - maze[H-1][W-1] - cnt - 1)