Problem p02803 (python)

Fixed submission:
from collections import deque

h,w = map(int,input().split())
maze = []
ans = 0
dy_dx = [[0,1],[1,0],[0,-1],[-1,0]]
for i in range(h):
    maze.append(input())
flag = True
check = '.'*w
for i in range(h):
    if maze[i] != check:
        flag = False
        break
if flag:
    print(h+w-2)
else:
    for i in range(h):
        for j in range(w):
            if maze[i][j] == '.':
                ne = deque([[i,j]])
                dist = [[0]*w for k in range(h)]
                checked = [[0]*w for k in range(h)]
                while len(ne) != 0:
                    cur = ne.popleft()
                    checked[cur[0]][cur[1]] = 1
                    for l in range(4):
                        t = cur[0]+dy_dx[l][0]
                        u = cur[1]+dy_dx[l][1]
                        if (0 <= t <= h-1) and (0 <= u <= w-1):
                            if checked[t][u] == 0:
                                if maze[t][u] == '.':
                                    dist[t][u] = dist[cur[0]][cur[1]]+1
                                    ne.append([t,u])
                                    ans = max(ans,dist[t][u])
    print(ans)

Buggy submission (verdict: Runtime Error):
import collections

h,w = map(int,input().split())
maze = []
ans = 0
dy_dx = [[0,1],[1,0],[0,-1],[-1,0]]
for i in range(h):
    s = input()
    a = []
    for j in range(w):
        a.append(s[j])
    maze.append(a)
for i in range(h):
    for j in range(w):
        if maze[i][j] != '#':
            next = collections.deque()
            dist = []
            checked = []
            curmax = 0
            for k in range(h):
                a = [0]*w
                b = [0]*w
                dist.append(a)
                checked.append(b)
            next.append([i,j])
            while len(next) != 0:
                cur = next.popleft()
                checked[cur[0]][cur[1]] = 1
                for l in range(4):
                    t = cur[0]+dy_dx[l][0]
                    u = cur[1]+dy_dx[l][1]
                    if (0 <= t <= h-1) and (0 <= u <= w-1):
                        if checked[t][u] == 0:
                            if maze[t][u] == '.':
                                dist[t][u] = dist[cur[0]][cur[1]]+1
                                next.append([t,u])
                                curmax = max(curmax,dist[t][u])
        ans = max(ans,curmax)
print(ans)

Buggy submission (verdict: Runtime Error):
import collections

h,w = map(int,input().split())
maze = []
ans = 0
dy_dx = [[0,1],[1,0],[0,-1],[-1,0]]
for i in range(h):
    s = input()
    a = []
    for j in range(w):
        a.append(s[j])
    maze.append(a)
for i in range(h):
    for j in range(w):
        if maze[i][j] != '#':
            next = collections.deque()
            dist = []
            checked = []
            curmax = 0
            for k in range(h):
                a = [0]*w
                b = [0]*w
                dist.append(a)
                checked.append(b)
            next.append([i,j])
            while len(next) != 0:
                cur = next.popleft()
                checked[cur[0]][cur[1]] = 1
                for l in range(4):
                    if (0 <= cur[0]+dy_dx[l][0] <= h-1) and (0 <= cur[1]+dy_dx[l][1] <= w-1):
                        if checked[cur[0]+dy_dx[l][0]][cur[1]+dy_dx[l][1]] == 0:
                            if maze[cur[0]+dy_dx[l][0]][cur[1]+dy_dx[l][1]] == '.':
                                dist[cur[0]+dy_dx[l][0]][cur[1]+dy_dx[l][1]] = dist[cur[0]][cur[1]]+1
                                next.append([cur[0]+dy_dx[l][0],cur[1]+dy_dx[l][1]])
                                curmax = max(curmax,dist[cur[0]+dy_dx[l][0]][cur[1]+dy_dx[l][1]])
        ans = max(ans,curmax)
print(ans)

Buggy submission (verdict: Runtime Error):
import collections

h,w = map(int,input().split())
maze = []
ans = 0
dy_dx = [[0,1],[1,0],[0,-1],[-1,0]]
for i in range(h):
    s = input()
    a = []
    for j in range(w):
        a.append(s[j])
    maze.append(a)
for i in range(h):
    for j in range(w):
        if maze[i][j] != '#':
            next = collections.deque()
            dist = []
            checked = []
            curmax = 0
            for k in range(h):
                a = [0]*w
                b = [0]*w
                dist.append(a)
                checked.append(b)
            next.append([i,j])
            while len(next) != 0:
                cur = next.popleft()
                checked[cur[0]][cur[1]] = 1
                for l in range(4):
                    if (0 <= cur[0]+dy_dx[l][0] <= h-1) and (0 <= cur[1]+dy_dx[l][1] <= w-1):
                        if checked[cur[0]+dy_dx[l][0]][cur[1]+dy_dx[l][1]] == 0:
                            if maze[cur[0]+dy_dx[l][0]][cur[1]+dy_dx[l][1]] == '.':
                                dist[cur[0]+dy_dx[l][0]][cur[1]+dy_dx[l][1]] = dist[cur[0]][cur[1]]+1
                                next.append([cur[0]+dy_dx[l][0],cur[1]+dy_dx[l][1]])
                                curmax = max(curmax,dist[cur[0]+dy_dx[l][0]][cur[1]+dy_dx[l][1]])
        ans = max(ans,curmax)
print(ans)