Buggy python submission (verdict: Time Limit Exceeded):
from queue import deque
from copy import deepcopy

    
H, W = map(int, input().split())
S = [['#'] * (W + 2)]
cnt = []
Checked = [[0 for _ in range(W+2)] for _ in range(H+2)]


for _ in range(H):
    S.append(['#'] + [i for i in input()] + ['#'])
S.append(['#']*(W + 2))

for start_h in range(1, H+1):
    for start_w in range(1, W+1):
        if S[start_h][start_w] == '#':
            continue
        for goal_h in range(1, H+1):
            for goal_w in range(1, W+1):
                if S[goal_h][goal_w] == '#':
                    continue
                if start_h == goal_h or start_w == goal_w:
                    cnt.append(abs(start_h - goal_h) + abs(start_w - goal_w))
                    continue
                    
                q = deque([[start_h, start_w]])
                checked = deepcopy(Checked)
                c = 0
                tmp = []
                while q:
                    h, w = q.popleft()
                    if checked[h][w]:
                        continue
                    checked[h][w] = 1
                    if h == goal_h and w == goal_w:
                        cnt.append(c)
                        break
                        
                    for dw, dh in ([1, 0], [-1, 0], [0, 1], [0, -1]):
                        next_h, next_w = h + dh, w + dw
                        if S[next_h][next_w] == '#':
                            continue
                        if [next_h, next_w] in tmp or checked[next_h][next_w]:
                            continue
                        tmp.append([next_h, next_w])
                    if not q:
                        q.extend(tmp)
                        tmp = []
                        c += 1

print(max(cnt))

Fixed submission:
from queue import deque
from copy import deepcopy

    
H, W = map(int, input().split())
S = [['#'] * (W + 2)]
cnt = []
Checked = [[0 for _ in range(W+2)] for _ in range(H+2)]


for _ in range(H):
    S.append(['#'] + [i for i in input()] + ['#'])
S.append(['#']*(W + 2))

for start_h in range(1, H+1):
    for start_w in range(1, W+1):
        if S[start_h][start_w] == '#':
            continue
        q = deque([[start_h, start_w]])
        checked = deepcopy(Checked)
        c = 0
        tmp = []
        while q:
            h, w = q.popleft()
            if checked[h][w]:
                 continue
            checked[h][w] = 1
                    
            for dw, dh in ([1, 0], [-1, 0], [0, 1], [0, -1]):
                next_h, next_w = h + dh, w + dw
                if S[next_h][next_w] == '#':
                    continue
                if [next_h, next_w] in tmp or checked[next_h][next_w]:
                    continue
                tmp.append([next_h, next_w])
            if not q:
                q.extend(tmp)
                tmp = []
                c += 1
        cnt.append(c-1)
            
print(max(cnt))

Buggy python submission (verdict: Time Limit Exceeded):
from queue import deque
from copy import deepcopy

    
H, W = map(int, input().split())
S = []
cnt = []
Checked = [[0 for _ in range(W)] for _ in range(H)]


for _ in range(H):
    S.append([i for i in input()])
    
for start_h in range(H):
    for start_w in range(W):
        if S[start_h][start_w] == '#':
            continue
        for goal_h in range(H):
            for goal_w in range(W):
                if S[goal_h][goal_w] == '#':
                    continue
                if start_h == goal_h or start_w == goal_w:
                    cnt.append(abs(start_h - goal_h) + abs(start_w - goal_w))
                    continue
                    
                q = deque([[start_h, start_w]])
                checked = deepcopy(Checked)
                c = 0
                tmp = []
                while q:
                    h, w = q.popleft()
                    if checked[h][w]:
                        continue
                    checked[h][w] = 1
                    if h == goal_h and w == goal_w:
                        cnt.append(c)
                        break
                        
                    for dw, dh in ([1, 0], [-1, 0], [0, 1], [0, -1]):
                        next_h, next_w = h + dh, w + dw
                        if next_h < 0 or next_h >= H or next_w < 0 or next_w >= W:
                            continue
                        if S[next_h][next_w] == '#':
                            continue
                        if [next_h, next_w] in tmp or checked[next_h][next_w]:
                            continue
                        tmp.append([next_h, next_w])
                    if not q:
                        q.extend(tmp)
                        tmp = []
                        c += 1

print(max(cnt))

Fixed submission:
from queue import deque
from copy import deepcopy

    
H, W = map(int, input().split())
S = [['#'] * (W + 2)]
cnt = []
Checked = [[0 for _ in range(W+2)] for _ in range(H+2)]


for _ in range(H):
    S.append(['#'] + [i for i in input()] + ['#'])
S.append(['#']*(W + 2))

for start_h in range(1, H+1):
    for start_w in range(1, W+1):
        if S[start_h][start_w] == '#':
            continue
        q = deque([[start_h, start_w]])
        checked = deepcopy(Checked)
        c = 0
        tmp = []
        while q:
            h, w = q.popleft()
            if checked[h][w]:
                 continue
            checked[h][w] = 1
                    
            for dw, dh in ([1, 0], [-1, 0], [0, 1], [0, -1]):
                next_h, next_w = h + dh, w + dw
                if S[next_h][next_w] == '#':
                    continue
                if [next_h, next_w] in tmp or checked[next_h][next_w]:
                    continue
                tmp.append([next_h, next_w])
            if not q:
                q.extend(tmp)
                tmp = []
                c += 1
        cnt.append(c-1)
            
print(max(cnt))

Buggy python submission (verdict: Time Limit Exceeded):
from queue import deque
from copy import deepcopy

    
H, W = map(int, input().split())
S = []
cnt = []
Checked = [[0 for _ in range(W)] for _ in range(H)]


for _ in range(H):
    S.append([i for i in input()])
    
for start_h in range(H):
    for start_w in range(W):
        if S[start_h][start_w] == '#':
            continue
        for goal_h in range(H):
            for goal_w in range(W):
                if S[goal_h][goal_w] == '#':
                    continue
                if start_h == goal_h or start_w == goal_w:
                    cnt.append(abs(start_h - goal_h) + abs(start_w - goal_w))
                    continue
                    
                q = [[start_h, start_w]]
                checked = deepcopy(Checked)
                c = 0
                tmp = []
                while q:
                    h, w = q.pop(0)
                    if checked[h][w]:
                        continue
                    checked[h][w] = 1
                    if h == goal_h and w == goal_w:
                        cnt.append(c)
                        break
                        
                    for dw, dh in ([1, 0], [-1, 0], [0, 1], [0, -1]):
                        next_h, next_w = h + dh, w + dw
                        if next_h < 0 or next_h >= H or next_w < 0 or next_w >= W:
                            continue
                        if S[next_h][next_w] == '#':
                            continue
                        if [next_h, next_w] in tmp or checked[next_h][next_w]:
                            continue
                        tmp.append([next_h, next_w])
                    if not q:
                        q.extend(tmp)
                        tmp = []
                        c += 1

print(max(cnt))

Fixed submission:
from queue import deque
from copy import deepcopy

    
H, W = map(int, input().split())
S = [['#'] * (W + 2)]
cnt = []
Checked = [[0 for _ in range(W+2)] for _ in range(H+2)]


for _ in range(H):
    S.append(['#'] + [i for i in input()] + ['#'])
S.append(['#']*(W + 2))

for start_h in range(1, H+1):
    for start_w in range(1, W+1):
        if S[start_h][start_w] == '#':
            continue
        q = deque([[start_h, start_w]])
        checked = deepcopy(Checked)
        c = 0
        tmp = []
        while q:
            h, w = q.popleft()
            if checked[h][w]:
                 continue
            checked[h][w] = 1
                    
            for dw, dh in ([1, 0], [-1, 0], [0, 1], [0, -1]):
                next_h, next_w = h + dh, w + dw
                if S[next_h][next_w] == '#':
                    continue
                if [next_h, next_w] in tmp or checked[next_h][next_w]:
                    continue
                tmp.append([next_h, next_w])
            if not q:
                q.extend(tmp)
                tmp = []
                c += 1
        cnt.append(c-1)
            
print(max(cnt))

Buggy python submission (verdict: Time Limit Exceeded):
from queue import deque

    
H, W = map(int, input().split())
S = []
cnt = []


for _ in range(H):
    S.append([i for i in input()])
    
for start_h in range(H):
    for start_w in range(W):
        if S[start_h][start_w] == '#':
            continue
        for goal_h in range(H):
            for goal_w in range(W):
                if S[goal_h][goal_w] == '#':
                    continue
                if start_h == goal_h or start_w == goal_w:
                    cnt.append(abs(start_h - goal_h) + abs(start_w - goal_w))
                    continue
                    
                q = [[start_h, start_w]]
                checked = []
                c = 0
                tmp = []
                while q:
                    h, w = q.pop(0)
                    if [h, w] in checked:
                        continue
                    checked.append([h, w])
                    if h == goal_h and w == goal_w:
                        cnt.append(c)
                        break
                        
                    for dw, dh in ([1, 0], [-1, 0], [0, 1], [0, -1]):
                        next_h, next_w = h + dh, w + dw
                        if next_h < 0 or next_h >= H or next_w < 0 or next_w >= W:
                            continue
                        if S[next_h][next_w] == '#':
                            continue
                        if [next_h, next_w] in tmp or [next_h, next_w] in checked:
                            continue
                        tmp.append([next_h, next_w])
                    if not q:
                        q.extend(tmp)
                        tmp = []
                        c += 1

print(max(cnt))

Fixed submission:
from queue import deque
from copy import deepcopy

    
H, W = map(int, input().split())
S = [['#'] * (W + 2)]
cnt = []
Checked = [[0 for _ in range(W+2)] for _ in range(H+2)]


for _ in range(H):
    S.append(['#'] + [i for i in input()] + ['#'])
S.append(['#']*(W + 2))

for start_h in range(1, H+1):
    for start_w in range(1, W+1):
        if S[start_h][start_w] == '#':
            continue
        q = deque([[start_h, start_w]])
        checked = deepcopy(Checked)
        c = 0
        tmp = []
        while q:
            h, w = q.popleft()
            if checked[h][w]:
                 continue
            checked[h][w] = 1
                    
            for dw, dh in ([1, 0], [-1, 0], [0, 1], [0, -1]):
                next_h, next_w = h + dh, w + dw
                if S[next_h][next_w] == '#':
                    continue
                if [next_h, next_w] in tmp or checked[next_h][next_w]:
                    continue
                tmp.append([next_h, next_w])
            if not q:
                q.extend(tmp)
                tmp = []
                c += 1
        cnt.append(c-1)
            
print(max(cnt))

Buggy python submission (verdict: Time Limit Exceeded):
import copy


def bfs(x, y, goal_h, goal_w, c, s):
    if x < 0 or y < 0 or x >= H or y >= W or s[x][y] == '#':
        return 10000
    if x == goal_h and y == goal_w:
        return c
    s[x][y] = '#'
    return min([bfs(next_x, next_y,goal_h, goal_w, c+1, copy.deepcopy(s))
                for next_x, next_y in ([x+1, y], [x-1, y], [x, y+1], [x, y-1])])
    
H, W = map(int, input().split())
S = []
cnt = []


for _ in range(H):
    S.append([i for i in input()])
    
for start_h in range(H):
    for start_w in range(W):
        if S[start_h][start_w] == '#':
            continue
        for goal_h in range(H):
            for goal_w in range(W):
                if S[goal_h][goal_w] == '#':
                    continue
                if start_h == goal_h or start_w == goal_w:
                    cnt.append(abs(start_h - goal_h) + abs(start_w - goal_w))
                    continue
                c = 0
                s = copy.deepcopy(S)
                s[start_h][start_w], s[goal_h][goal_w] = 'S', 'G'
                cnt.append(bfs(start_h, start_w, goal_h, goal_w, c, s))

print(max(cnt))

Fixed submission:
from queue import deque
from copy import deepcopy

    
H, W = map(int, input().split())
S = [['#'] * (W + 2)]
cnt = []
Checked = [[0 for _ in range(W+2)] for _ in range(H+2)]


for _ in range(H):
    S.append(['#'] + [i for i in input()] + ['#'])
S.append(['#']*(W + 2))

for start_h in range(1, H+1):
    for start_w in range(1, W+1):
        if S[start_h][start_w] == '#':
            continue
        q = deque([[start_h, start_w]])
        checked = deepcopy(Checked)
        c = 0
        tmp = []
        while q:
            h, w = q.popleft()
            if checked[h][w]:
                 continue
            checked[h][w] = 1
                    
            for dw, dh in ([1, 0], [-1, 0], [0, 1], [0, -1]):
                next_h, next_w = h + dh, w + dw
                if S[next_h][next_w] == '#':
                    continue
                if [next_h, next_w] in tmp or checked[next_h][next_w]:
                    continue
                tmp.append([next_h, next_w])
            if not q:
                q.extend(tmp)
                tmp = []
                c += 1
        cnt.append(c-1)
            
print(max(cnt))