Buggy python submission (verdict: Time Limit Exceeded):
import collections
h,w = map(int, input().split())
s=[input()for _ in range(h)]
r=[[1,0],[-1,0],[0,1],[0,-1]]
def bfs(i,j):
    global h,w,r
    f=[-1 for _ in range(h*w)]
    que = [[] for _ in range(h*w)]
    que[0].append([i,j])
    for k in range(h*w):
        if que[k]:
            res = k
            for i,j in que[k]:
                f[i*w+j] =k
                for sr in r:
                    ti = i + sr[0]
                    tj = j + sr[1]
                    if ti>=0 and tj>=0 and ti<h and tj<w:
                        if s[ti][tj] == ".":
                            if f[ti*w+tj] == -1:
                                que[k+1].append([ti,tj])
    return res

res = 0
for i in range(h):
    for j in range(w):
        if s[i][j] ==".":
            res = max(res,bfs(i,j))
print(res)

Fixed submission:
import collections

h, w = map(int, input().split())
s = [input() for _ in range(h)]
r = [[1, 0], [-1, 0], [0, 1], [0, -1]]


def bfs(i, j):
    global h, w, r
    f = [-1 for _ in range(h * w)]
    que = [[] for _ in range(h * w)]
    que[0].append([i, j])
    for k in range(h * w):
        if que[k]:
            res = k
            for i, j in que[k]:
                f[i * w + j] = k
                for sr in r:
                    ti = i + sr[0]
                    tj = j + sr[1]
                    if ti >= 0 and tj >= 0 and ti < h and tj < w:
                        if s[ti][tj] == ".":
                            if f[ti * w + tj] == -1:
                                que[k + 1].append([ti, tj])
    return res


fl = 0
for i in range(h):
    if "#" in s[i]:
        fl = 1
        break
if fl == 0:
    print(h + w - 2)
else:
    res = 0
    for i in range(h):
        for j in range(w):
            if s[i][j] == ".":
                res = max(res, bfs(i, j))
    print(res)

Buggy python submission (verdict: Time Limit Exceeded):
import collections
h,w = map(int, input().split())
s=[input()for _ in range(h)]
r=[[1,0],[-1,0],[0,1],[0,-1]]
def bfs(i,j):
    global h,w,r
    f=[-1 for _ in range(h*w)]
    que = collections.deque([[i*w+j,0]])
    while que:
        ij,k = que.popleft()
        i = ij//w
        j=ij%w
        f[ij] =k
        for sr in r:
            ti = i + sr[0]
            tj = j + sr[1]
            if ti>=0 and tj>=0 and ti<h and tj<w:
                if s[ti][tj] == ".":
                    if f[ti*w+tj] == -1:
                        que.append([ti*w+tj,k+1])
    return k

res = 0
for i in range(h):
    for j in range(w):
        if s[i][j] ==".":
            tres = bfs(i,j)
            res = max(res,tres)
print(res)

Fixed submission:
import collections

h, w = map(int, input().split())
s = [input() for _ in range(h)]
r = [[1, 0], [-1, 0], [0, 1], [0, -1]]


def bfs(i, j):
    global h, w, r
    f = [-1 for _ in range(h * w)]
    que = [[] for _ in range(h * w)]
    que[0].append([i, j])
    for k in range(h * w):
        if que[k]:
            res = k
            for i, j in que[k]:
                f[i * w + j] = k
                for sr in r:
                    ti = i + sr[0]
                    tj = j + sr[1]
                    if ti >= 0 and tj >= 0 and ti < h and tj < w:
                        if s[ti][tj] == ".":
                            if f[ti * w + tj] == -1:
                                que[k + 1].append([ti, tj])
    return res


fl = 0
for i in range(h):
    if "#" in s[i]:
        fl = 1
        break
if fl == 0:
    print(h + w - 2)
else:
    res = 0
    for i in range(h):
        for j in range(w):
            if s[i][j] == ".":
                res = max(res, bfs(i, j))
    print(res)

Buggy python submission (verdict: Time Limit Exceeded):
import collections
h,w = map(int, input().split())
s=[input()for _ in range(h)]
r=[[1,0],[-1,0],[0,1],[0,-1]]
def bfs(i,j):
    global h,w,r
    f=[-1 for _ in range(h*w)]
    que = collections.deque([[i,j,0]])
    while que:
        i,j,k = que.popleft()
        f[i*w+j] =k
        for sr in r:
            ti = i + sr[0]
            tj = j + sr[1]
            if ti>=0 and tj>=0 and ti<h and tj<w:
                if s[ti][tj] == ".":
                    if f[ti*w+tj] == -1:
                        que.append([ti,tj,k+1])
    return k

res = 0
for i in range(h):
    for j in range(w):
        if s[i][j] ==".":
            tres = bfs(i,j)
            res = max(res,tres)
print(res)

Fixed submission:
import collections

h, w = map(int, input().split())
s = [input() for _ in range(h)]
r = [[1, 0], [-1, 0], [0, 1], [0, -1]]


def bfs(i, j):
    global h, w, r
    f = [-1 for _ in range(h * w)]
    que = [[] for _ in range(h * w)]
    que[0].append([i, j])
    for k in range(h * w):
        if que[k]:
            res = k
            for i, j in que[k]:
                f[i * w + j] = k
                for sr in r:
                    ti = i + sr[0]
                    tj = j + sr[1]
                    if ti >= 0 and tj >= 0 and ti < h and tj < w:
                        if s[ti][tj] == ".":
                            if f[ti * w + tj] == -1:
                                que[k + 1].append([ti, tj])
    return res


fl = 0
for i in range(h):
    if "#" in s[i]:
        fl = 1
        break
if fl == 0:
    print(h + w - 2)
else:
    res = 0
    for i in range(h):
        for j in range(w):
            if s[i][j] == ".":
                res = max(res, bfs(i, j))
    print(res)

Buggy python submission (verdict: Time Limit Exceeded):
import collections
h,w = map(int, input().split())
s=[input()for _ in range(h)]
r=[[1,0],[-1,0],[0,1],[0,-1]]
def bfs(i,j):
    global h,w,r
    f=[-1 for _ in range(h*w)]
    que = collections.deque([[i,j,0]])
    while que:
        i,j,k = que.popleft()
        f[i*w+j] =k
        for sr in r:
            ti = i + sr[0]
            tj = j + sr[1]
            if ti>=0 and tj>=0 and ti<h and tj<w:
                if s[ti][tj] == ".":
                    if f[ti*w+tj] == -1:
                        que.append([ti,tj,k+1])
    return max(f)

def main():
    res = 0
    for i in range(h):
        for j in range(w):
            if s[i][j] ==".":
                tres = bfs(i,j)
                res = max(res,tres)
    print(res)

main()

Fixed submission:
import collections

h, w = map(int, input().split())
s = [input() for _ in range(h)]
r = [[1, 0], [-1, 0], [0, 1], [0, -1]]


def bfs(i, j):
    global h, w, r
    f = [-1 for _ in range(h * w)]
    que = [[] for _ in range(h * w)]
    que[0].append([i, j])
    for k in range(h * w):
        if que[k]:
            res = k
            for i, j in que[k]:
                f[i * w + j] = k
                for sr in r:
                    ti = i + sr[0]
                    tj = j + sr[1]
                    if ti >= 0 and tj >= 0 and ti < h and tj < w:
                        if s[ti][tj] == ".":
                            if f[ti * w + tj] == -1:
                                que[k + 1].append([ti, tj])
    return res


fl = 0
for i in range(h):
    if "#" in s[i]:
        fl = 1
        break
if fl == 0:
    print(h + w - 2)
else:
    res = 0
    for i in range(h):
        for j in range(w):
            if s[i][j] == ".":
                res = max(res, bfs(i, j))
    print(res)

Buggy python submission (verdict: Time Limit Exceeded):
import collections
h,w = map(int, input().split())
s=[input()for _ in range(h)]

r=[[1,0],[-1,0],[0,1],[0,-1]]
def bfs(i,j):
    global h,w,r
    f=[-1 for _ in range(h*w)]
    que = collections.deque([[i,j,0]])
    while que:
        i,j,k = que.popleft()
        f[i*w+j] =k
        for sr in r:
            ti = i + sr[0]
            tj = j + sr[1]
            if ti>=0 and tj>=0 and ti<h and tj<w:
                if s[ti][tj] == ".":
                    if f[ti*w+tj] == -1:
                        que.append([ti,tj,k+1])
    return max(f)


res = 0
for i in range(h):
    for j in range(w):
        if s[i][j] ==".":
            tres = bfs(i,j)
            res = max(res,tres)
print(res)

Fixed submission:
import collections

h, w = map(int, input().split())
s = [input() for _ in range(h)]
r = [[1, 0], [-1, 0], [0, 1], [0, -1]]


def bfs(i, j):
    global h, w, r
    f = [-1 for _ in range(h * w)]
    que = [[] for _ in range(h * w)]
    que[0].append([i, j])
    for k in range(h * w):
        if que[k]:
            res = k
            for i, j in que[k]:
                f[i * w + j] = k
                for sr in r:
                    ti = i + sr[0]
                    tj = j + sr[1]
                    if ti >= 0 and tj >= 0 and ti < h and tj < w:
                        if s[ti][tj] == ".":
                            if f[ti * w + tj] == -1:
                                que[k + 1].append([ti, tj])
    return res


fl = 0
for i in range(h):
    if "#" in s[i]:
        fl = 1
        break
if fl == 0:
    print(h + w - 2)
else:
    res = 0
    for i in range(h):
        for j in range(w):
            if s[i][j] == ".":
                res = max(res, bfs(i, j))
    print(res)

Buggy python submission (verdict: Time Limit Exceeded):
import collections
h,w = map(int, input().split())
s=[input()for _ in range(h)]

r=[[1,0],[-1,0],[0,1],[0,-1]]
def bfs(i,j):
    global h,w,r
    f=[-1 for _ in range(h*w)]
    que = collections.deque([[i,j,0]])
    while que:
        i,j,k = que.popleft()
        f[i*w+j] =k
        for sr in r:
            ti = i + sr[0]
            tj = j + sr[1]
            if ti>=0 and tj>=0 and ti<h and tj<w:
                if s[ti][tj] == ".":
                    if f[ti*w+tj] == -1:
                        que.append([ti,tj,k+1])
    return max(f)


res = 0
for i in range(h):
    for j in range(w):
        if s[i][j] ==".":
            tres = bfs(i,j)
            res = max(res,tres)
print(res)

Fixed submission:
import collections

h, w = map(int, input().split())
s = [input() for _ in range(h)]
r = [[1, 0], [-1, 0], [0, 1], [0, -1]]


def bfs(i, j):
    global h, w, r
    f = [-1 for _ in range(h * w)]
    que = [[] for _ in range(h * w)]
    que[0].append([i, j])
    for k in range(h * w):
        if que[k]:
            res = k
            for i, j in que[k]:
                f[i * w + j] = k
                for sr in r:
                    ti = i + sr[0]
                    tj = j + sr[1]
                    if ti >= 0 and tj >= 0 and ti < h and tj < w:
                        if s[ti][tj] == ".":
                            if f[ti * w + tj] == -1:
                                que[k + 1].append([ti, tj])
    return res


fl = 0
for i in range(h):
    if "#" in s[i]:
        fl = 1
        break
if fl == 0:
    print(h + w - 2)
else:
    res = 0
    for i in range(h):
        for j in range(w):
            if s[i][j] == ".":
                res = max(res, bfs(i, j))
    print(res)