Buggy python submission (verdict: Time Limit Exceeded):
from collections import deque

[H, W] = [int(i) for i in input().split()]
S = [0]*(H+2)
S[0] = ['#']*(W+2)
S[H+1] = ['#']*(W+2)

for i in range(1, H+1):
    row = ['#'] + list(input()) + ['#']
    S[i] = row

dy = [1, 0, -1, 0]
dx = [0, 1, 0, -1]
    
def bfs(p, q, c):
    distance = {(p, q):0}
    que = deque([(p, q)])
    reached = [[0]*(W+2) for k in range(H+2)]
    
    while len(que) > 0: 
        (y, x) = que.popleft()
        
        if c[y][x] == '#':
            continue
        
        reached[y][x] = 1
        
        for k in range(4):
            yy, xx = y + dy[k], x + dx[k]
            if reached[yy][xx] == 1 or c[yy][xx] == '#':
                continue
            else:
                distance[(yy, xx)] = distance[(y, x)] + 1
                que.append((yy, xx))
        
    distances = list(distance.values())
    return max(distances)

anss = [0]*(H*W)
times = 0

for i in range(1, H+1):
    for j in range(1, W+1):
        if S[i][j] == '.':
            anss[times] = bfs(i, j, S)
        times += 1

print(max(anss))

Fixed submission:
from collections import deque

[H, W] = [int(i) for i in input().split()]
S = [list(input()) for _ in range(H)]
dy, dx = [1, 0, -1, 0], [0, 1, 0, -1]

def bfs(sy, sx):
    if S[sy][sx] == '#':
        return 0
        
    reached = [[-1]*W for _ in range(H)]
    que = deque([[sy, sx]])
    reached[sy][sx] = 0
    
    while que:
        [y, x] = que.pop()
        for i in range(4):
            yy, xx = y + dy[i], x + dx[i]
            if yy < 0 or xx < 0 or yy > H-1 or xx > W-1 or S[yy][xx] == '#' or reached[yy][xx] != -1:
                continue
            else:
                reached[yy][xx] = reached[y][x] + 1
                que.appendleft([yy, xx])
                
    distance = 0
    for i in range(H):
        for j in range(W):
            distance = max(distance, reached[i][j])
            
    return distance

ans = 0
for a in range(H):
    for b in range(W):
        ans = max(ans, bfs(a, b))

print(ans)

Buggy python submission (verdict: Time Limit Exceeded):
from collections import deque

[H, W] = [int(i) for i in input().split()]
S = [0]*(H+2)
S[0] = ['#']*(W+2)
S[H+1] = ['#']*(W+2)

for i in range(1, H+1):
    row = ['#'] + list(input()) + ['#']
    S[i] = row
    
def bfs(p, q, c):
    distance = {(p, q):0}
    que = deque([(p, q)])
    reached = {(p, q):1}
    
    while len(que) > 0: 
        (y, x) = que.popleft()
        if c[y][x] == '#':
            continue
        
        reached[(y, x)] = 1
        try:
            if reached[(y+1, x)] == 1:
                pass
        except KeyError:
            if c[y+1][x] == '.':
                que.append((y+1, x))
                distance[(y+1, x)] = distance[(y, x)] + 1
        
        try:
            if reached[(y-1, x)] == 1:
                pass
        except KeyError:
            if c[y-1][x] == '.':
                que.append((y-1, x))
                distance[(y-1, x)] = distance[(y, x)] + 1
        
        try:
            if reached[(y, x+1)] == 1:
                pass
        except KeyError:
            if c[y][x+1] == '.':
                que.append((y, x+1))
                distance[(y, x+1)] = distance[(y, x)] + 1
        
        try:
            if reached[(y, x-1)] == 1:
                pass
        except KeyError:
            if c[y][x-1] == '.':
                que.append((y, x-1))
                distance[(y, x-1)] = distance[(y, x)] + 1
        
    distances = list(distance.values())
    return max(distances)

anss = [0]*(H*W)
times = 0

for i in range(1, H+1):
    for j in range(1, W+1):
        if S[i][j] == '.':
            anss[times] = bfs(i, j, S)
        times += 1

print(max(anss))


Fixed submission:
from collections import deque

[H, W] = [int(i) for i in input().split()]
S = [list(input()) for _ in range(H)]
dy, dx = [1, 0, -1, 0], [0, 1, 0, -1]

def bfs(sy, sx):
    if S[sy][sx] == '#':
        return 0
        
    reached = [[-1]*W for _ in range(H)]
    que = deque([[sy, sx]])
    reached[sy][sx] = 0
    
    while que:
        [y, x] = que.pop()
        for i in range(4):
            yy, xx = y + dy[i], x + dx[i]
            if yy < 0 or xx < 0 or yy > H-1 or xx > W-1 or S[yy][xx] == '#' or reached[yy][xx] != -1:
                continue
            else:
                reached[yy][xx] = reached[y][x] + 1
                que.appendleft([yy, xx])
                
    distance = 0
    for i in range(H):
        for j in range(W):
            distance = max(distance, reached[i][j])
            
    return distance

ans = 0
for a in range(H):
    for b in range(W):
        ans = max(ans, bfs(a, b))

print(ans)