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

n, m = map(int, input().split(" "))
t = [ [-1] * m for i in range(n) ]

NG='#'
OK='.'

ok_list = []
for i in range(n):
    for j,s in enumerate(input()):
        t[i][j] = s
        if s == OK:
            ok_list.append( (i, j ) )


def depth(t, src, dest):
    q = collections.deque()
    deadlist = { src: 0 }
    q.append( (src[0], src[1], 0) )

    while len(q) != 0:
        x, y, cost = q.popleft()
        for _x, _y in [(x+1, y), (x-1,y), (x,y+1),(x,y-1)]:
            if ((_x,_y) not in deadlist ) and \
                ( 0 <= _x < n) and (0 <= _y < m) and t[_x][_y] == OK:
                deadlist[(_x, _y)] = cost+1
                q.append((_x, _y, cost+1))

    return deadlist.get(dest, -1)


ans = 0
for src, dest in itertools.combinations(ok_list, 2):
    tmp = depth(t, src, dest)
    if ans < tmp:
        ans = tmp
print(ans)

Fixed submission:
import itertools
import collections

n, m = map(int, input().split(" "))
t = [ [-1] * m for i in range(n) ]

NG='#'
OK='.'

ok_list = []
for i in range(n):
    for j,s in enumerate(input()):
        t[i][j] = s
        if s == OK:
            ok_list.append( (i, j ) )


def depth(t, src):
    q = collections.deque()
    deadlist = { src: 0 }
    q.append( (src[0], src[1], 0) )

    while len(q) != 0:
        x, y, cost = q.popleft()
        for _x, _y in [(x+1, y), (x-1,y), (x,y+1),(x,y-1)]:
            if ((_x,_y) not in deadlist ) and \
                ( 0 <= _x < n) and (0 <= _y < m) and t[_x][_y] == OK:
                deadlist[(_x, _y)] = cost+1
                q.append((_x, _y, cost+1))

    return max(deadlist.values())


ans = 0
for src in ok_list:
    tmp = depth(t, src)
    if ans < tmp:
        ans = tmp
print(ans)

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

n, m = map(int, input().split(" "))
t = [ [-1] * m for i in range(n) ]

NG='#'
OK='.'

ok_list = []
for i in range(n):
    for j,s in enumerate(input()):
        t[i][j] = s
        if s == OK:
            ok_list.append( (i, j ) )


def depth(t, src, dest):
    q = collections.deque()
    deadlist = { src: 0 }
    q.append( (src[0], src[1], 0) )

    while len(q) != 0:
        x, y, cost = q.popleft()
        for _x, _y in [(x+1, y), (x-1,y), (x,y+1),(x,y-1)]:
            if ((_x,_y) not in deadlist ) and \
                ( 0 <= _x < n) and (0 <= _y < m) and t[_x][_y] == OK:
                deadlist[(_x, _y)] = cost+1
                q.append((_x, _y, cost+1))

    return deadlist.get(dest, -1)


ans = 0
for src, dest in itertools.combinations(ok_list, 2):
    tmp = depth(t, src, dest)
    if ans < tmp:
        ans = tmp
print(ans)

Fixed submission:
import itertools
import collections

n, m = map(int, input().split(" "))
t = [ [-1] * m for i in range(n) ]

NG='#'
OK='.'

ok_list = []
for i in range(n):
    for j,s in enumerate(input()):
        t[i][j] = s
        if s == OK:
            ok_list.append( (i, j ) )


def depth(t, src):
    q = collections.deque()
    deadlist = { src: 0 }
    q.append( (src[0], src[1], 0) )

    while len(q) != 0:
        x, y, cost = q.popleft()
        for _x, _y in [(x+1, y), (x-1,y), (x,y+1),(x,y-1)]:
            if ((_x,_y) not in deadlist ) and \
                ( 0 <= _x < n) and (0 <= _y < m) and t[_x][_y] == OK:
                deadlist[(_x, _y)] = cost+1
                q.append((_x, _y, cost+1))

    return max(deadlist.values())


ans = 0
for src in ok_list:
    tmp = depth(t, src)
    if ans < tmp:
        ans = tmp
print(ans)