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):
# coding: UTF-8
#@document_it
from collections import deque
def document_it(func):
    def new_function(*args,**kwargs):
        print('Running function:', func.__name__)
        print('Positional arguments:', args)
        print('Kewword arguments:', kwargs)
        result = func(*args,**kwargs)
        print('Result:', result)
        return result
    return new_function
###########################

#@document_it

def nb(i,j,H,W):
    ans = []
    a = i+1
    b = i-1
    c = j+1
    d = j-1
    if a >= 0 and a <= H-1:
        ans.append((a,j))
    if b >= 0 and b <= H-1:
        ans.append((b,j))
    if c >= 0 and c <= W-1:
        ans.append((i,c))
    if d >= 0 and d <= W-1:
        ans.append((i,d))
    return ans
        

def e():
    
    dist={}
    white = {}
    wtotal = 0
    H,W = map(int,input().split())
    for i in range(H):
        row = input()
        for j in range(W):
            dist[(i,j)] = -1
            if row[j] == '.':
                white[(i,j)] = True
                wtotal += 1
            else:
                white[(i,j)] = False
    
    #tuple -> [tuple]
    
    link = {}
    for i in range(H):
        for j in range(W):
            link[(i,j)] = []
            if not white[(i,j)]:
                continue
            else:
                nlist = nb(i,j,H,W)
                for p in nlist:
                    if white[p]:
                        link[(i,j)].append(p)
    
    q = deque()
    dist[(0,0)] = 1
    q.append((0,0))
    while q:
        v = q.popleft()
        for w in link[v]:
            if dist[w] == -1:
                dist[w] = dist[v] + 1
                q.append(w)
                #print(w,dist[w])
    if dist[(H-1,W-1)] != -1:
        ans = wtotal - dist[(H-1,W-1)]
    else:
        ans = -1
    print(wtotal)
    print(dist[(H-1,W-1)])
                    
    print(ans)

###########################
if __name__ == '__main__':
    e()

Fixed, accepted submission:
# coding: UTF-8
#@document_it
from collections import deque
def document_it(func):
    def new_function(*args,**kwargs):
        print('Running function:', func.__name__)
        print('Positional arguments:', args)
        print('Kewword arguments:', kwargs)
        result = func(*args,**kwargs)
        print('Result:', result)
        return result
    return new_function
###########################

#@document_it

def nb(i,j,H,W):
    ans = []
    a = i+1
    b = i-1
    c = j+1
    d = j-1
    if a >= 0 and a <= H-1:
        ans.append((a,j))
    if b >= 0 and b <= H-1:
        ans.append((b,j))
    if c >= 0 and c <= W-1:
        ans.append((i,c))
    if d >= 0 and d <= W-1:
        ans.append((i,d))
    return ans
        

def e():
    
    dist={}
    white = {}
    wtotal = 0
    H,W = map(int,input().split())
    for i in range(H):
        row = input()
        for j in range(W):
            dist[(i,j)] = -1
            if row[j] == '.':
                white[(i,j)] = True
                wtotal += 1
            else:
                white[(i,j)] = False
    
    #tuple -> [tuple]
    
    link = {}
    for i in range(H):
        for j in range(W):
            link[(i,j)] = []
            if not white[(i,j)]:
                continue
            else:
                nlist = nb(i,j,H,W)
                for p in nlist:
                    if white[p]:
                        link[(i,j)].append(p)
    
    q = deque()
    dist[(0,0)] = 1
    q.append((0,0))
    while q:
        v = q.popleft()
        for w in link[v]:
            if dist[w] == -1:
                dist[w] = dist[v] + 1
                q.append(w)
                #print(w,dist[w])
    if dist[(H-1,W-1)] != -1:
        ans = wtotal - dist[(H-1,W-1)]
    else:
        ans = -1
    #print(wtotal)
    #print(dist[(H-1,W-1)])
                    
    print(ans)

###########################
if __name__ == '__main__':
    e()