Buggy python submission (verdict: Runtime Error):
import sys
from collections import deque

sr = lambda: sys.stdin.readline().rstrip()
ir = lambda: int(sr())
lr = lambda: list(map(int, sr().split()))

def bfs(sy, sx):
    dist = [[-INF] * W for _ in range(H)]
    dist[sy][sx] = 0
    que = deque([(sy, sx)])
    popleft = que.popleft; append = que.append
    while que:
        y, x = popleft()
        cur = dist[y][x]
        for dy, dx in zip([0, 1, 0, -1], [1, 0, -1, 0]):            
            ny = y + dy
            nx = x + dx
            if 0 <= ny < H and 0 <= nx < W and \
                    grid[ny][nx] != '#' and dist[ny][nx] == -INF:
                dist[ny][nx] = cur + 1
                append((ny, nx))
    ret = max(max(x for x in di)for di in dist)
    return ret

INF = 10 ** 3
H, W = lr()
grid = [sr() for _ in range(H)]
answer = max(max(bfs(i, j) for j in range(W) if grid[i][j] == '.') for i in range(H))
print(answer)
# 22

Fixed submission:
import sys
from collections import deque
#import numpy as np

stdin = sys.stdin

ri = lambda: int(rs())
rl = lambda: list(map(int, stdin.readline().split())) # applies to numbers only
rs = lambda: stdin.readline().rstrip()  # ignore trailing spaces

H, W = rl()
grid = [list(rs()) for _ in range(H)]
INF = 1000

def bfs(sx, sy):
    d = [[INF] * W for _ in range(H)]
    dx = [1, 0, -1, 0]
    dy = [0, 1, 0, -1]

    que = deque()
    que.append((sx, sy))
    d[sy][sx] = 0
    while que:
        p = que.popleft()
        for i in range(4):
            nx = p[0] + dx[i]
            ny = p[1] + dy[i]                
            if 0 <= ny and ny <= H-1 and 0 <= nx and nx <= W-1 and \
                    grid[ny][nx] != '#' and d[ny][nx] == INF:
                d[ny][nx] = d[p[1]][p[0]] + 1
                que.append((nx, ny))
    dis = [[0 if x == INF else x for x in row] for row in d]
    return max([max(y) for y in dis])

answer = 0

for s in range(H):
    for t in range(W):
        if grid[s][t] == '#':
            continue
        distance = bfs(t, s)
        if distance > answer:
            answer = distance

print(answer)

Buggy python submission (verdict: Runtime Error):
import sys
from collections import deque

sr = lambda: sys.stdin.readline().rstrip()
ir = lambda: int(sr())
lr = lambda: list(map(int, sr().split()))

H, W = lr()
grid = [sr() for _ in range(H)]

def bfs(sy, sx):
    d = [[float('inf')] * W for _ in range(H)]
    dy = [0, 1, 0, -1]
    dx = [1, 0, -1, 0]
    que = deque()
    que.append((sy, sx))
    d[sy][sx] = 0
    while que:
        p = que.popleft()
        for i in range(4):
            ny = p[0] + dy[i]
            nx = p[1] + dx[i]                
            if 0 <= ny < H and 0 <= nx < W and \
                    grid[ny][nx] != '#' and d[ny][nx] == float('inf'):
                d[ny][nx] = d[p[0]][p[1]] + 1
                que.append((ny, nx))
    max = 0
    for row in d:
        for r in row:
            if r != float('inf') and r > max:
                max = r
    return max

answer = 0    
for i in range(H):
    for j in range(W):
        if S[i][j] == '#':
            continue
        result = bfs(i, j)
        if result > answer:
            answer = result

print(answer)
# 49


Fixed submission:
import sys
from collections import deque
#import numpy as np

stdin = sys.stdin

ri = lambda: int(rs())
rl = lambda: list(map(int, stdin.readline().split())) # applies to numbers only
rs = lambda: stdin.readline().rstrip()  # ignore trailing spaces

H, W = rl()
grid = [list(rs()) for _ in range(H)]
INF = 1000

def bfs(sx, sy):
    d = [[INF] * W for _ in range(H)]
    dx = [1, 0, -1, 0]
    dy = [0, 1, 0, -1]

    que = deque()
    que.append((sx, sy))
    d[sy][sx] = 0
    while que:
        p = que.popleft()
        for i in range(4):
            nx = p[0] + dx[i]
            ny = p[1] + dy[i]                
            if 0 <= ny and ny <= H-1 and 0 <= nx and nx <= W-1 and \
                    grid[ny][nx] != '#' and d[ny][nx] == INF:
                d[ny][nx] = d[p[1]][p[0]] + 1
                que.append((nx, ny))
    dis = [[0 if x == INF else x for x in row] for row in d]
    return max([max(y) for y in dis])

answer = 0

for s in range(H):
    for t in range(W):
        if grid[s][t] == '#':
            continue
        distance = bfs(t, s)
        if distance > answer:
            answer = distance

print(answer)

Buggy python submission (verdict: Time Limit Exceeded):
import sys
from collections import deque
#import numpy as np

stdin = sys.stdin

ri = lambda: int(rs())
rl = lambda: list(map(int, stdin.readline().split())) # applies to numbers only
rs = lambda: stdin.readline().rstrip()  # ignore trailing spaces

H, W = rl()
grid = [list(rs()) for _ in range(H)]
INF = 1000

def bfs(sx, sy, gx, gy):
    d = [[INF] * W for _ in range(H)]
    dx = [1, 0, -1, 0]
    dy = [0, 1, 0, -1]

    que = deque()
    que.append((sx, sy))
    d[sy][sx] = 0
    while que:
        p = que.popleft()
        if p[0] == gx and p[1] == gy:
            break
        for i in range(4):
            nx = p[0] + dx[i]
            ny = p[1] + dy[i]                
            if 0 <= ny and ny <= H-1 and 0 <= nx and nx <= W-1 and \
                    grid[ny][nx] != '#' and d[ny][nx] == INF:
                d[ny][nx] = d[p[1]][p[0]] + 1
                que.append((nx, ny))
    return d[gy][gx]

answer = 0
for s in range(H):
    for t in range(W):
        for u in range(s, H):
            for v in range(W):
                if grid[s][t] == '#' or grid[u][v] == '#':
                    continue
                if s == u and t == v:
                    continue
                dis = bfs(t, s, v, u)
                if dis < 400 and dis > answer:
                    answer = dis

print(answer)


Fixed submission:
import sys
from collections import deque
#import numpy as np

stdin = sys.stdin

ri = lambda: int(rs())
rl = lambda: list(map(int, stdin.readline().split())) # applies to numbers only
rs = lambda: stdin.readline().rstrip()  # ignore trailing spaces

H, W = rl()
grid = [list(rs()) for _ in range(H)]
INF = 1000

def bfs(sx, sy):
    d = [[INF] * W for _ in range(H)]
    dx = [1, 0, -1, 0]
    dy = [0, 1, 0, -1]

    que = deque()
    que.append((sx, sy))
    d[sy][sx] = 0
    while que:
        p = que.popleft()
        for i in range(4):
            nx = p[0] + dx[i]
            ny = p[1] + dy[i]                
            if 0 <= ny and ny <= H-1 and 0 <= nx and nx <= W-1 and \
                    grid[ny][nx] != '#' and d[ny][nx] == INF:
                d[ny][nx] = d[p[1]][p[0]] + 1
                que.append((nx, ny))
    dis = [[0 if x == INF else x for x in row] for row in d]
    return max([max(y) for y in dis])

answer = 0

for s in range(H):
    for t in range(W):
        if grid[s][t] == '#':
            continue
        distance = bfs(t, s)
        if distance > answer:
            answer = distance

print(answer)