Buggy python submission (verdict: Time Limit Exceeded):
import sys
input = sys.stdin.readline
h, w = [int(x) for x in input().strip().split()]
m = [[0 for _ in range(w)] for _ in range(h)]
for hi in range(h):
    for wi, s in enumerate(input().strip()):
        m[hi][wi] = s

c = [[float('inf')] * (h*w) for _ in range(h*w)]
for i in range(h*w):
    c[i][i] = 0

for hi in range(h):
    for wi in range(w):
        #print(hi, wi)
        if m[hi][wi] == '#':
            continue
        for offset in [(0, 1), (-1, 0), (0, -1), (1, 0)]:
            if hi+offset[0] < 0 or hi+offset[0] == h:
                continue
            if wi+offset[1] < 0 or wi+offset[1] == w:
                continue
            if m[hi+offset[0]][wi+offset[1]] == '#':
                continue
            #print('offset = {}'.format(offset))
            #print('hi*w+wi = {}'.format(hi*w+wi), '(hi+offset[0])*w+(wi+offset[1]) = {}'.format((hi+offset[0])*w+(wi+offset[1])))
            c[hi*w+wi][(hi+offset[0])*w+(wi+offset[1])] = 1

#print('-- cost --')
#for ci in c:
#    print(ci)

for k in range(h*w):
    for i in range(h*w):
        for j in range(h*w):
            c[i][j] = min(c[i][j], c[i][k]+c[k][j])

ans = 0
#print('-- total cost --')
#for ci in c:
#    print(ci)

for ci in c:
    for cii in ci:
        if cii != 0 and cii != float('inf'):
            ans = max(ans, cii)

print(ans)

Fixed submission:
import sys
from collections import deque
from copy import deepcopy

input = sys.stdin.readline
h, w = [int(x) for x in input().strip().split()]
m = [['#' for _ in range(w+2)] for _ in range(h+2)]
hs = []
ws = []
for hi in range(1, h+1):
    for wi, s in enumerate(input().strip(), start=1):
        m[hi][wi] = s
        if s == '.':
            hs.append(hi)
            ws.append(wi)

def bfs(hs, ws, m):
    cost = 0
    q = deque([])
    m[hs][ws] = '#'
    for offset in [(0, 1), (-1, 0), (0, -1), (1, 0)]:
        if m[hs+offset[0]][ws+offset[1]] == '.':
            q.append((hs+offset[0], ws+offset[1]))
    while q:
        cost += 1
        q_ = deque(q)
        q = deque([])
        for h, w in q_:
            m[h][w] = '#'
            for offset in [(0, 1), (-1, 0), (0, -1), (1, 0)]:
                if m[h+offset[0]][w+offset[1]] == '.':
                    m[h+offset[0]][w+offset[1]] = '#'
                    q.append((h+offset[0], w+offset[1]))

    return cost, h, w

maxi = -1
for hss, wss in zip(hs, ws):
    maxi_, hh, ww = bfs(hss, wss, deepcopy(m[:]))
    maxi = max(maxi, maxi_)

print(maxi)

Buggy python submission (verdict: Time Limit Exceeded):
import sys
input = sys.stdin.readline
h, w = [int(x) for x in input().strip().split()]
m = [[0 for _ in range(w)] for _ in range(h)]
for hi in range(h):
    for wi, s in enumerate(input().strip()):
        m[hi][wi] = s

c = [[float('inf')] * (h*w) for _ in range(h*w)]
for i in range(h*w):
    c[i][i] = 0

for hi in range(h):
    for wi in range(w):
        #print(hi, wi)
        if m[hi][wi] == '#':
            continue
        for offset in [(0, 1), (-1, 0), (0, -1), (1, 0)]:
            if hi+offset[0] < 0 or hi+offset[0] == h:
                continue
            if wi+offset[1] < 0 or wi+offset[1] == w:
                continue
            if m[hi+offset[0]][wi+offset[1]] == '#':
                continue
            #print('offset = {}'.format(offset))
            #print('hi*w+wi = {}'.format(hi*w+wi), '(hi+offset[0])*w+(wi+offset[1]) = {}'.format((hi+offset[0])*w+(wi+offset[1])))
            c[hi*w+wi][(hi+offset[0])*w+(wi+offset[1])] = 1

#print('-- cost --')
#for ci in c:
#    print(ci)

for k in range(h*w):
    for i in range(h*w):
        for j in range(h*w):
            c[i][j] = min(c[i][j], c[i][k]+c[k][j])

ans = 0
#print('-- total cost --')
#for ci in c:
#    print(ci)

for ci in c:
    for cii in ci:
        if cii != 0 and cii != float('inf'):
            ans = max(ans, cii)

print(ans)

Fixed submission:
import sys
from collections import deque
from copy import deepcopy

input = sys.stdin.readline
h, w = [int(x) for x in input().strip().split()]
m = [['#' for _ in range(w+2)] for _ in range(h+2)]
hs = []
ws = []
for hi in range(1, h+1):
    for wi, s in enumerate(input().strip(), start=1):
        m[hi][wi] = s
        if s == '.':
            hs.append(hi)
            ws.append(wi)

def bfs(hs, ws, m):
    cost = 0
    q = deque([])
    m[hs][ws] = '#'
    for offset in [(0, 1), (-1, 0), (0, -1), (1, 0)]:
        if m[hs+offset[0]][ws+offset[1]] == '.':
            q.append((hs+offset[0], ws+offset[1]))
    while q:
        cost += 1
        q_ = deque(q)
        q = deque([])
        for h, w in q_:
            m[h][w] = '#'
            for offset in [(0, 1), (-1, 0), (0, -1), (1, 0)]:
                if m[h+offset[0]][w+offset[1]] == '.':
                    m[h+offset[0]][w+offset[1]] = '#'
                    q.append((h+offset[0], w+offset[1]))

    return cost, h, w

maxi = -1
for hss, wss in zip(hs, ws):
    maxi_, hh, ww = bfs(hss, wss, deepcopy(m[:]))
    maxi = max(maxi, maxi_)

print(maxi)