Buggy python submission (verdict: Time Limit Exceeded):
from collections import deque
H,W = map(int,input().split())
S = [input().strip() for _ in range(H)]
cmax = 0
for i in range(H):
    for j in range(W):
            for k in range(H):
                for l in range(W):
                    if (i,j)!=(k,l) and S[i][j]=="." and S[k][l]==".":
                        que = deque([(i,j,0)])
                        hist = {(n,m):0 for n in range(H) for m in range(W)}
                        hist[(i,j)] = 1
                        while que:
                            x = que.popleft()
                            n,m,c = x[0],x[1],x[2]
                            if n==k and m==l:
                                cmax = max(cmax,c)
                                break
                            if m+1<W and S[n][m+1]=="." and hist[(n,m+1)]==0:
                                que.append((n,m+1,c+1))
                                hist[(n,m+1)] = 1
                            if m-1>-1 and S[n][m-1]=="." and hist[(n,m-1)]==0:
                                que.append((n,m-1,c+1))
                                hist[(n,m-1)] = 1
                            if n+1<H and S[n+1][m]=="." and hist[(n+1,m)]==0:
                                que.append((n+1,m,c+1))
                                hist[(n+1,m)] = 1
                            if n-1>-1 and S[n-1][m]=="." and hist[(n-1,m)]==0:
                                que.append((n-1,m,c+1))
                                hist[(n-1,m)] = 1
print(cmax)

Fixed submission:
import heapq
INFTY = 10**3
H,W = map(int,input().split())
S = [input().strip() for _ in range(H)]
dist = {(i,j):{(k,l):INFTY for k in range(H) for l in range(W)} for j in range(W) for i in range(H)}
for i in range(H):
    for j in range(W):
        if S[i][j]==".":
            hist = {(k,l):0 for k in range(H) for l in range(W)}
            heap = [(0,i,j)]
            dist[(i,j)][(i,j)]= 0
            hist[(i,j)] = 1
            while heap:
                d,k,l = heapq.heappop(heap)
                if d>dist[(i,j)][(k,l)]:continue
                hist[(k,l)] = 1
                if l+1<W and S[k][l+1]=="." and hist[(k,l+1)]==0:
                    if dist[(i,j)][(k,l+1)]>d+1:
                        dist[(i,j)][(k,l+1)] = d+1
                        heapq.heappush(heap,(d+1,k,l+1))
                if l-1>-1 and S[k][l-1]=="." and hist[(k,l-1)]==0:
                    if dist[(i,j)][(k,l-1)]>d+1:
                        dist[(i,j)][(k,l-1)]=d+1
                        heapq.heappush(heap,(d+1,k,l-1))
                if k+1<H and S[k+1][l]=="." and hist[(k+1,l)]==0:
                    if dist[(i,j)][(k+1,l)]>d+1:
                        dist[(i,j)][(k+1,l)]=d+1
                        heapq.heappush(heap,(d+1,k+1,l))
                if k-1>-1 and S[k-1][l]=="." and hist[(k-1,l)]==0:
                    if dist[(i,j)][(k-1,l)]>d+1:
                        dist[(i,j)][(k-1,l)]=d+1
                        heapq.heappush(heap,(d+1,k-1,l))
dmax = 0
for i in range(H):
    for j in range(W):
        for k in range(H):
            for l in range(W):
                if dist[(i,j)][(k,l)]<INFTY:
                    dmax = max(dmax,dist[(i,j)][(k,l)])
print(dmax)

Buggy python submission (verdict: Time Limit Exceeded):
from collections import deque
H,W = map(int,input().split())
S = [input().strip() for _ in range(H)]
cmax = 0
for i in range(H):
    for j in range(W):
            for k in range(H):
                for l in range(W):
                    if S[i][j]=="." and S[k][l]==".":
                        que = deque([(i,j,0)])
                        hist = {(n,m):0 for n in range(H) for m in range(W)}
                        hist[(i,j)] = 1
                        while que:
                            x = que.popleft()
                            n,m,c = x[0],x[1],x[2]
                            if n==k and m==l:
                                cmax = max(cmax,c)
                                break
                            if m+1<W and S[n][m+1]=="." and hist[(n,m+1)]==0:
                                que.append((n,m+1,c+1))
                                hist[(n,m+1)] = 1
                            if m-1>-1 and S[n][m-1]=="." and hist[(n,m-1)]==0:
                                que.append((n,m-1,c+1))
                                hist[(n,m-1)] = 1
                            if n+1<H and S[n+1][m]=="." and hist[(n+1,m)]==0:
                                que.append((n+1,m,c+1))
                                hist[(n+1,m)] = 1
                            if n-1>-1 and S[n-1][m]=="." and hist[(n-1,m)]==0:
                                que.append((n-1,m,c+1))
                                hist[(n-1,m)] = 1
print(cmax)

Fixed submission:
import heapq
INFTY = 10**3
H,W = map(int,input().split())
S = [input().strip() for _ in range(H)]
dist = {(i,j):{(k,l):INFTY for k in range(H) for l in range(W)} for j in range(W) for i in range(H)}
for i in range(H):
    for j in range(W):
        if S[i][j]==".":
            hist = {(k,l):0 for k in range(H) for l in range(W)}
            heap = [(0,i,j)]
            dist[(i,j)][(i,j)]= 0
            hist[(i,j)] = 1
            while heap:
                d,k,l = heapq.heappop(heap)
                if d>dist[(i,j)][(k,l)]:continue
                hist[(k,l)] = 1
                if l+1<W and S[k][l+1]=="." and hist[(k,l+1)]==0:
                    if dist[(i,j)][(k,l+1)]>d+1:
                        dist[(i,j)][(k,l+1)] = d+1
                        heapq.heappush(heap,(d+1,k,l+1))
                if l-1>-1 and S[k][l-1]=="." and hist[(k,l-1)]==0:
                    if dist[(i,j)][(k,l-1)]>d+1:
                        dist[(i,j)][(k,l-1)]=d+1
                        heapq.heappush(heap,(d+1,k,l-1))
                if k+1<H and S[k+1][l]=="." and hist[(k+1,l)]==0:
                    if dist[(i,j)][(k+1,l)]>d+1:
                        dist[(i,j)][(k+1,l)]=d+1
                        heapq.heappush(heap,(d+1,k+1,l))
                if k-1>-1 and S[k-1][l]=="." and hist[(k-1,l)]==0:
                    if dist[(i,j)][(k-1,l)]>d+1:
                        dist[(i,j)][(k-1,l)]=d+1
                        heapq.heappush(heap,(d+1,k-1,l))
dmax = 0
for i in range(H):
    for j in range(W):
        for k in range(H):
            for l in range(W):
                if dist[(i,j)][(k,l)]<INFTY:
                    dmax = max(dmax,dist[(i,j)][(k,l)])
print(dmax)

Buggy python submission (verdict: Time Limit Exceeded):
from collections import deque
H,W = map(int,input().split())
S = [input().strip() for _ in range(H)]
A = [[0 for _ in range(W)] for _ in range(H)]
for i in range(H):
    for j in range(W):
        if S[i][j]==".":
            A[i][j] = 0
        else:
            A[i][j] = 1
cmax = 0
for i in range(H):
    for j in range(W):
            for k in range(H):
                for l in range(W):
                    if A[i][j]==0 and A[k][l]==0:
                        que = deque([(i,j,0)])
                        hist = {(n,m):0 for n in range(H) for m in range(W)}
                        hist[(i,j)] = 1
                        while que:
                            x = que.popleft()
                            n,m,c = x[0],x[1],x[2]
                            if n==k and m==l:
                                cmax = max(cmax,c)
                                break
                            if m+1<W and A[n][m+1]==0 and hist[(n,m+1)]==0:
                                que.append((n,m+1,c+1))
                                hist[(n,m+1)] = 1
                            if m-1>-1 and A[n][m-1]==0 and hist[(n,m-1)]==0:
                                que.append((n,m-1,c+1))
                                hist[(n,m-1)] = 1
                            if n+1<H and A[n+1][m]==0 and hist[(n+1,m)]==0:
                                que.append((n+1,m,c+1))
                                hist[(n+1,m)] = 1
                            if n-1>-1 and A[n-1][m]==0 and hist[(n-1,m)]==0:
                                que.append((n-1,m,c+1))
                                hist[(n-1,m)] = 1
print(cmax)

Fixed submission:
import heapq
INFTY = 10**3
H,W = map(int,input().split())
S = [input().strip() for _ in range(H)]
dist = {(i,j):{(k,l):INFTY for k in range(H) for l in range(W)} for j in range(W) for i in range(H)}
for i in range(H):
    for j in range(W):
        if S[i][j]==".":
            hist = {(k,l):0 for k in range(H) for l in range(W)}
            heap = [(0,i,j)]
            dist[(i,j)][(i,j)]= 0
            hist[(i,j)] = 1
            while heap:
                d,k,l = heapq.heappop(heap)
                if d>dist[(i,j)][(k,l)]:continue
                hist[(k,l)] = 1
                if l+1<W and S[k][l+1]=="." and hist[(k,l+1)]==0:
                    if dist[(i,j)][(k,l+1)]>d+1:
                        dist[(i,j)][(k,l+1)] = d+1
                        heapq.heappush(heap,(d+1,k,l+1))
                if l-1>-1 and S[k][l-1]=="." and hist[(k,l-1)]==0:
                    if dist[(i,j)][(k,l-1)]>d+1:
                        dist[(i,j)][(k,l-1)]=d+1
                        heapq.heappush(heap,(d+1,k,l-1))
                if k+1<H and S[k+1][l]=="." and hist[(k+1,l)]==0:
                    if dist[(i,j)][(k+1,l)]>d+1:
                        dist[(i,j)][(k+1,l)]=d+1
                        heapq.heappush(heap,(d+1,k+1,l))
                if k-1>-1 and S[k-1][l]=="." and hist[(k-1,l)]==0:
                    if dist[(i,j)][(k-1,l)]>d+1:
                        dist[(i,j)][(k-1,l)]=d+1
                        heapq.heappush(heap,(d+1,k-1,l))
dmax = 0
for i in range(H):
    for j in range(W):
        for k in range(H):
            for l in range(W):
                if dist[(i,j)][(k,l)]<INFTY:
                    dmax = max(dmax,dist[(i,j)][(k,l)])
print(dmax)

Buggy python submission (verdict: Time Limit Exceeded):
from collections import deque
H,W = map(int,input().split())
S = [input().strip() for _ in range(H)]
cmax = 0
for i in range(H):
    for j in range(W):
            for k in range(H):
                for l in range(W):
                    if S[i][j]=="." and S[k][l]==".":
                        que = deque([(i,j,0)])
                        hist = {(n,m):0 for n in range(H) for m in range(W)}
                        hist[(i,j)] = 1
                        while que:
                            x = que.popleft()
                            n,m,c = x[0],x[1],x[2]
                            if n==k and m==l:
                                cmax = max(cmax,c)
                                break
                            if m+1<W and S[n][m+1]=="." and hist[(n,m+1)]==0:
                                que.append((n,m+1,c+1))
                                hist[(n,m+1)] = 1
                            if m-1>-1 and S[n][m-1]=="." and hist[(n,m-1)]==0:
                                que.append((n,m-1,c+1))
                                hist[(n,m-1)] = 1
                            if n+1<H and S[n+1][m]=="." and hist[(n+1,m)]==0:
                                que.append((n+1,m,c+1))
                                hist[(n+1,m)] = 1
                            if n-1>-1 and S[n-1][m]=="." and hist[(n-1,m)]==0:
                                que.append((n-1,m,c+1))
                                hist[(n-1,m)] = 1
print(cmax)

Fixed submission:
import heapq
INFTY = 10**3
H,W = map(int,input().split())
S = [input().strip() for _ in range(H)]
dist = {(i,j):{(k,l):INFTY for k in range(H) for l in range(W)} for j in range(W) for i in range(H)}
for i in range(H):
    for j in range(W):
        if S[i][j]==".":
            hist = {(k,l):0 for k in range(H) for l in range(W)}
            heap = [(0,i,j)]
            dist[(i,j)][(i,j)]= 0
            hist[(i,j)] = 1
            while heap:
                d,k,l = heapq.heappop(heap)
                if d>dist[(i,j)][(k,l)]:continue
                hist[(k,l)] = 1
                if l+1<W and S[k][l+1]=="." and hist[(k,l+1)]==0:
                    if dist[(i,j)][(k,l+1)]>d+1:
                        dist[(i,j)][(k,l+1)] = d+1
                        heapq.heappush(heap,(d+1,k,l+1))
                if l-1>-1 and S[k][l-1]=="." and hist[(k,l-1)]==0:
                    if dist[(i,j)][(k,l-1)]>d+1:
                        dist[(i,j)][(k,l-1)]=d+1
                        heapq.heappush(heap,(d+1,k,l-1))
                if k+1<H and S[k+1][l]=="." and hist[(k+1,l)]==0:
                    if dist[(i,j)][(k+1,l)]>d+1:
                        dist[(i,j)][(k+1,l)]=d+1
                        heapq.heappush(heap,(d+1,k+1,l))
                if k-1>-1 and S[k-1][l]=="." and hist[(k-1,l)]==0:
                    if dist[(i,j)][(k-1,l)]>d+1:
                        dist[(i,j)][(k-1,l)]=d+1
                        heapq.heappush(heap,(d+1,k-1,l))
dmax = 0
for i in range(H):
    for j in range(W):
        for k in range(H):
            for l in range(W):
                if dist[(i,j)][(k,l)]<INFTY:
                    dmax = max(dmax,dist[(i,j)][(k,l)])
print(dmax)