Buggy python submission (verdict: Time Limit Exceeded):
#D幅優先探索
from collections import deque
h, w = list(map(int,input().split()))

def bfs(que,c):
    while que:
        c += 1
        qq = que
        que = deque([])
        while qq:
            x, y = qq.popleft()
            field[x][y] = "#"
            for i in range(4):
                nx = x + dx[i]
                ny = y + dy[i]
        
                if 0 <= nx <= h-1 and 0 <= ny <= w-1 and field[nx][ny] == ".":
                    que.append((nx,ny))
            
    return c-1
            
A = [list(input()) for _ in range(h)]



dx = [1,0,-1,0]
dy = [0,1,0,-1]
que = deque([]) 
ans = []

for p in range(h):
    for q in range(w):
        if A[p][q] == ".":
            field = [k[:] for k in A]
            
            que.append((p,q))
            ans.append(bfs(que,0))
print(max(ans))

Fixed submission:
#D幅優先探索
from collections import deque
h, w = list(map(int,input().split()))

def bfs(que,c):
    while que:
        c += 1
        qq = que
        que = deque([])
        while qq:
            x, y = qq.popleft()
            field[x][y] = "#"
            for i in range(4):
                nx = x + dx[i]
                ny = y + dy[i]
        
                if 0 <= nx <= h-1 and 0 <= ny <= w-1 and field[nx][ny] == ".":
                    if (nx,ny) not in que:
                        que.append((nx,ny))
            
    return c-1
            
A = [list(input()) for _ in range(h)]



dx = [1,0,-1,0]
dy = [0,1,0,-1]
que = deque([]) 
ans = []

for p in range(h):
    for q in range(w):
        if A[p][q] == ".":
            field = [k[:] for k in A]
            
            que.append((p,q))
            ans.append(bfs(que,0))
print(max(ans))

Buggy python submission (verdict: Time Limit Exceeded):
#D幅優先探索
from collections import deque
h, w = list(map(int,input().split()))

def bfs(que,c):
    while que:
        c += 1
        qq = que
        que = deque([])
        while qq:
            x, y = qq.popleft()
            field[x][y] = "#"
            for i in range(4):
                nx = x + dx[i]
                ny = y + dy[i]
        
                if 0 <= nx <= h-1 and 0 <= ny <= w-1 and field[nx][ny] == ".":
                    que.append((nx,ny))
            
    return c-1
            
A = [list(input()) for _ in range(h)]



dx = [1,0,-1,0]
dy = [0,1,0,-1]
que = deque([]) 
ans = []

for p in range(h):
    for q in range(w):
        if A[p][q] == ".":
            field = [k[:] for k in A]
            
            que.append((p,q))
            ans.append(bfs(que,0))
print(max(ans))

Fixed submission:
#D幅優先探索
from collections import deque
h, w = list(map(int,input().split()))

def bfs(que,c):
    while que:
        c += 1
        qq = que
        que = deque([])
        while qq:
            x, y = qq.popleft()
            field[x][y] = "#"
            for i in range(4):
                nx = x + dx[i]
                ny = y + dy[i]
        
                if 0 <= nx <= h-1 and 0 <= ny <= w-1 and field[nx][ny] == ".":
                    if (nx,ny) not in que:
                        que.append((nx,ny))
            
    return c-1
            
A = [list(input()) for _ in range(h)]



dx = [1,0,-1,0]
dy = [0,1,0,-1]
que = deque([]) 
ans = []

for p in range(h):
    for q in range(w):
        if A[p][q] == ".":
            field = [k[:] for k in A]
            
            que.append((p,q))
            ans.append(bfs(que,0))
print(max(ans))