Buggy python submission (verdict: Runtime Error):
    H, W = map(int, input().split())
    S = [list(input()) for _ in range(H)]
     
    max_step = 0
    for i in range(H * W):
        sy, sx = i // W, i % W
        if S[sy][sx] == "#":
            continue
     
        steps = [[-1] * W for _ in range(H)]
        steps[sy][sx] = 0
        queue = [(sy, sx)]
        while len(queue) >= 1:
            cy, cx = queue.pop(0)
            for dy, dx in ((0, 1), (0, -1), (1, 0), (-1, 0)):
                ny, nx = cy + dy, cx + dx
                if not (0 <= ny <= H - 1 and 0 <= nx <= W - 1):
                    continue
                if S[ny][nx] == "#" or steps[ny][nx] >= 0:
                    continue
                queue.append((ny, nx))
                steps[ny][nx] = steps[cy][cx] + 1
     
        ms = max(max(r) for r in steps)
        max_step = max(ms, max_step)
     
    print(max_step)

Fixed submission:
H, W = map(int, input().split())
S = [list(input()) for _ in range(H)]

max_step = 0
for i in range(H * W):
    sy, sx = i // W, i % W
    if S[sy][sx] == "#":
        continue

    steps = [[-1] * W for _ in range(H)]
    steps[sy][sx] = 0
    queue = [(sy, sx)]
    while len(queue) >= 1:
        cy, cx = queue.pop(0)
        for dy, dx in ((0, 1), (0, -1), (1, 0), (-1, 0)):
            ny, nx = cy + dy, cx + dx
            if not (0 <= ny <= H - 1 and 0 <= nx <= W - 1):
                continue
            if S[ny][nx] == "#" or steps[ny][nx] >= 0:
                continue
            queue.append((ny, nx))
            steps[ny][nx] = steps[cy][cx] + 1

    ms = max(max(r) for r in steps)
    max_step = max(ms, max_step)

print(max_step)

Buggy python submission (verdict: Time Limit Exceeded):
from itertools import combinations, product
import copy
H, W = map(int, input().split())


def search_steps(maze, cy, cx, gy, gx, step):
    if (cy, cx) == (gy, gx):
        return step
    ss = []
    for dy, dx in ((0, 1), (0, -1), (1, 0), (-1, 0)):
        ny, nx = cy + dy, cx + dx
        new_maze = copy.deepcopy(maze)
        new_maze[cy][cx] = "#"
        if 0 <= ny <= H - 1 and 0 <= nx <= W - 1 and maze[ny][nx] == ".":
            s = search_steps(new_maze, ny, nx, gy, gx, step + 1)
            if s >= 0:
                ss.append(s)

    if len(ss) == 0:
        return -1
    else:
        mss = min(ss)
        return min(ss) if mss >= 0 else -1


S = [0] * H
for i in range(H):
    S[i] = list(input())

max_steps = 0
for s, g in combinations(range(H * W), 2):
    sy, sx = s // W, s % W
    gy, gx = g // W, g % W
    if S[sy][sx] == "#" or S[gy][gx] == "#":
        continue

    steps = search_steps(copy.deepcopy(S), sy, sx, gy, gx, 0)
    if steps > max_steps:
        max_steps = steps
    if steps == H * W - 1:
        break

print(max_steps)


Fixed submission:
H, W = map(int, input().split())
S = [list(input()) for _ in range(H)]

max_step = 0
for i in range(H * W):
    sy, sx = i // W, i % W
    if S[sy][sx] == "#":
        continue

    steps = [[-1] * W for _ in range(H)]
    steps[sy][sx] = 0
    queue = [(sy, sx)]
    while len(queue) >= 1:
        cy, cx = queue.pop(0)
        for dy, dx in ((0, 1), (0, -1), (1, 0), (-1, 0)):
            ny, nx = cy + dy, cx + dx
            if not (0 <= ny <= H - 1 and 0 <= nx <= W - 1):
                continue
            if S[ny][nx] == "#" or steps[ny][nx] >= 0:
                continue
            queue.append((ny, nx))
            steps[ny][nx] = steps[cy][cx] + 1

    ms = max(max(r) for r in steps)
    max_step = max(ms, max_step)

print(max_step)

Buggy python submission (verdict: Time Limit Exceeded):
from itertools import combinations, product
import copy
H, W = map(int, input().split())


def search_steps(maze, cy, cx, gy, gx, step):
    if (cy, cx) == (gy, gx):
        return step
    ss = []
    for dy, dx in ((0, 1), (0, -1), (1, 0), (-1, 0)):
        ny, nx = cy + dy, cx + dx
        new_maze = copy.deepcopy(maze)
        new_maze[cy][cx] = "#"
        if 0 <= ny <= H - 1 and 0 <= nx <= W - 1 and maze[ny][nx] == ".":
            s = search_steps(new_maze, ny, nx, gy, gx, step + 1)
            if s >= 0:
                ss.append(s)

    if len(ss) == 0:
        return -1
    else:
        mss = min(ss)
        return min(ss) if mss >= 0 else -1


S = [0] * H
for i in range(H):
    S[i] = list(input())

max_steps = 0
for s, g in combinations(range(H * W), 2):
    sy, sx = s // W, s % W
    gy, gx = g // W, g % W
    if S[sy][sx] == "#" or S[gy][gx] == "#":
        continue

    steps = search_steps(copy.deepcopy(S), sy, sx, gy, gx, 0)
    if steps > max_steps:
        max_steps = steps

print(max_steps)

Fixed submission:
H, W = map(int, input().split())
S = [list(input()) for _ in range(H)]

max_step = 0
for i in range(H * W):
    sy, sx = i // W, i % W
    if S[sy][sx] == "#":
        continue

    steps = [[-1] * W for _ in range(H)]
    steps[sy][sx] = 0
    queue = [(sy, sx)]
    while len(queue) >= 1:
        cy, cx = queue.pop(0)
        for dy, dx in ((0, 1), (0, -1), (1, 0), (-1, 0)):
            ny, nx = cy + dy, cx + dx
            if not (0 <= ny <= H - 1 and 0 <= nx <= W - 1):
                continue
            if S[ny][nx] == "#" or steps[ny][nx] >= 0:
                continue
            queue.append((ny, nx))
            steps[ny][nx] = steps[cy][cx] + 1

    ms = max(max(r) for r in steps)
    max_step = max(ms, max_step)

print(max_step)