Problem p02803 (python)

Fixed submission:
H,W = map(int,input().split())
D = [[0]*(W) for i in range(H)]

for i in range(H):
    S = input()
    for j in range(W):
        s = S[j]
        if s=='.':
            D[i][j] = 1

ans = 0
import heapq

for i in range(H):
    for j in range(W):
        if D[i][j] == 1:
            tmp = 0
            h = [[0,i,j]]
            visited = [[0]*W for i in range(H)]
            while h!=[]:
                w,nowi,nowj = heapq.heappop(h)
                tmp = max(tmp,w)
                visited[nowi][nowj] = 1
                if 0<nowi<H-1:
                    for k in [-1,1]:
                        if D[nowi+k][nowj]==1 and visited[nowi+k][nowj]==0:
                            heapq.heappush(h,[w+1,nowi+k,nowj])
                            visited[nowi+k][nowj]=1
                elif nowi+1<=H-1:
                    if D[nowi+1][nowj]==1 and visited[nowi+1][nowj]==0:
                        heapq.heappush(h,[w+1,nowi+1,nowj])
                        visited[nowi+1][nowj]=1
                elif nowi-1 >=0:
                    if D[nowi-1][nowj]==1 and visited[nowi-1][nowj]==0:
                        heapq.heappush(h,[w+1,nowi-1,nowj])
                        visited[nowi-1][nowj]=1

                if 0<nowj<W-1:
                    for k in [-1,1]:
                        if D[nowi][nowj+k]==1 and visited[nowi][nowj+k]==0:
                            heapq.heappush(h,[w+1,nowi,nowj+k])
                            visited[nowi][nowj+k]=1
                elif nowj-1>=0:
                    if D[nowi][nowj-1]==1 and visited[nowi][nowj-1]==0:
                        heapq.heappush(h,[w+1,nowi,nowj-1])
                        visited[nowi][nowj-1]=1
                elif nowj+1 <= W-1:
                    if D[nowi][nowj+1]==1 and visited[nowi][nowj+1]==0:
                        heapq.heappush(h,[w+1,nowi,nowj+1])
                        visited[nowi][nowj+1]=1
            ans = max(ans,tmp)
            
print(ans)

Buggy submission (verdict: Runtime Error):
H,W = map(int,input().split())
D = [[0]*(W) for i in range(H)]

for i in range(H):
    S = input()
    for j in range(W):
        s = S[j]
        if s=='.':
            D[i][j] = 1

ans = 0
import heapq

for i in range(H):
    for j in range(W):
        if D[i][j] == 1:
            tmp = 0
            h = [[0,i,j]]
            visited = [[0]*W for i in range(H)]
            while h!=[]:
                w,nowi,nowj = heapq.heappop(h)
                tmp = max(tmp,w)
                visited[nowi][nowj] = 1
                if 0<nowi<H-1:
                    for k in [-1,1]:
                        if D[nowi+k][nowj]==1 and visited[nowi+k][nowj]==0:
                            heapq.heappush(h,[w+1,nowi+k,nowj])
                            visited[nowi+k][nowj]=1
                elif nowi==0:
                    if D[nowi+1][nowj]==1 and visited[nowi+1][nowj]==0:
                        heapq.heappush(h,[w+1,nowi+1,nowj])
                        visited[nowi+1][nowj]=1
                else:
                    if D[nowi-1][nowj]==1 and visited[nowi-1][nowj]==0:
                        heapq.heappush(h,[w+1,nowi-1,nowj])
                        visited[nowi-1][nowj]=1

                if 0<nowj<W-1:
                    for k in [-1,1]:
                        if D[nowi][nowj+k]==1 and visited[nowi][nowj+k]==0:
                            heapq.heappush(h,[w+1,nowi,nowj+k])
                            visited[nowi][nowj+k]=1
                elif nowj == W-1:
                    if D[nowi][nowj-1]==1 and visited[nowi][nowj-1]==0:
                        heapq.heappush(h,[w+1,nowi,nowj-1])
                        visited[nowi][nowj-1]=1
                else:
                    if D[nowi][nowj+1]==1 and visited[nowi][nowj+1]==0:
                        heapq.heappush(h,[w+1,nowi,nowj+1])
                        visited[nowi][nowj+1]=1
            ans = max(ans,tmp)
            
print(ans)