Buggy python submission (verdict: Time Limit Exceeded):
def main():
    import sys
    input=sys.stdin.readline
    from collections import deque
    #class ctmp():
    #    def __init__(self,ij,dis):
    #        self.ij=ij
    #        self.dis=dis
    H,W=map(int,input().split())
    ls=["#"*(W+2)]#
    for i in range(H):
        ls.append("#"+input()+"#")
    ls.append("#"*(W+2))#
    q=deque([])
    qd=deque([])
    #tmp=ctmp((1,1),0)
    q.append((1,1))
    qd.append(0)
    #df={}
    lf=[[0 for _ in range(W+2)] for _ in range(H+2)]#
    lf[1][1]=0
    while q:
        tmpij=q.popleft()
        tmpd=qd.popleft()
        #df[tmpij]=1
        #lf[tmpij[0]][tmpij[1]]=1
        if tmpij==(H,W):
            break
        lind=[(-1,0),(1,0),(0,-1),(0,1)]#
        for i in range(4):
            #ntmpij=(tmpij[0]+(i<2)*(2*i-1),tmpij[1]+(i>1)*(2*i-5))
            ntmpij=(tmpij[0]+lind[i][0],tmpij[1]+lind[i][1])
            #print(ntmpij)
            #if ntmpij not in df and ls[ntmpij[0]][ntmpij[1]]==".":
            if lf[ntmpij[0]][ntmpij[1]]==0 and ls[ntmpij[0]][ntmpij[1]]==".":
                #c_tmp=ctmp(ntmpij,tmp.dis+1)
                #q.append(c_tmp)
                q.append(ntmpij)
                qd.append(tmpd+1)
                lf[tmpij[0]][tmpij[1]]=1
                #lf[ntmpij[0]][ntmpij[1]]=lf[tmpij[0]][tmpij[1]]+1
    if tmpij==(H,W):
        nw=0
        for i in range(1,H+1):
            #for j in range(1,W+1):
             #   if ls[i][j]==".":
              #      nw+=1
            nw+=ls[i].count(".")
        print(nw-tmpd-1)
        #print(nw-lf[H][W]-1)
    else:
        print(-1)

if __name__=='__main__':
    main()


Fixed submission:
def main():
    import sys
    input=sys.stdin.readline
    from collections import deque
    #class ctmp():
    #    def __init__(self,ij,dis):
    #        self.ij=ij
    #        self.dis=dis
    H,W=map(int,input().split())
    ls=["#"*(W+2)]#
    for i in range(H):
        ls.append("#"+input()+"#")
    ls.append("#"*(W+2))#
    q=deque([])
    #qd=deque([])
    #tmp=ctmp((1,1),0)
    q.append((1,1))
    #qd.append(0)
    #df={}
    lf=[[float("inf") for _ in range(W+2)] for _ in range(H+2)]#
    #これでfとdの両方の役割をこなす
    lf[1][1]=0
    while q:
        tmpij=q.popleft()
        #tmpd=qd.popleft()
        #df[tmpij]=1
        if tmpij==(H,W):
            break
        lind=[(-1,0),(1,0),(0,-1),(0,1)]#
        for i in range(4):
            #ntmpij=(tmpij[0]+(i<2)*(2*i-1),tmpij[1]+(i>1)*(2*i-5))
            ntmpij=(tmpij[0]+lind[i][0],tmpij[1]+lind[i][1])
            #print(ntmpij)
            #if ntmpij not in df and ls[ntmpij[0]][ntmpij[1]]==".":
            if lf[ntmpij[0]][ntmpij[1]]==float("inf") and ls[ntmpij[0]][ntmpij[1]]==".":
                #c_tmp=ctmp(ntmpij,tmp.dis+1)
                #q.append(c_tmp)
                q.append(ntmpij)
                #qd.append(tmpd+1)
                lf[ntmpij[0]][ntmpij[1]]=lf[tmpij[0]][tmpij[1]]+1
                #dは訪れる前に書き込んでしまう
    if tmpij==(H,W):
        nw=0
        for i in range(1,H+1):
            #for j in range(1,W+1):
             #   if ls[i][j]==".":
              #      nw+=1
            nw+=ls[i].count(".")
        #print(nw-tmpd-1)
        print(nw-lf[H][W]-1)
    else:
        print(-1)

if __name__=='__main__':
    main()

Buggy python submission (verdict: Time Limit Exceeded):
def main():
    import sys
    input=sys.stdin.readline
    from collections import deque
    #class ctmp():
    #    def __init__(self,ij,dis):
    #        self.ij=ij
    #        self.dis=dis
    H,W=map(int,input().split())
    ls=["#"*(W+2)]#
    for i in range(H):
        ls.append("#"+input()+"#")
    ls.append("#"*(W+2))#
    q=deque([])
    qd=deque([])
    #tmp=ctmp((1,1),0)
    q.append((1,1))
    qd.append(0)
    #df={}
    lf=[[0 for _ in range(W+2)] for _ in range(H+2)]#
    lf[1][1]=0
    while q:
        tmpij=q.popleft()
        tmpd=qd.popleft()
        #df[tmpij]=1
        #lf[tmpij[0]][tmpij[1]]=1
        if tmpij==(H,W):
            break
        lind=[(-1,0),(1,0),(0,-1),(0,1)]#
        for i in range(4):
            #ntmpij=(tmpij[0]+(i<2)*(2*i-1),tmpij[1]+(i>1)*(2*i-5))
            ntmpij=(tmpij[0]+lind[i][0],tmpij[1]+lind[i][1])
            #print(ntmpij)
            #if ntmpij not in df and ls[ntmpij[0]][ntmpij[1]]==".":
            if lf[ntmpij[0]][ntmpij[1]]==0 and ls[ntmpij[0]][ntmpij[1]]==".":
                #c_tmp=ctmp(ntmpij,tmp.dis+1)
                #q.append(c_tmp)
                q.append(ntmpij)
                qd.append(tmpd+1)
                lf[tmpij[0]][tmpij[1]]=1
                #lf[ntmpij[0]][ntmpij[1]]=lf[tmpij[0]][tmpij[1]]+1
    if tmpij==(H,W):
        nw=0
        for i in range(1,H+1):
            #for j in range(1,W+1):
             #   if ls[i][j]==".":
              #      nw+=1
            nw+=ls[i].count(".")
        print(nw-tmpd-1)
        #print(nw-lf[H][W]-1)
    else:
        print(-1)

if __name__=='__main__':
    main()


Fixed submission:
def main():
    import sys
    input=sys.stdin.readline
    from collections import deque
    #class ctmp():
    #    def __init__(self,ij,dis):
    #        self.ij=ij
    #        self.dis=dis
    H,W=map(int,input().split())
    ls=["#"*(W+2)]#
    for i in range(H):
        ls.append("#"+input()+"#")
    ls.append("#"*(W+2))#
    q=deque([])
    #qd=deque([])
    #tmp=ctmp((1,1),0)
    q.append((1,1))
    #qd.append(0)
    #df={}
    lf=[[float("inf") for _ in range(W+2)] for _ in range(H+2)]#
    #これでfとdの両方の役割をこなす
    lf[1][1]=0
    while q:
        tmpij=q.popleft()
        #tmpd=qd.popleft()
        #df[tmpij]=1
        if tmpij==(H,W):
            break
        lind=[(-1,0),(1,0),(0,-1),(0,1)]#
        for i in range(4):
            #ntmpij=(tmpij[0]+(i<2)*(2*i-1),tmpij[1]+(i>1)*(2*i-5))
            ntmpij=(tmpij[0]+lind[i][0],tmpij[1]+lind[i][1])
            #print(ntmpij)
            #if ntmpij not in df and ls[ntmpij[0]][ntmpij[1]]==".":
            if lf[ntmpij[0]][ntmpij[1]]==float("inf") and ls[ntmpij[0]][ntmpij[1]]==".":
                #c_tmp=ctmp(ntmpij,tmp.dis+1)
                #q.append(c_tmp)
                q.append(ntmpij)
                #qd.append(tmpd+1)
                lf[ntmpij[0]][ntmpij[1]]=lf[tmpij[0]][tmpij[1]]+1
                #dは訪れる前に書き込んでしまう
    if tmpij==(H,W):
        nw=0
        for i in range(1,H+1):
            #for j in range(1,W+1):
             #   if ls[i][j]==".":
              #      nw+=1
            nw+=ls[i].count(".")
        #print(nw-tmpd-1)
        print(nw-lf[H][W]-1)
    else:
        print(-1)

if __name__=='__main__':
    main()

Buggy python submission (verdict: Time Limit Exceeded):
def main():
    import sys
    input=sys.stdin.readline
    from collections import deque
    #class ctmp():
    #    def __init__(self,ij,dis):
    #        self.ij=ij
    #        self.dis=dis
    H,W=map(int,input().split())
    ls=["#"*(W+2)]#
    for i in range(H):
        ls.append("#"+input()+"#")
    ls.append("#"*(W+2))#
    q=deque([])
    qd=deque([])
    #tmp=ctmp((1,1),0)
    q.append((1,1))
    qd.append(0)
    #df={}
    lf=[[0 for _ in range(W+2)] for _ in range(H+2)]#
    lf[1][1]=0
    while q:
        tmpij=q.popleft()
        tmpd=qd.popleft()
        #df[tmpij]=1
        lf[tmpij[0]][tmpij[1]]=1
        if tmpij==(H,W):
            break
        lind=[(-1,0),(1,0),(0,-1),(0,1)]#
        for i in range(4):
            #ntmpij=(tmpij[0]+(i<2)*(2*i-1),tmpij[1]+(i>1)*(2*i-5))
            ntmpij=(tmpij[0]+lind[i][0],tmpij[1]+lind[i][1])
            #print(ntmpij)
            #if ntmpij not in df and ls[ntmpij[0]][ntmpij[1]]==".":
            if lf[ntmpij[0]][ntmpij[1]]==0 and ls[ntmpij[0]][ntmpij[1]]==".":
                #c_tmp=ctmp(ntmpij,tmp.dis+1)
                #q.append(c_tmp)
                q.append(ntmpij)
                qd.append(tmpd+1)
                #lf[ntmpij[0]][ntmpij[1]]=lf[tmpij[0]][tmpij[1]]+1
    if tmpij==(H,W):
        nw=0
        for i in range(1,H+1):
            #for j in range(1,W+1):
             #   if ls[i][j]==".":
              #      nw+=1
            nw+=ls[i].count(".")
        print(nw-tmpd-1)
        #print(nw-lf[H][W]-1)
    else:
        print(-1)

if __name__=='__main__':
    main()


Fixed submission:
def main():
    import sys
    input=sys.stdin.readline
    from collections import deque
    #class ctmp():
    #    def __init__(self,ij,dis):
    #        self.ij=ij
    #        self.dis=dis
    H,W=map(int,input().split())
    ls=["#"*(W+2)]#
    for i in range(H):
        ls.append("#"+input()+"#")
    ls.append("#"*(W+2))#
    q=deque([])
    #qd=deque([])
    #tmp=ctmp((1,1),0)
    q.append((1,1))
    #qd.append(0)
    #df={}
    lf=[[float("inf") for _ in range(W+2)] for _ in range(H+2)]#
    #これでfとdの両方の役割をこなす
    lf[1][1]=0
    while q:
        tmpij=q.popleft()
        #tmpd=qd.popleft()
        #df[tmpij]=1
        if tmpij==(H,W):
            break
        lind=[(-1,0),(1,0),(0,-1),(0,1)]#
        for i in range(4):
            #ntmpij=(tmpij[0]+(i<2)*(2*i-1),tmpij[1]+(i>1)*(2*i-5))
            ntmpij=(tmpij[0]+lind[i][0],tmpij[1]+lind[i][1])
            #print(ntmpij)
            #if ntmpij not in df and ls[ntmpij[0]][ntmpij[1]]==".":
            if lf[ntmpij[0]][ntmpij[1]]==float("inf") and ls[ntmpij[0]][ntmpij[1]]==".":
                #c_tmp=ctmp(ntmpij,tmp.dis+1)
                #q.append(c_tmp)
                q.append(ntmpij)
                #qd.append(tmpd+1)
                lf[ntmpij[0]][ntmpij[1]]=lf[tmpij[0]][tmpij[1]]+1
                #dは訪れる前に書き込んでしまう
    if tmpij==(H,W):
        nw=0
        for i in range(1,H+1):
            #for j in range(1,W+1):
             #   if ls[i][j]==".":
              #      nw+=1
            nw+=ls[i].count(".")
        #print(nw-tmpd-1)
        print(nw-lf[H][W]-1)
    else:
        print(-1)

if __name__=='__main__':
    main()

Buggy python submission (verdict: Time Limit Exceeded):
def main():
    import sys
    input=sys.stdin.readline
    from collections import deque
    #class ctmp():
    #    def __init__(self,ij,dis):
    #        self.ij=ij
    #        self.dis=dis
    H,W=map(int,input().split())
    ls=["#"*(W+2)]
    for i in range(H):
        ls.append("#"+input()+"#")
    ls.append("#"*(W+2))
    q=deque([])
    qd=deque([])
    #tmp=ctmp((1,1),0)
    q.append((1,1))
    qd.append(0)
    df={}
    while q:
        tmpij=q.popleft()
        tmpd=qd.popleft()
        df[tmpij]=1
        if tmpij==(H,W):
            break
        for i in range(4):
            ntmpij=(tmpij[0]+(i<2)*(2*i-1),tmpij[1]+(i>1)*(2*i-5))
            #print(ntmpij)
            if ntmpij not in df and ls[ntmpij[0]][ntmpij[1]]==".":
                #c_tmp=ctmp(ntmpij,tmp.dis+1)
                #q.append(c_tmp)
                q.append(ntmpij)
                qd.append(tmpd+1)
    if tmpij==(H,W):
        nw=0
        for i in range(1,H+1):
            #for j in range(1,W+1):
             #   if ls[i][j]==".":
              #      nw+=1
            nw+=ls[i].count(".")
        print(nw-tmpd-1)
    else:
        print(-1)

if __name__=='__main__':
    main()


Fixed submission:
def main():
    import sys
    input=sys.stdin.readline
    from collections import deque
    #class ctmp():
    #    def __init__(self,ij,dis):
    #        self.ij=ij
    #        self.dis=dis
    H,W=map(int,input().split())
    ls=["#"*(W+2)]#
    for i in range(H):
        ls.append("#"+input()+"#")
    ls.append("#"*(W+2))#
    q=deque([])
    #qd=deque([])
    #tmp=ctmp((1,1),0)
    q.append((1,1))
    #qd.append(0)
    #df={}
    lf=[[float("inf") for _ in range(W+2)] for _ in range(H+2)]#
    #これでfとdの両方の役割をこなす
    lf[1][1]=0
    while q:
        tmpij=q.popleft()
        #tmpd=qd.popleft()
        #df[tmpij]=1
        if tmpij==(H,W):
            break
        lind=[(-1,0),(1,0),(0,-1),(0,1)]#
        for i in range(4):
            #ntmpij=(tmpij[0]+(i<2)*(2*i-1),tmpij[1]+(i>1)*(2*i-5))
            ntmpij=(tmpij[0]+lind[i][0],tmpij[1]+lind[i][1])
            #print(ntmpij)
            #if ntmpij not in df and ls[ntmpij[0]][ntmpij[1]]==".":
            if lf[ntmpij[0]][ntmpij[1]]==float("inf") and ls[ntmpij[0]][ntmpij[1]]==".":
                #c_tmp=ctmp(ntmpij,tmp.dis+1)
                #q.append(c_tmp)
                q.append(ntmpij)
                #qd.append(tmpd+1)
                lf[ntmpij[0]][ntmpij[1]]=lf[tmpij[0]][tmpij[1]]+1
                #dは訪れる前に書き込んでしまう
    if tmpij==(H,W):
        nw=0
        for i in range(1,H+1):
            #for j in range(1,W+1):
             #   if ls[i][j]==".":
              #      nw+=1
            nw+=ls[i].count(".")
        #print(nw-tmpd-1)
        print(nw-lf[H][W]-1)
    else:
        print(-1)

if __name__=='__main__':
    main()

Buggy python submission (verdict: Time Limit Exceeded):
import sys
input=sys.stdin.readline
from collections import deque
#class ctmp():
#    def __init__(self,ij,dis):
#        self.ij=ij
#        self.dis=dis
H,W=map(int,input().split())
ls=["#"*(W+2)]
for i in range(H):
    ls.append("#"+input()+"#")
ls.append("#"*(W+2))
q=deque([])
qd=deque([])
#tmp=ctmp((1,1),0)
q.append((1,1))
qd.append(0)
df={}
while q:
    tmpij=q.popleft()
    tmpd=qd.popleft()
    df[tmpij]=1
    if tmpij==(H,W):
        break
    for i in range(4):
        ntmpij=(tmpij[0]+(i<2)*(2*i-1),tmpij[1]+(i>1)*(2*i-5))
        #print(ntmpij)
        if ntmpij not in df and ls[ntmpij[0]][ntmpij[1]]==".":
            #c_tmp=ctmp(ntmpij,tmp.dis+1)
            #q.append(c_tmp)
            q.append(ntmpij)
            qd.append(tmpd+1)
if tmpij==(H,W):
    nw=0
    for i in range(1,H+1):
        #for j in range(1,W+1):
         #   if ls[i][j]==".":
          #      nw+=1
        nw+=ls[i].count(".")
    print(nw-tmpd-1)
else:
    print(-1)


Fixed submission:
def main():
    import sys
    input=sys.stdin.readline
    from collections import deque
    #class ctmp():
    #    def __init__(self,ij,dis):
    #        self.ij=ij
    #        self.dis=dis
    H,W=map(int,input().split())
    ls=["#"*(W+2)]#
    for i in range(H):
        ls.append("#"+input()+"#")
    ls.append("#"*(W+2))#
    q=deque([])
    #qd=deque([])
    #tmp=ctmp((1,1),0)
    q.append((1,1))
    #qd.append(0)
    #df={}
    lf=[[float("inf") for _ in range(W+2)] for _ in range(H+2)]#
    #これでfとdの両方の役割をこなす
    lf[1][1]=0
    while q:
        tmpij=q.popleft()
        #tmpd=qd.popleft()
        #df[tmpij]=1
        if tmpij==(H,W):
            break
        lind=[(-1,0),(1,0),(0,-1),(0,1)]#
        for i in range(4):
            #ntmpij=(tmpij[0]+(i<2)*(2*i-1),tmpij[1]+(i>1)*(2*i-5))
            ntmpij=(tmpij[0]+lind[i][0],tmpij[1]+lind[i][1])
            #print(ntmpij)
            #if ntmpij not in df and ls[ntmpij[0]][ntmpij[1]]==".":
            if lf[ntmpij[0]][ntmpij[1]]==float("inf") and ls[ntmpij[0]][ntmpij[1]]==".":
                #c_tmp=ctmp(ntmpij,tmp.dis+1)
                #q.append(c_tmp)
                q.append(ntmpij)
                #qd.append(tmpd+1)
                lf[ntmpij[0]][ntmpij[1]]=lf[tmpij[0]][tmpij[1]]+1
                #dは訪れる前に書き込んでしまう
    if tmpij==(H,W):
        nw=0
        for i in range(1,H+1):
            #for j in range(1,W+1):
             #   if ls[i][j]==".":
              #      nw+=1
            nw+=ls[i].count(".")
        #print(nw-tmpd-1)
        print(nw-lf[H][W]-1)
    else:
        print(-1)

if __name__=='__main__':
    main()

Buggy python submission (verdict: Time Limit Exceeded):
import sys
input=sys.stdin.readline
from collections import deque
class ctmp():
    def __init__(self,ij,dis):
        self.ij=ij
        self.dis=dis
H,W=map(int,input().split())
ls=["#"*(W+2)]
for i in range(H):
    ls.append("#"+input()+"#")
ls.append("#"*(W+2))
q=deque([])
tmp=ctmp((1,1),0)
q.append(tmp)
df={}
while q:
    tmp=q.popleft()
    df[tmp.ij]=1
    if tmp.ij==(H,W):
        break
    for i in range(4):
        ntmpij=(tmp.ij[0]+(i<2)*(2*i-1),tmp.ij[1]+(i>1)*(2*i-5))
        #print(ntmpij)
        if ntmpij not in df and ls[ntmpij[0]][ntmpij[1]]==".":
            c_tmp=ctmp(ntmpij,tmp.dis+1)
            q.append(c_tmp)

if tmp.ij==(H,W):
    nw=0
    for i in range(1,H+1):
        #for j in range(1,W+1):
         #   if ls[i][j]==".":
          #      nw+=1
        nw+=ls[i].count(".")
    print(nw-tmp.dis-1)
else:
    print(-1)


Fixed submission:
def main():
    import sys
    input=sys.stdin.readline
    from collections import deque
    #class ctmp():
    #    def __init__(self,ij,dis):
    #        self.ij=ij
    #        self.dis=dis
    H,W=map(int,input().split())
    ls=["#"*(W+2)]#
    for i in range(H):
        ls.append("#"+input()+"#")
    ls.append("#"*(W+2))#
    q=deque([])
    #qd=deque([])
    #tmp=ctmp((1,1),0)
    q.append((1,1))
    #qd.append(0)
    #df={}
    lf=[[float("inf") for _ in range(W+2)] for _ in range(H+2)]#
    #これでfとdの両方の役割をこなす
    lf[1][1]=0
    while q:
        tmpij=q.popleft()
        #tmpd=qd.popleft()
        #df[tmpij]=1
        if tmpij==(H,W):
            break
        lind=[(-1,0),(1,0),(0,-1),(0,1)]#
        for i in range(4):
            #ntmpij=(tmpij[0]+(i<2)*(2*i-1),tmpij[1]+(i>1)*(2*i-5))
            ntmpij=(tmpij[0]+lind[i][0],tmpij[1]+lind[i][1])
            #print(ntmpij)
            #if ntmpij not in df and ls[ntmpij[0]][ntmpij[1]]==".":
            if lf[ntmpij[0]][ntmpij[1]]==float("inf") and ls[ntmpij[0]][ntmpij[1]]==".":
                #c_tmp=ctmp(ntmpij,tmp.dis+1)
                #q.append(c_tmp)
                q.append(ntmpij)
                #qd.append(tmpd+1)
                lf[ntmpij[0]][ntmpij[1]]=lf[tmpij[0]][tmpij[1]]+1
                #dは訪れる前に書き込んでしまう
    if tmpij==(H,W):
        nw=0
        for i in range(1,H+1):
            #for j in range(1,W+1):
             #   if ls[i][j]==".":
              #      nw+=1
            nw+=ls[i].count(".")
        #print(nw-tmpd-1)
        print(nw-lf[H][W]-1)
    else:
        print(-1)

if __name__=='__main__':
    main()

Buggy python submission (verdict: Time Limit Exceeded):
import sys
input=sys.stdin.readline
from collections import deque
class ctmp():
    def __init__(self,ij,dis):
        self.ij=ij
        self.dis=dis
H,W=map(int,input().split())
ls=["#"*(W+2)]
for i in range(H):
    ls.append("#"+input()+"#")
ls.append("#"*(W+2))
q=deque([])
tmp=ctmp((1,1),0)
q.append(tmp)
df={}
while q:
    tmp=q.popleft()
    df[tmp.ij]=1
    if tmp.ij==(H,W):
        break
    if (tmp.ij[0]-1,tmp.ij[1]) not in df and ls[tmp.ij[0]-1][tmp.ij[1]]==".":
        c_tmp=ctmp((tmp.ij[0]-1,tmp.ij[1]),tmp.dis+1)
        q.append(c_tmp)
    if (tmp.ij[0]+1,tmp.ij[1]) not in df and ls[tmp.ij[0]+1][tmp.ij[1]]==".":
        c_tmp=ctmp((tmp.ij[0]+1,tmp.ij[1]),tmp.dis+1)
        q.append(c_tmp)
    if (tmp.ij[0],tmp.ij[1]-1) not in df and ls[tmp.ij[0]][tmp.ij[1]-1]==".":
        c_tmp=ctmp((tmp.ij[0],tmp.ij[1]-1),tmp.dis+1)
        q.append(c_tmp)
    if (tmp.ij[0],tmp.ij[1]+1) not in df and ls[tmp.ij[0]][tmp.ij[1]+1]==".":
        c_tmp=ctmp((tmp.ij[0],tmp.ij[1]+1),tmp.dis+1)
        q.append(c_tmp)
if tmp.ij==(H,W):
    nw=0
    for i in range(1,H+1):
        #for j in range(1,W+1):
         #   if ls[i][j]==".":
          #      nw+=1
        nw+=ls[i].count(".")
    print(nw-tmp.dis-1)
else:
    print(-1)

Fixed submission:
def main():
    import sys
    input=sys.stdin.readline
    from collections import deque
    #class ctmp():
    #    def __init__(self,ij,dis):
    #        self.ij=ij
    #        self.dis=dis
    H,W=map(int,input().split())
    ls=["#"*(W+2)]#
    for i in range(H):
        ls.append("#"+input()+"#")
    ls.append("#"*(W+2))#
    q=deque([])
    #qd=deque([])
    #tmp=ctmp((1,1),0)
    q.append((1,1))
    #qd.append(0)
    #df={}
    lf=[[float("inf") for _ in range(W+2)] for _ in range(H+2)]#
    #これでfとdの両方の役割をこなす
    lf[1][1]=0
    while q:
        tmpij=q.popleft()
        #tmpd=qd.popleft()
        #df[tmpij]=1
        if tmpij==(H,W):
            break
        lind=[(-1,0),(1,0),(0,-1),(0,1)]#
        for i in range(4):
            #ntmpij=(tmpij[0]+(i<2)*(2*i-1),tmpij[1]+(i>1)*(2*i-5))
            ntmpij=(tmpij[0]+lind[i][0],tmpij[1]+lind[i][1])
            #print(ntmpij)
            #if ntmpij not in df and ls[ntmpij[0]][ntmpij[1]]==".":
            if lf[ntmpij[0]][ntmpij[1]]==float("inf") and ls[ntmpij[0]][ntmpij[1]]==".":
                #c_tmp=ctmp(ntmpij,tmp.dis+1)
                #q.append(c_tmp)
                q.append(ntmpij)
                #qd.append(tmpd+1)
                lf[ntmpij[0]][ntmpij[1]]=lf[tmpij[0]][tmpij[1]]+1
                #dは訪れる前に書き込んでしまう
    if tmpij==(H,W):
        nw=0
        for i in range(1,H+1):
            #for j in range(1,W+1):
             #   if ls[i][j]==".":
              #      nw+=1
            nw+=ls[i].count(".")
        #print(nw-tmpd-1)
        print(nw-lf[H][W]-1)
    else:
        print(-1)

if __name__=='__main__':
    main()

Buggy python submission (verdict: Time Limit Exceeded):
import sys
input=sys.stdin.readline
from collections import deque
class ctmp():
    def __init__(self,ij,dis):
        self.ij=ij
        self.dis=dis
H,W=map(int,input().split())
ls=["#"*(W+2)]
for i in range(H):
    ls.append("#"+input()+"#")
ls.append("#"*(W+2))
q=deque([])
tmp=ctmp((1,1),0)
q.append(tmp)
df={}
while q:
    tmp=q.popleft()
    df[tmp.ij]=1
    if tmp.ij==(H,W):
        break
    if (tmp.ij[0]-1,tmp.ij[1]) not in df and ls[tmp.ij[0]-1][tmp.ij[1]]==".":
        c_tmp=ctmp((tmp.ij[0]-1,tmp.ij[1]),tmp.dis+1)
        q.append(c_tmp)
    if (tmp.ij[0]+1,tmp.ij[1]) not in df and ls[tmp.ij[0]+1][tmp.ij[1]]==".":
        c_tmp=ctmp((tmp.ij[0]+1,tmp.ij[1]),tmp.dis+1)
        q.append(c_tmp)
    if (tmp.ij[0],tmp.ij[1]-1) not in df and ls[tmp.ij[0]][tmp.ij[1]-1]==".":
        c_tmp=ctmp((tmp.ij[0],tmp.ij[1]-1),tmp.dis+1)
        q.append(c_tmp)
    if (tmp.ij[0],tmp.ij[1]+1) not in df and ls[tmp.ij[0]][tmp.ij[1]+1]==".":
        c_tmp=ctmp((tmp.ij[0],tmp.ij[1]+1),tmp.dis+1)
        q.append(c_tmp)
if tmp.ij==(H,W):
    nw=0
    for i in range(1,H+1):
        for j in range(1,W+1):
            if ls[i][j]==".":
                nw+=1
    print(nw-tmp.dis-1)
else:
    print(-1)

Fixed submission:
def main():
    import sys
    input=sys.stdin.readline
    from collections import deque
    #class ctmp():
    #    def __init__(self,ij,dis):
    #        self.ij=ij
    #        self.dis=dis
    H,W=map(int,input().split())
    ls=["#"*(W+2)]#
    for i in range(H):
        ls.append("#"+input()+"#")
    ls.append("#"*(W+2))#
    q=deque([])
    #qd=deque([])
    #tmp=ctmp((1,1),0)
    q.append((1,1))
    #qd.append(0)
    #df={}
    lf=[[float("inf") for _ in range(W+2)] for _ in range(H+2)]#
    #これでfとdの両方の役割をこなす
    lf[1][1]=0
    while q:
        tmpij=q.popleft()
        #tmpd=qd.popleft()
        #df[tmpij]=1
        if tmpij==(H,W):
            break
        lind=[(-1,0),(1,0),(0,-1),(0,1)]#
        for i in range(4):
            #ntmpij=(tmpij[0]+(i<2)*(2*i-1),tmpij[1]+(i>1)*(2*i-5))
            ntmpij=(tmpij[0]+lind[i][0],tmpij[1]+lind[i][1])
            #print(ntmpij)
            #if ntmpij not in df and ls[ntmpij[0]][ntmpij[1]]==".":
            if lf[ntmpij[0]][ntmpij[1]]==float("inf") and ls[ntmpij[0]][ntmpij[1]]==".":
                #c_tmp=ctmp(ntmpij,tmp.dis+1)
                #q.append(c_tmp)
                q.append(ntmpij)
                #qd.append(tmpd+1)
                lf[ntmpij[0]][ntmpij[1]]=lf[tmpij[0]][tmpij[1]]+1
                #dは訪れる前に書き込んでしまう
    if tmpij==(H,W):
        nw=0
        for i in range(1,H+1):
            #for j in range(1,W+1):
             #   if ls[i][j]==".":
              #      nw+=1
            nw+=ls[i].count(".")
        #print(nw-tmpd-1)
        print(nw-lf[H][W]-1)
    else:
        print(-1)

if __name__=='__main__':
    main()

Buggy python submission (verdict: Time Limit Exceeded):
import sys
input=sys.stdin.readline
from collections import deque
class ctmp():
    def __init__(self,ij,dis):
        self.ij=ij
        self.dis=dis
H,W=map(int,input().split())
ls=["#"*(W+2)]
for i in range(H):
    ls.append("#"+input()+"#")
ls.append("#"*(W+2))
q=deque([])
tmp=ctmp((1,1),0)
q.append(tmp)
df={}
while q:
    tmp=q.popleft()
    df[tmp.ij]=1
    if tmp.ij==(H,W):
        break
    if (tmp.ij[0]-1,tmp.ij[1]) not in df and ls[tmp.ij[0]-1][tmp.ij[1]]==".":
        c_tmp=ctmp((tmp.ij[0]-1,tmp.ij[1]),tmp.dis+1)
        q.append(c_tmp)
    if (tmp.ij[0]+1,tmp.ij[1]) not in df and ls[tmp.ij[0]+1][tmp.ij[1]]==".":
        c_tmp=ctmp((tmp.ij[0]+1,tmp.ij[1]),tmp.dis+1)
        q.append(c_tmp)
    if (tmp.ij[0],tmp.ij[1]-1) not in df and ls[tmp.ij[0]][tmp.ij[1]-1]==".":
        c_tmp=ctmp((tmp.ij[0],tmp.ij[1]-1),tmp.dis+1)
        q.append(c_tmp)
    if (tmp.ij[0],tmp.ij[1]+1) not in df and ls[tmp.ij[0]][tmp.ij[1]+1]==".":
        c_tmp=ctmp((tmp.ij[0],tmp.ij[1]+1),tmp.dis+1)
        q.append(c_tmp)
if tmp.ij==(H,W):
    nw=0
    for i in range(1,H+1):
        for j in range(1,W+1):
            if ls[i][j]==".":
                nw+=1
    print(nw-tmp.dis-1)
else:
    print(-1)

Fixed submission:
def main():
    import sys
    input=sys.stdin.readline
    from collections import deque
    #class ctmp():
    #    def __init__(self,ij,dis):
    #        self.ij=ij
    #        self.dis=dis
    H,W=map(int,input().split())
    ls=["#"*(W+2)]#
    for i in range(H):
        ls.append("#"+input()+"#")
    ls.append("#"*(W+2))#
    q=deque([])
    #qd=deque([])
    #tmp=ctmp((1,1),0)
    q.append((1,1))
    #qd.append(0)
    #df={}
    lf=[[float("inf") for _ in range(W+2)] for _ in range(H+2)]#
    #これでfとdの両方の役割をこなす
    lf[1][1]=0
    while q:
        tmpij=q.popleft()
        #tmpd=qd.popleft()
        #df[tmpij]=1
        if tmpij==(H,W):
            break
        lind=[(-1,0),(1,0),(0,-1),(0,1)]#
        for i in range(4):
            #ntmpij=(tmpij[0]+(i<2)*(2*i-1),tmpij[1]+(i>1)*(2*i-5))
            ntmpij=(tmpij[0]+lind[i][0],tmpij[1]+lind[i][1])
            #print(ntmpij)
            #if ntmpij not in df and ls[ntmpij[0]][ntmpij[1]]==".":
            if lf[ntmpij[0]][ntmpij[1]]==float("inf") and ls[ntmpij[0]][ntmpij[1]]==".":
                #c_tmp=ctmp(ntmpij,tmp.dis+1)
                #q.append(c_tmp)
                q.append(ntmpij)
                #qd.append(tmpd+1)
                lf[ntmpij[0]][ntmpij[1]]=lf[tmpij[0]][tmpij[1]]+1
                #dは訪れる前に書き込んでしまう
    if tmpij==(H,W):
        nw=0
        for i in range(1,H+1):
            #for j in range(1,W+1):
             #   if ls[i][j]==".":
              #      nw+=1
            nw+=ls[i].count(".")
        #print(nw-tmpd-1)
        print(nw-lf[H][W]-1)
    else:
        print(-1)

if __name__=='__main__':
    main()

Buggy python submission (verdict: Time Limit Exceeded):
#なんかどうやらBFSにおいて自分含め誰かが訪れた点は行かなくてよい
from collections import deque
class ctmp():
    #def __init__(self,ij,df_ij,df_c,dis):
    #def __init__(self,ij,pij,df_ij,df_c,dis):
    def __init__(self,ij,dis):
        self.ij=ij
        #self.pij=pij
        #self.lf=lf
        #self.df_ij=df_ij
        #self.df_c=df_c#これ要らなくね？
        self.dis=dis
H,W=map(int,input().split())
#ls=[["#"]*(W+2)]
#ls=[["#"*(W+2)]]
ls=["#"*(W+2)]
for i in range(H):
    #ls.append(["#"]+list(map(int,input().split()))+["#"])
    #ls.append(["#"]+input().split()+["#"])
    #s=input()
    #ls.append(["#"+input()+"#"])
    ls.append("#"+input()+"#")
#ls.append(["#"]*(W+2))
ls.append("#"*(W+2))
#print("ls",ls)
#print(ls[0])
#print(ls[0][0])
q=deque([])
#tmp=(1,1)
#tmp=ctmp((1,1),{(1,1):1},0)
#tmp=ctmp((1,1),{},{},0)
#tmp=ctmp((1,1),(0,0),{},{},0)
tmp=ctmp((1,1),0)
q.append(tmp)
#lf=[]
#for i in range(H):
#dng={}
df={}
#while tmp.ij!=(H,W) and q:
while q:
    #f=0#行き止まり判定
    #if tmpls[tmp[0]-1,tmp[1]]="."
    #print(q)
    tmp=q.popleft()
    df[tmp.ij]=1
    #print(tmp.ij)
    if tmp.ij==(H,W):
        break
    #ind=bisect_left(tmp.lf,(tmp.ij[0]-1,tmp.ij[1]))
    #if lf[ind]!=lf[ind+1] and ls[tmp.ij[0]][tmp.ij[1]]=".":
    #    qin=tmp((tmp.ij[0]-1,tmp.ij[1]),tmp.lf)
    #    q.append()
    #if (tmp.ij[0]-1,tmp.ij[1]) not in tmp.df_ij and ls[tmp.ij[0]-1][tmp.ij[1]]=="." and (tmp.ij[0]-1,tmp.ij[1]) not in dng:
    if (tmp.ij[0]-1,tmp.ij[1]) not in df and ls[tmp.ij[0]-1][tmp.ij[1]]==".":
        #c_df_ij=copy.copy(tmp.df_ij)
        #c_df_ij[(tmp.ij[0],tmp.ij[1])]=1
        #c_df_c=copy.copy(tmp.df_c)
        #c_df_c[tmp]=1
        c_tmp=ctmp((tmp.ij[0]-1,tmp.ij[1]),tmp.dis+1)
        q.append(c_tmp)
        #f=1
        #print(1)
    if (tmp.ij[0]+1,tmp.ij[1]) not in df and ls[tmp.ij[0]+1][tmp.ij[1]]==".":
        #c_df_ij=copy.copy(tmp.df_ij)
        #c_df_ij[(tmp.ij[0],tmp.ij[1])]=1
        #c_df_c=copy.copy(tmp.df_c)
        #c_df_c[tmp]=1
        c_tmp=ctmp((tmp.ij[0]+1,tmp.ij[1]),tmp.dis+1)
        q.append(c_tmp)
        #f=1
        #print(2)
    if (tmp.ij[0],tmp.ij[1]-1) not in df and ls[tmp.ij[0]][tmp.ij[1]-1]==".":
        #c_df_ij=copy.copy(tmp.df_ij)
        #c_df_ij[(tmp.ij[0],tmp.ij[1])]=1
        #c_df_c=copy.copy(tmp.df_c)
        #c_df_c[tmp]=1
        c_tmp=ctmp((tmp.ij[0],tmp.ij[1]-1),tmp.dis+1)
        q.append(c_tmp)
        #f=1
        #print(3)
    #print((tmp.ij[0],tmp.ij[1]+1) not in tmp.df_ij)
    #print(tmp.ij[0],tmp.ij[1]+1,ls[tmp.ij[0]][tmp.ij[1]+1]==".")
    if (tmp.ij[0],tmp.ij[1]+1) not in df and ls[tmp.ij[0]][tmp.ij[1]+1]==".":
        #c_df_ij=copy.copy(tmp.df_ij)
        #c_df_ij[(tmp.ij[0],tmp.ij[1])]=1
        #c_df_c=copy.copy(tmp.df_c)
        #c_df_c[tmp]=1
        c_tmp=ctmp((tmp.ij[0],tmp.ij[1]+1),tmp.dis+1)
        q.append(c_tmp)
        #f=1
        #print(4)
    #if f==0:
    #    dng[tmp.ij]=1
if tmp.ij==(H,W):
    nw=0
    for i in range(1,H+1):
        for j in range(1,W+1):
            if ls[i][j]==".":
                nw+=1
    print(nw-tmp.dis-1)
else:
    print(-1)


Fixed submission:
def main():
    import sys
    input=sys.stdin.readline
    from collections import deque
    #class ctmp():
    #    def __init__(self,ij,dis):
    #        self.ij=ij
    #        self.dis=dis
    H,W=map(int,input().split())
    ls=["#"*(W+2)]#
    for i in range(H):
        ls.append("#"+input()+"#")
    ls.append("#"*(W+2))#
    q=deque([])
    #qd=deque([])
    #tmp=ctmp((1,1),0)
    q.append((1,1))
    #qd.append(0)
    #df={}
    lf=[[float("inf") for _ in range(W+2)] for _ in range(H+2)]#
    #これでfとdの両方の役割をこなす
    lf[1][1]=0
    while q:
        tmpij=q.popleft()
        #tmpd=qd.popleft()
        #df[tmpij]=1
        if tmpij==(H,W):
            break
        lind=[(-1,0),(1,0),(0,-1),(0,1)]#
        for i in range(4):
            #ntmpij=(tmpij[0]+(i<2)*(2*i-1),tmpij[1]+(i>1)*(2*i-5))
            ntmpij=(tmpij[0]+lind[i][0],tmpij[1]+lind[i][1])
            #print(ntmpij)
            #if ntmpij not in df and ls[ntmpij[0]][ntmpij[1]]==".":
            if lf[ntmpij[0]][ntmpij[1]]==float("inf") and ls[ntmpij[0]][ntmpij[1]]==".":
                #c_tmp=ctmp(ntmpij,tmp.dis+1)
                #q.append(c_tmp)
                q.append(ntmpij)
                #qd.append(tmpd+1)
                lf[ntmpij[0]][ntmpij[1]]=lf[tmpij[0]][tmpij[1]]+1
                #dは訪れる前に書き込んでしまう
    if tmpij==(H,W):
        nw=0
        for i in range(1,H+1):
            #for j in range(1,W+1):
             #   if ls[i][j]==".":
              #      nw+=1
            nw+=ls[i].count(".")
        #print(nw-tmpd-1)
        print(nw-lf[H][W]-1)
    else:
        print(-1)

if __name__=='__main__':
    main()

Buggy python submission (verdict: Time Limit Exceeded):
#なんかどうやらBFSにおいて自分含め誰かが訪れた点は行かなくてよい
from collections import deque
class ctmp():
    #def __init__(self,ij,df_ij,df_c,dis):
    #def __init__(self,ij,pij,df_ij,df_c,dis):
    def __init__(self,ij,dis):
        self.ij=ij
        #self.pij=pij
        #self.lf=lf
        #self.df_ij=df_ij
        #self.df_c=df_c#これ要らなくね？
        self.dis=dis
H,W=map(int,input().split())
#ls=[["#"]*(W+2)]
#ls=[["#"*(W+2)]]
ls=["#"*(W+2)]
for i in range(H):
    #ls.append(["#"]+list(map(int,input().split()))+["#"])
    #ls.append(["#"]+input().split()+["#"])
    #s=input()
    #ls.append(["#"+input()+"#"])
    ls.append("#"+input()+"#")
#ls.append(["#"]*(W+2))
ls.append("#"*(W+2))
#print("ls",ls)
#print(ls[0])
#print(ls[0][0])
q=deque([])
#tmp=(1,1)
#tmp=ctmp((1,1),{(1,1):1},0)
#tmp=ctmp((1,1),{},{},0)
#tmp=ctmp((1,1),(0,0),{},{},0)
tmp=ctmp((1,1),0)
q.append(tmp)
#lf=[]
#for i in range(H):
#dng={}
df={}
#while tmp.ij!=(H,W) and q:
while q:
    #f=0#行き止まり判定
    #if tmpls[tmp[0]-1,tmp[1]]="."
    #print(q)
    tmp=q.popleft()
    df[tmp.ij]=1
    #print(tmp.ij)
    if tmp.ij==(H,W):
        break
    #ind=bisect_left(tmp.lf,(tmp.ij[0]-1,tmp.ij[1]))
    #if lf[ind]!=lf[ind+1] and ls[tmp.ij[0]][tmp.ij[1]]=".":
    #    qin=tmp((tmp.ij[0]-1,tmp.ij[1]),tmp.lf)
    #    q.append()
    #if (tmp.ij[0]-1,tmp.ij[1]) not in tmp.df_ij and ls[tmp.ij[0]-1][tmp.ij[1]]=="." and (tmp.ij[0]-1,tmp.ij[1]) not in dng:
    if (tmp.ij[0]-1,tmp.ij[1]) not in df and ls[tmp.ij[0]-1][tmp.ij[1]]==".":
        #c_df_ij=copy.copy(tmp.df_ij)
        #c_df_ij[(tmp.ij[0],tmp.ij[1])]=1
        #c_df_c=copy.copy(tmp.df_c)
        #c_df_c[tmp]=1
        c_tmp=ctmp((tmp.ij[0]-1,tmp.ij[1]),tmp.dis+1)
        q.append(c_tmp)
        #f=1
        #print(1)
    if (tmp.ij[0]+1,tmp.ij[1]) not in df and ls[tmp.ij[0]+1][tmp.ij[1]]==".":
        #c_df_ij=copy.copy(tmp.df_ij)
        #c_df_ij[(tmp.ij[0],tmp.ij[1])]=1
        #c_df_c=copy.copy(tmp.df_c)
        #c_df_c[tmp]=1
        c_tmp=ctmp((tmp.ij[0]+1,tmp.ij[1]),tmp.dis+1)
        q.append(c_tmp)
        #f=1
        #print(2)
    if (tmp.ij[0],tmp.ij[1]-1) not in df and ls[tmp.ij[0]][tmp.ij[1]-1]==".":
        #c_df_ij=copy.copy(tmp.df_ij)
        #c_df_ij[(tmp.ij[0],tmp.ij[1])]=1
        #c_df_c=copy.copy(tmp.df_c)
        #c_df_c[tmp]=1
        c_tmp=ctmp((tmp.ij[0],tmp.ij[1]-1),tmp.dis+1)
        q.append(c_tmp)
        #f=1
        #print(3)
    #print((tmp.ij[0],tmp.ij[1]+1) not in tmp.df_ij)
    #print(tmp.ij[0],tmp.ij[1]+1,ls[tmp.ij[0]][tmp.ij[1]+1]==".")
    if (tmp.ij[0],tmp.ij[1]+1) not in df and ls[tmp.ij[0]][tmp.ij[1]+1]==".":
        #c_df_ij=copy.copy(tmp.df_ij)
        #c_df_ij[(tmp.ij[0],tmp.ij[1])]=1
        #c_df_c=copy.copy(tmp.df_c)
        #c_df_c[tmp]=1
        c_tmp=ctmp((tmp.ij[0],tmp.ij[1]+1),tmp.dis+1)
        q.append(c_tmp)
        #f=1
        #print(4)
    #if f==0:
    #    dng[tmp.ij]=1
if tmp.ij==(H,W):
    nw=0
    for i in range(1,H+1):
        for j in range(1,W+1):
            if ls[i][j]==".":
                nw+=1
    print(nw-tmp.dis-1)
else:
    print(-1)


Fixed submission:
def main():
    import sys
    input=sys.stdin.readline
    from collections import deque
    #class ctmp():
    #    def __init__(self,ij,dis):
    #        self.ij=ij
    #        self.dis=dis
    H,W=map(int,input().split())
    ls=["#"*(W+2)]#
    for i in range(H):
        ls.append("#"+input()+"#")
    ls.append("#"*(W+2))#
    q=deque([])
    #qd=deque([])
    #tmp=ctmp((1,1),0)
    q.append((1,1))
    #qd.append(0)
    #df={}
    lf=[[float("inf") for _ in range(W+2)] for _ in range(H+2)]#
    #これでfとdの両方の役割をこなす
    lf[1][1]=0
    while q:
        tmpij=q.popleft()
        #tmpd=qd.popleft()
        #df[tmpij]=1
        if tmpij==(H,W):
            break
        lind=[(-1,0),(1,0),(0,-1),(0,1)]#
        for i in range(4):
            #ntmpij=(tmpij[0]+(i<2)*(2*i-1),tmpij[1]+(i>1)*(2*i-5))
            ntmpij=(tmpij[0]+lind[i][0],tmpij[1]+lind[i][1])
            #print(ntmpij)
            #if ntmpij not in df and ls[ntmpij[0]][ntmpij[1]]==".":
            if lf[ntmpij[0]][ntmpij[1]]==float("inf") and ls[ntmpij[0]][ntmpij[1]]==".":
                #c_tmp=ctmp(ntmpij,tmp.dis+1)
                #q.append(c_tmp)
                q.append(ntmpij)
                #qd.append(tmpd+1)
                lf[ntmpij[0]][ntmpij[1]]=lf[tmpij[0]][tmpij[1]]+1
                #dは訪れる前に書き込んでしまう
    if tmpij==(H,W):
        nw=0
        for i in range(1,H+1):
            #for j in range(1,W+1):
             #   if ls[i][j]==".":
              #      nw+=1
            nw+=ls[i].count(".")
        #print(nw-tmpd-1)
        print(nw-lf[H][W]-1)
    else:
        print(-1)

if __name__=='__main__':
    main()