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):
from collections import deque

nNM = []
nNM = input().rstrip().split(' ')

nH = int(nNM[0])
nW = int(nNM[1])
nTmp = [0 for i in range(nW)]




graph = {}
kyori = {}
checked = {}





nWallCnt = 0

for i in range(nH):
    nTmp=input()
#    print(nTmp)
    
    for i2 in range(nW):
 
        nPoi=('%d-%d' %(i,i2))
        graph[nPoi] = []
        kyori[nPoi] = float("inf")
        checked[nPoi] = 0
        
        if i==0 and i2==0:
            nStart = ('%d-%d' %(0,0))
            kyori[nPoi] = int (0)
            continue
        elif i==nH - 1 and i2==nW - 1:
            nGoal = ('%d-%d' %(nH - 1, nW - 1))
            continue

        elif nTmp[i2]=='#':
            nWall = ('%d-%d' %(i,i2))
            nWallCnt += 1
            checked[nPoi] = 1

        else:
            if i - 1 >= 0:
                graph[nPoi].append('%d-%d' %(i - 1, i2))

            if i + 1 < nH :
                graph[nPoi].append('%d-%d' %(i + 1 , i2))

            if i2 - 1 >= 0:
                graph[nPoi].append('%d-%d' %(i , i2 - 1))

            if i2 + 1 < nW:
                graph[nPoi].append('%d-%d' %(i , i2 + 1))
        


root_queue = deque()
root_queue += graph[nStart]
nCount = len(graph[nStart])

for i in range(nCount):
    kyori[(graph[nStart][i])] = 1


nFlag = 0


while root_queue:
    nPoi2 = root_queue.popleft()

    if checked[nPoi2] == 0:
        if nPoi2 == nGoal:
            nFlag = 1
            break
        else:
            root_queue += graph[nPoi2]
            for i in range(len(graph[nPoi2])):
                nTmpCount = kyori[nPoi2] + 1
                if kyori[(graph[nPoi2][i])] > nTmpCount:
                    kyori[(graph[nPoi2][i])] = nTmpCount
            checked[nPoi2] = 1


if nFlag == 0:
    print('-1')
else:
    nTmp = kyori[nGoal] + 1
    nTotal = nH * nW
    nAns = nTotal - (nTmp + nWallCnt )
    print(nAns)



Fixed, accepted submission:
from collections import deque

nNM = []
nNM = input().rstrip().split(' ')

nH = int(nNM[0])
nW = int(nNM[1])
nTmp = [0 for i in range(nW)]

graph = {}
kyori = {}
checked = {}

nWallCnt = 0

for i in range(nH):
    nTmp=input()
#    print(nTmp)
    
    for i2 in range(nW):

#        print(nTmp[i2]) 
        nPoi=('%d-%d' %(i,i2))
        graph[nPoi] = []
        kyori[nPoi] = float("inf")
        checked[nPoi] = 0
        
        if i==0 and i2==0:
            nStart = ('%d-%d' %(0,0))
            kyori[nPoi] = int (0)

        if i==nH - 1 and i2==nW - 1:
            nGoal = ('%d-%d' %(nH - 1, nW - 1))

        elif nTmp[i2]=='#':
            nWall = ('%d-%d' %(i,i2))
            nWallCnt += 1
            checked[nPoi] = 1

        else:
            if i - 1 >= 0:
                graph[nPoi].append('%d-%d' %(i - 1, i2))

            if i + 1 < nH :
                graph[nPoi].append('%d-%d' %(i + 1 , i2))

            if i2 - 1 >= 0:
                graph[nPoi].append('%d-%d' %(i , i2 - 1))

            if i2 + 1 < nW:
                graph[nPoi].append('%d-%d' %(i , i2 + 1))
        


root_queue = deque()
root_queue += graph[nStart]
nCount = len(graph[nStart])

for i in range(nCount):
    kyori[(graph[nStart][i])] = 1


nFlag = 0


while root_queue:
    nPoi2 = root_queue.popleft()

    if checked[nPoi2] == 0:
        if nPoi2 == nGoal:
            nFlag = 1
            break
        else:
            root_queue += graph[nPoi2]
            for i in range(len(graph[nPoi2])):
                nTmpCount = kyori[nPoi2] + 1
                if kyori[(graph[nPoi2][i])] > nTmpCount:
                    kyori[(graph[nPoi2][i])] = nTmpCount
            checked[nPoi2] = 1


if nFlag == 0:
    print('-1')
else:
    nTmp = kyori[nGoal] + 1
    nTotal = nH * nW
    nAns = nTotal - (nTmp + nWallCnt )
    print(nAns)