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

h,w = [int(x) for x in input().split()]
m = []
for _ in range(h):
    m.append(list(input()))

def bfs(sy,sx,gy,gx):
    res = [[-1]*(w) for i in range(h)]
    res[sy][sx] = 0
    q = deque()
    q.append([sy,sx])
    while q:
        z = q.popleft()
        if z[0]==gy and z[1]==gx:
            break
        for dy,dx in [[1,0],[-1,0],[0,1],[0,-1]]:
            ny = z[0] + dy
            nx = z[1] + dx
            if 0<=ny<h and 0<=nx<w and m[ny][nx]=="." and res[ny][nx]==-1:
                q.append([ny,nx])
                res[ny][nx] = res[z[0]][z[1]] + 1
    return res[gy][gx]

ans = -1
for i in range(h):
    for j in range(w):
        for ii in range(h):
            for jj in range(w):
                if i==ii and j==jj:
                    continue
                if (m[i][j]=="." and m[ii][jj]=="."):
                    xxx = bfs(i,j,ii,jj)
                    if xxx != -1:
                        if xxx > ans:
                            ans = xxx
print(ans)

Fixed submission:
from collections import deque

h,w = [int(x) for x in input().split()]
m = []
for _ in range(h):
    m.append(list(input()))

def bfs(sy,sx):
    res = [[-1]*(w) for i in range(h)]
    res[sy][sx] = 0
    q = deque()
    q.append([sy,sx])
    dis = 0
    while q:
        z = q.popleft()
        for dy,dx in [[1,0],[-1,0],[0,1],[0,-1]]:
            ny = z[0] + dy
            nx = z[1] + dx
            if 0<=ny<h and 0<=nx<w and m[ny][nx]=="." and res[ny][nx]==-1:
                q.append([ny,nx])
                res[ny][nx] = res[z[0]][z[1]] + 1
                dis = res[ny][nx]
    return dis

ans = -1
for i in range(h):
    for j in range(w):
        if m[i][j] == ".":
            ret = bfs(i,j)
            if ret > ans:
                ans = ret
print(ans)

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

h,w = [int(x) for x in input().split()]
m = []
for _ in range(h):
    m.append(list(input()))

def bfs(sy,sx,gy,gx):
    res = [[-1]*(w) for i in range(h)]
    res[sy][sx] = 0
    q = deque()
    q.append([sy,sx])
    while q:
        z = q.popleft()
        if z[0]==gy and z[1]==gx:
            break
        for dy,dx in [[1,0],[-1,0],[0,1],[0,-1]]:
            ny = z[0] + dy
            nx = z[1] + dx
            if 0<=ny<h and 0<=nx<w and m[ny][nx]=="." and res[ny][nx]==-1:
                q.append([ny,nx])
                res[ny][nx] = res[z[0]][z[1]] + 1
    return res[gy][gx]

ans = -1
for i in range(h):
    for j in range(w):
        for ii in range(h):
            for jj in range(w):
                if i==ii and j==jj:
                    continue
                if (m[i][j]=="." and m[ii][jj]=="."):
                    xxx = bfs(i,j,ii,jj)
                    if xxx != -1:
                        if xxx > ans:
                            ans = xxx
print(ans)

Fixed submission:
from collections import deque

h,w = [int(x) for x in input().split()]
m = []
for _ in range(h):
    m.append(list(input()))

def bfs(sy,sx):
    res = [[-1]*(w) for i in range(h)]
    res[sy][sx] = 0
    q = deque()
    q.append([sy,sx])
    dis = 0
    while q:
        z = q.popleft()
        for dy,dx in [[1,0],[-1,0],[0,1],[0,-1]]:
            ny = z[0] + dy
            nx = z[1] + dx
            if 0<=ny<h and 0<=nx<w and m[ny][nx]=="." and res[ny][nx]==-1:
                q.append([ny,nx])
                res[ny][nx] = res[z[0]][z[1]] + 1
                dis = res[ny][nx]
    return dis

ans = -1
for i in range(h):
    for j in range(w):
        if m[i][j] == ".":
            ret = bfs(i,j)
            if ret > ans:
                ans = ret
print(ans)

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

h,w = [int(x) for x in input().split()]
m = []
for _ in range(h):
    m.append(list(input()))

def bfs(sy,sx,gy,gx):
    res = [[-1]*(w) for i in range(h)]
    res[sy][sx] = 0
    q = deque()
    q.append([sy,sx])
    while q:
        z = q.popleft()
        if z[0]==gy and z[1]==gx:
            break
        for dy,dx in [[1,0],[-1,0],[0,1],[0,-1]]:
            ny = z[0] + dy
            nx = z[1] + dx
            if 0<=ny<h and 0<=nx<w and m[ny][nx]=="." and res[ny][nx]==-1:
                q.append([ny,nx])
                res[ny][nx] = res[z[0]][z[1]] + 1
    return res[gy][gx]

ans = []
for i in range(h):
    for j in range(w):
        for ii in range(h):
            for jj in range(w):
                if i==ii and j==jj:
                    continue
                if (m[i][j]=="." and m[ii][jj]=="."):
                    xxx = bfs(i,j,ii,jj)
                    if xxx != -1:
                        ans.append(xxx)

print(max(ans))

Fixed submission:
from collections import deque

h,w = [int(x) for x in input().split()]
m = []
for _ in range(h):
    m.append(list(input()))

def bfs(sy,sx):
    res = [[-1]*(w) for i in range(h)]
    res[sy][sx] = 0
    q = deque()
    q.append([sy,sx])
    dis = 0
    while q:
        z = q.popleft()
        for dy,dx in [[1,0],[-1,0],[0,1],[0,-1]]:
            ny = z[0] + dy
            nx = z[1] + dx
            if 0<=ny<h and 0<=nx<w and m[ny][nx]=="." and res[ny][nx]==-1:
                q.append([ny,nx])
                res[ny][nx] = res[z[0]][z[1]] + 1
                dis = res[ny][nx]
    return dis

ans = -1
for i in range(h):
    for j in range(w):
        if m[i][j] == ".":
            ret = bfs(i,j)
            if ret > ans:
                ans = ret
print(ans)