Buggy python submission (verdict: Time Limit Exceeded):
H, W = map(int, input().split())
S = []
for i in range(H):
    r = input()
    S.append(list(r))


def f(v1, v2):
    m = []
    for i in range(H):
        m.append([None]*W)
    m[v1][v2] = 0

    flag = True
    while flag:
        flag = False
        for i in range(H):
            for j in range(W):
                if S[i][j] == '#':
                    continue
                elif m[i][j] == None:
                    flag = True
                    continue
                else:
                    cur = m[i][j]
                    u = i-1
                    if 0 <= u and S[u][j] == '.':
                        if m[u][j] is None:
                            m[u][j] = cur + 1
                        m[u][j] = min(m[u][j], cur+1)
                    d = i+1
                    if d <= H-1 and S[d][j] == '.':
                        if m[d][j] is None:
                            m[d][j] = cur + 1
                        m[d][j] = min(m[d][j], cur+1)
                    l = j-1
                    if 0 <= l and S[i][l] == '.':
                        if m[i][l] is None:
                            m[i][l] = cur + 1
                        m[i][l] = min(m[i][l], cur+1)
                    r = j+1
                    if r <= W-1 and S[i][r] == '.':
                        if m[i][r] is None:
                            m[i][r] = cur + 1
                        m[i][r] = min(m[i][r], cur+1)

    ans = 0
    for i in range(H):
        for j in range(W):
            if m[i][j] == None:
                continue
            else:
                ans = max(ans, m[i][j])
    return ans

r = -1
#　スタート位置
for i in range(H):
    for j in range(W):
        if S[i][j] == '#':
            continue
        else:
            x = f(i, j)
            r = max(r, x)
            # print(x)
print(r)

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

def is_kabe(v1, v2):
    if S[v1][v2] == '#':
        return True
    else:
        return False

def bfs(v1, v2):
    m = []
    for i in range(H):
        m.append([None]*W)

    m[v1][v2] = 0
    q = [[v1, v2]]
    while q:
        mass = q.pop(0)
        # print('mass', mass)
        # print('--', m)
        a = mass[0]
        b = mass[1]
        next_cost = m[a][b] + 1
        if a - 1 >= 0:
            up = m[a-1][b]
            if not is_kabe(a-1, b) and up is None:
                m[a-1][b] = next_cost
                q.append([a-1, b])
        if a + 1 < H:
            down = m[a+1][b]
            if not is_kabe(a+1, b) and down is None:
                m[a+1][b] = next_cost
                q.append([a+1, b])
        if b - 1 >= 0:
            left = m[a][b-1]
            if not is_kabe(a, b-1) and left is None:
                m[a][b-1] = next_cost
                q.append([a, b-1])
        if b + 1 < W:
            right = m[a][b+1]
            if not is_kabe(a, b+1) and right is None:
                m[a][b+1] = next_cost
                q.append([a, b+1])

    # print('---')
    # for i in range(H):
    #     for j in range(W):
    #         if m[i][j] is None:
    #             print('-', end=' ')
    #         else:
    #             print(m[i][j], end=' ')
    #     print()

    mx = 0
    for i in range(H):
        for j in range(W):
            cost = m[i][j]
            if cost is not None:
                mx = max(mx, cost)
    return mx


r = 0
for i in range(H):
    for j in range(W):
        if S[i][j] == '#':
            continue
        x = bfs(i, j)
        r = max(r, x)
print(r)