Problem p02803 (python)

Fixed submission:
h, w = map(int,input().split())
grid = []
grid.append('#' * (w+2))
for _ in range(h):
    grid.append('#' + input().rstrip() + '#')
grid.append('#' * (w+2))
            
from collections import deque
import sys
sys.setrecursionlimit(1000000)


def flatten_2dim(array):
    return [item for sublist in array for item in sublist]

maxstep = 0
step = [[-1] * (w+2) for _ in range(h+2)]
def bfs():
    global maxstep
    dy = [1, 0, -1, 0]
    dx = [0, 1, 0, -1]
    
    que = deque([])
    que.append((s_y, s_x))
    step[s_y][s_x] = 0
    
    while que:
        y, x = que.popleft()

        for i in range(4):
            ny = y + dy[i]
            nx = x + dx[i]
            if step[ny][nx] == -1 and grid[ny][nx] != "#":
                que.append((ny, nx))
                step[ny][nx] = step[y][x] + 1

    maxstep = max(max(flatten_2dim(step)), maxstep)
    
for i in range(h+2):
    for j in range(w+2):
        if grid[i][j] == ".":
            s_y, s_x = i, j
            bfs()
            step = [[-1] * (w+2) for _ in range(h+2)]
print(maxstep)

Buggy submission (verdict: Runtime Error):
h, w = map(int,input().split())
grid = []
grid.append('#' * (w+2))
for _ in range(h):
    grid.append('#' + input().rstrip() + '#')
grid.append('#' * (w+2))
            
from collections import deque
import sys
sys.setrecursionlimit(1000000)


def flatten_2dim(array):
    return [item for sublist in array for item in sublist]
def all_argmax_2dim(array):
    flat_array = flatten_2dim(array)

    row_num = len(array)
    max_value = max(flat_array)

    for i, elem in enumerate(flat_array):
        if elem == max_value:
            yield i // row_num, i % row_num

maxstep = 0
flag = 0
step = [[-1] * (w+2) for _ in range(h+2)]
def bfs():
    global maxstep

    dy = [1, 0, -1, 0]
    dx = [0, 1, 0, -1]
    
    que = deque([])
    que.append((s_y, s_x))
    step[s_y][s_x] = 0
    
    while que:
        y, x = que.popleft()

        for i in range(4):
            ny = y + dy[i]
            nx = x + dx[i]
            if step[ny][nx] == -1 and grid[ny][nx] != "#":
                que.append((ny, nx))
                step[ny][nx] = step[y][x] + 1
    if flag == 0:
        maxstep = max(flatten_2dim(step))
    else:
        maxstep = max(max(flatten_2dim(step)), maxstep)
    
for i in range(h+2):
    for j in range(w+2):
        if grid[i][j] == ".":
            s_y, s_x = i, j
            break
bfs()
flag = 1
li = []
for argmax in all_argmax_2dim(step):
    li.append(argmax)

for s_y, s_x in li:
    bfs()
for argmax in all_argmax_2dim(step):
    li.append(argmax)
for s_y, s_x in li:
    bfs()   
print(maxstep)

Buggy submission (verdict: Runtime Error):
h, w = map(int,input().split())
grid = []
grid.append('#' * (w+2))
for _ in range(h):
    grid.append('#' + input().rstrip() + '#')
grid.append('#' * (w+2))
            
from collections import deque
import sys
sys.setrecursionlimit(1000000)


def flatten_2dim(array):
    return [item for sublist in array for item in sublist]
def all_argmax_2dim(array):
    flat_array = flatten_2dim(array)

    row_num = len(array)
    max_value = max(flat_array)

    for i, elem in enumerate(flat_array):
        if elem == max_value:
            yield i // row_num, i % row_num

maxstep = 0
flag = 0
step = [[-1] * (w+2) for _ in range(h+2)]
def bfs():
    global maxstep

    dy = [1, 0, -1, 0]
    dx = [0, 1, 0, -1]
    
    que = deque([])
    que.append((s_y, s_x))
    step[s_y][s_x] = 0
    
    while que:
        y, x = que.popleft()

        for i in range(4):
            ny = y + dy[i]
            nx = x + dx[i]
            if step[ny][nx] == -1 and grid[ny][nx] != "#":
                que.append((ny, nx))
                step[ny][nx] = step[y][x] + 1
    if flag == 0:
        maxstep = max(flatten_2dim(step))
    else:
        maxstep = max(max(flatten_2dim(step)), maxstep)
    
for i in range(h+2):
    for j in range(w+2):
        if grid[i][j] == ".":
            s_y, s_x = i, j
            break
bfs()
flag = 1
li = []
for argmax in all_argmax_2dim(step):
    li.append(argmax)

for s_y, s_x in li:
    bfs()
print(maxstep)