Buggy python submission (verdict: Runtime Error):
from collections import deque
import numpy as np

H,W = map(int,input().split())
maze = [input() for _ in range(H)]

ans = 0
for x in range(H):
    for y in range(W):
        #壁からはスタートできない
        if maze[x][y] == "#":
            continue

        #初期化
        distance = [[0]*W for _ in range(H)]
        #start位置
        stack = deque([[x,y]])

        while stack:
            h,w = stack.popleft()
            for i,j in [[1,0],[-1,0],[0,1],[0,-1]]:
                new_h, new_w = h+i, w+j
                if new_h < 0 or new_w < 0 or new_h >= H or new_w >= W:
                    continue
                elif maze[new_h][new_w] != "#" and distance[new_h][new_w] == 0:
                    distance[new_h][new_w] = distance[h][w]+1
                    stack.append([new_h, new_w])

        #start位置は0で初期化
        distance[x][y] = 0
        ans = max(ans, np.max(distance))

print(ans)

Fixed submission:
from collections import deque

def main():
    h, w = map(int, input().split())
    ls = [input() for _ in range(h)]

    def trans(a, b):
        return a * w + b

    ls2 = [[] for _ in range(h * w)]

    for i in range(h):
        for j in range(w):
            # check ij
            if ls[i][j] == '#':
                continue
            if i >= 1 and ls[i-1][j] != '#':
                ls2[trans(i, j)].append(trans(i-1, j))
            if i <= h-2 and ls[i+1][j] != '#':
                ls2[trans(i, j)].append(trans(i + 1, j))
            if j <= w-2 and ls[i][j+1] != '#':
                ls2[trans(i, j)].append(trans(i, j + 1))
            if j >= 1 and ls[i][j-1] != '#':
                ls2[trans(i, j)].append(trans(i, j - 1))


    #print(ls2)


    def route(i):
        count = 1
        ss = deque(ls2[i])
        next = deque()
        explored = {i}
        while ss or next:
            if not ss:
                count +=1
                ss = next
                next = deque()
            now =ss.popleft()
            if now in explored:
                continue
            else:
                explored.add(now)
                next.extend(ls2[now])
        return count-1
    ans = 0
    for i in range(h * w - 1):
        if ls2[i] == []:
            continue
        ans = max(ans, route(i))

    print(ans)
if __name__ == '__main__':
    main()

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

def main():
    h, w = map(int, input().split())
    ls = [input() for _ in range(h)]

    def trans(a, b):
        return a * w + b

    ls2 = [[] for _ in range(h * w)]

    for i in range(h):
        for j in range(w):
            # check ij
            if ls[i][j] == '#':
                continue
            if i >= 1 and ls[i-1][j] != '#':
                ls2[trans(i, j)].append(trans(i-1, j))
            if i <= h-2 and ls[i+1][j] != '#':
                ls2[trans(i, j)].append(trans(i + 1, j))
            if j <= w-2 and ls[i][j+1] != '#':
                ls2[trans(i, j)].append(trans(i, j + 1))
            if j >= 1 and ls[i][j-1] != '#':
                ls2[trans(i, j)].append(trans(i, j - 1))


    #print(ls2)


    def route(i, j):
        count = 1
        ss = deque(ls2[i])
        next = deque()
        explored = {i}
        while True:
            if not ss:
                count +=1
                ss = next
                next = deque()
            now =ss.popleft()
            if now in explored:
                continue
            if now == j:
                return count
            else:
                explored.add(now)
                next.extend(ls2[now])
    ans = 0
    for i in range(h * w - 1):
        if ls2[i] == []:
            continue
        for j in range(i+1, h * w):
            if ls2[j] == []:
                continue
            else:
                ans = max(ans, route(i, j))
    print(ans)
if __name__ == '__main__':
    main()

Fixed submission:
from collections import deque

def main():
    h, w = map(int, input().split())
    ls = [input() for _ in range(h)]

    def trans(a, b):
        return a * w + b

    ls2 = [[] for _ in range(h * w)]

    for i in range(h):
        for j in range(w):
            # check ij
            if ls[i][j] == '#':
                continue
            if i >= 1 and ls[i-1][j] != '#':
                ls2[trans(i, j)].append(trans(i-1, j))
            if i <= h-2 and ls[i+1][j] != '#':
                ls2[trans(i, j)].append(trans(i + 1, j))
            if j <= w-2 and ls[i][j+1] != '#':
                ls2[trans(i, j)].append(trans(i, j + 1))
            if j >= 1 and ls[i][j-1] != '#':
                ls2[trans(i, j)].append(trans(i, j - 1))


    #print(ls2)


    def route(i):
        count = 1
        ss = deque(ls2[i])
        next = deque()
        explored = {i}
        while ss or next:
            if not ss:
                count +=1
                ss = next
                next = deque()
            now =ss.popleft()
            if now in explored:
                continue
            else:
                explored.add(now)
                next.extend(ls2[now])
        return count-1
    ans = 0
    for i in range(h * w - 1):
        if ls2[i] == []:
            continue
        ans = max(ans, route(i))

    print(ans)
if __name__ == '__main__':
    main()

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

def main():
    h, w = map(int, input().split())
    ls = [input() for _ in range(h)]

    def trans(a, b):
        return a * w + b

    ls2 = [[] for _ in range(h * w)]

    for i in range(h):
        for j in range(w):
            # check ij
            if ls[i][j] == '#':
                continue
            if i >= 1 and ls[i-1][j] != '#':
                ls2[trans(i, j)].append(trans(i-1, j))
            if i <= h-2 and ls[i+1][j] != '#':
                ls2[trans(i, j)].append(trans(i + 1, j))
            if j <= w-2 and ls[i][j+1] != '#':
                ls2[trans(i, j)].append(trans(i, j + 1))
            if j >= 1 and ls[i][j-1] != '#':
                ls2[trans(i, j)].append(trans(i, j - 1))


    #print(ls2)


    def route(i, j):
        count = 1
        ss = deque(ls2[i])
        next = deque()
        explored = {i}
        while True:
            if not ss:
                count +=1
                ss = next
                next = deque()
            now =ss.popleft()
            if now in explored:
                continue
            if now == j:
                return count
            else:
                explored.add(now)
                next.extend(ls2[now])
    ans = 0
    for i in range(h * w - 1):
        if ls2[i] == []:
            continue
        for j in range(i+1, h * w):
            if ls2[j] == []:
                continue
            else:
                ans = max(ans, route(i, j))
    print(ans)
if __name__ == '__main__':
    main()

Fixed submission:
from collections import deque

def main():
    h, w = map(int, input().split())
    ls = [input() for _ in range(h)]

    def trans(a, b):
        return a * w + b

    ls2 = [[] for _ in range(h * w)]

    for i in range(h):
        for j in range(w):
            # check ij
            if ls[i][j] == '#':
                continue
            if i >= 1 and ls[i-1][j] != '#':
                ls2[trans(i, j)].append(trans(i-1, j))
            if i <= h-2 and ls[i+1][j] != '#':
                ls2[trans(i, j)].append(trans(i + 1, j))
            if j <= w-2 and ls[i][j+1] != '#':
                ls2[trans(i, j)].append(trans(i, j + 1))
            if j >= 1 and ls[i][j-1] != '#':
                ls2[trans(i, j)].append(trans(i, j - 1))


    #print(ls2)


    def route(i):
        count = 1
        ss = deque(ls2[i])
        next = deque()
        explored = {i}
        while ss or next:
            if not ss:
                count +=1
                ss = next
                next = deque()
            now =ss.popleft()
            if now in explored:
                continue
            else:
                explored.add(now)
                next.extend(ls2[now])
        return count-1
    ans = 0
    for i in range(h * w - 1):
        if ls2[i] == []:
            continue
        ans = max(ans, route(i))

    print(ans)
if __name__ == '__main__':
    main()

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

def main():
    h, w = map(int, input().split())
    ls = []
    for _ in range(h):
        ls1 = []
        s = input()
        for i in range(w):
            if s[i] == '.':
                ls1.append(0)
            else:
                ls1.append(1)
        ls.append(ls1)

    def trans(a, b):
        return a * w + b

    ls2 = [[] for _ in range(h * w)]

    for i in range(h):
        for j in range(w):
            # check ij
            if ls[i][j] == 1:
                continue
            if i >= 1 and ls[i-1][j] != 1:
                ls2[trans(i, j)].append(trans(i-1, j))
            if i <= h-2 and ls[i+1][j] != 1:
                ls2[trans(i, j)].append(trans(i + 1, j))
            if j <= w-2 and ls[i][j+1] != 1:
                ls2[trans(i, j)].append(trans(i, j + 1))
            if j >= 1 and ls[i][j-1] != 1:
                ls2[trans(i, j)].append(trans(i, j - 1))


    #print(ls2)


    def route(i, j):
        count = 1
        ss = deque(ls2[i])
        next = deque()
        explored = {i}
        while True:
            if not ss:
                count +=1
                ss = next
                next = deque()
            now =ss.popleft()
            if now in explored:
                continue
            if now == j:
                return count
            else:
                explored.add(now)
                next.extend(ls2[now])
    ans = 0
    for i in range(h * w - 1):
        if ls2[i] == []:
            continue
        for j in range(i+1, h * w):
            if ls2[j] == []:
                continue
            else:
                ans = max(ans, route(i, j))
    print(ans)
if __name__ == '__main__':
    main()

Fixed submission:
from collections import deque

def main():
    h, w = map(int, input().split())
    ls = [input() for _ in range(h)]

    def trans(a, b):
        return a * w + b

    ls2 = [[] for _ in range(h * w)]

    for i in range(h):
        for j in range(w):
            # check ij
            if ls[i][j] == '#':
                continue
            if i >= 1 and ls[i-1][j] != '#':
                ls2[trans(i, j)].append(trans(i-1, j))
            if i <= h-2 and ls[i+1][j] != '#':
                ls2[trans(i, j)].append(trans(i + 1, j))
            if j <= w-2 and ls[i][j+1] != '#':
                ls2[trans(i, j)].append(trans(i, j + 1))
            if j >= 1 and ls[i][j-1] != '#':
                ls2[trans(i, j)].append(trans(i, j - 1))


    #print(ls2)


    def route(i):
        count = 1
        ss = deque(ls2[i])
        next = deque()
        explored = {i}
        while ss or next:
            if not ss:
                count +=1
                ss = next
                next = deque()
            now =ss.popleft()
            if now in explored:
                continue
            else:
                explored.add(now)
                next.extend(ls2[now])
        return count-1
    ans = 0
    for i in range(h * w - 1):
        if ls2[i] == []:
            continue
        ans = max(ans, route(i))

    print(ans)
if __name__ == '__main__':
    main()

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

def main():
    h, w = map(int, input().split())
    ls = []
    for _ in range(h):
        ls1 = []
        s = input()
        for i in range(w):
            if s[i] == '.':
                ls1.append(0)
            else:
                ls1.append(1)
        ls.append(ls1)

    def trans(a, b):
        return a * w + b

    ls2 = [[] for _ in range(h * w)]

    for i in range(h):
        for j in range(w):
            # check ij
            if ls[i][j] == 1:
                continue
            if i >= 1 and ls[i-1][j] != 1:
                ls2[trans(i, j)].append(trans(i-1, j))
            if i <= h-2 and ls[i+1][j] != 1:
                ls2[trans(i, j)].append(trans(i + 1, j))
            if j <= w-2 and ls[i][j+1] != 1:
                ls2[trans(i, j)].append(trans(i, j + 1))
            if j >= 1 and ls[i][j-1] != 1:
                ls2[trans(i, j)].append(trans(i, j - 1))


    #print(ls2)


    def route(i, j):
        count = 1
        ss = deque(ls2[i])
        next = deque()
        explored = {i}
        while True:
            if not ss:
                count +=1
                ss = next
                next = deque()
            now =ss.popleft()
            if now in explored:
                continue
            if now == j:
                return count
            else:
                explored.add(now)
                next.extend(ls2[now])
    ans = 0
    for i in range(h * w - 1):
        if ls2[i] == []:
            continue
        for j in range(i+1, h * w):
            if ls2[j] == []:
                continue
            else:
                ans = max(ans, route(i, j))
    print(ans)
if __name__ == '__main__':
    main()

Fixed submission:
from collections import deque

def main():
    h, w = map(int, input().split())
    ls = [input() for _ in range(h)]

    def trans(a, b):
        return a * w + b

    ls2 = [[] for _ in range(h * w)]

    for i in range(h):
        for j in range(w):
            # check ij
            if ls[i][j] == '#':
                continue
            if i >= 1 and ls[i-1][j] != '#':
                ls2[trans(i, j)].append(trans(i-1, j))
            if i <= h-2 and ls[i+1][j] != '#':
                ls2[trans(i, j)].append(trans(i + 1, j))
            if j <= w-2 and ls[i][j+1] != '#':
                ls2[trans(i, j)].append(trans(i, j + 1))
            if j >= 1 and ls[i][j-1] != '#':
                ls2[trans(i, j)].append(trans(i, j - 1))


    #print(ls2)


    def route(i):
        count = 1
        ss = deque(ls2[i])
        next = deque()
        explored = {i}
        while ss or next:
            if not ss:
                count +=1
                ss = next
                next = deque()
            now =ss.popleft()
            if now in explored:
                continue
            else:
                explored.add(now)
                next.extend(ls2[now])
        return count-1
    ans = 0
    for i in range(h * w - 1):
        if ls2[i] == []:
            continue
        ans = max(ans, route(i))

    print(ans)
if __name__ == '__main__':
    main()

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

def main():
    h, w = map(int, input().split())
    ls = []
    for _ in range(h):
        ls1 = []
        s = input()
        for i in range(w):
            if s[i] == '.':
                ls1.append(0)
            else:
                ls1.append(1)
        ls.append(ls1)

    def trans(a, b):
        return a * w + b

    ls2 = [[] for _ in range(h * w)]

    for i in range(h):
        for j in range(w):
            # check ij
            if ls[i][j] == 1:
                continue
            if 0 <= i < h and 0 <= j - 1 < w and ls[i][j - 1] == 0:
                ls2[trans(i, j)].append(trans(i, j - 1))
                #print(ls2)
            if 0 <= i + 1 < h and 0 <= j < w and ls[i + 1][j] == 0:
                ls2[trans(i, j)].append(trans(i + 1, j))
                #print(ls2)
            if 0 <= i - 1 < h and 0 <= j < w and ls[i - 1][j] == 0:
                ls2[trans(i, j)].append(trans(i - 1, j))
                #print(ls2)
            if 0 <= i < h and 0 <= j + 1 < w and ls[i][j + 1] == 0:
                ls2[trans(i, j)].append(trans(i, j + 1))

    def route(i, j):
        count = 1
        ss = deque(ls2[i])
        next = deque()
        explored = {i}
        while True:
            if not ss:
                count +=1
                ss = next
                next = deque()
            now =ss.popleft()
            if now in explored:
                continue
            if now == j:
                return count
            explored.add(now)
            next.extend(ls2[now])
    ans = 0
    for i in range(h * w - 1):
        if ls2[i] == []:
            continue
        for j in range(i+1, h * w):
            if ls2[j] == []:
                continue
            else:
                ans = max(ans, route(i, j))
    print(ans)
if __name__ == '__main__':
    main()

Fixed submission:
from collections import deque

def main():
    h, w = map(int, input().split())
    ls = [input() for _ in range(h)]

    def trans(a, b):
        return a * w + b

    ls2 = [[] for _ in range(h * w)]

    for i in range(h):
        for j in range(w):
            # check ij
            if ls[i][j] == '#':
                continue
            if i >= 1 and ls[i-1][j] != '#':
                ls2[trans(i, j)].append(trans(i-1, j))
            if i <= h-2 and ls[i+1][j] != '#':
                ls2[trans(i, j)].append(trans(i + 1, j))
            if j <= w-2 and ls[i][j+1] != '#':
                ls2[trans(i, j)].append(trans(i, j + 1))
            if j >= 1 and ls[i][j-1] != '#':
                ls2[trans(i, j)].append(trans(i, j - 1))


    #print(ls2)


    def route(i):
        count = 1
        ss = deque(ls2[i])
        next = deque()
        explored = {i}
        while ss or next:
            if not ss:
                count +=1
                ss = next
                next = deque()
            now =ss.popleft()
            if now in explored:
                continue
            else:
                explored.add(now)
                next.extend(ls2[now])
        return count-1
    ans = 0
    for i in range(h * w - 1):
        if ls2[i] == []:
            continue
        ans = max(ans, route(i))

    print(ans)
if __name__ == '__main__':
    main()

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

def main():
    h, w = map(int, input().split())
    ls = []
    for _ in range(h):
        ls1 = []
        s = input()
        for i in range(w):
            if s[i] == '.':
                ls1.append(0)
            else:
                ls1.append(1)
        ls.append(ls1)

    def trans(a, b):
        return a * w + b

    ls2 = [[] for _ in range(h * w)]

    for i in range(h):
        for j in range(w):
            # check ij
            if ls[i][j] == 1:
                continue
            if 0 <= i < h and 0 <= j - 1 < w and ls[i][j - 1] == 0:
                ls2[trans(i, j)].append(trans(i, j - 1))
                #print(ls2)
            if 0 <= i + 1 < h and 0 <= j < w and ls[i + 1][j] == 0:
                ls2[trans(i, j)].append(trans(i + 1, j))
                #print(ls2)
            if 0 <= i - 1 < h and 0 <= j < w and ls[i - 1][j] == 0:
                ls2[trans(i, j)].append(trans(i - 1, j))
                #print(ls2)
            if 0 <= i < h and 0 <= j + 1 < w and ls[i][j + 1] == 0:
                ls2[trans(i, j)].append(trans(i, j + 1))

    def route(i, j):
        count = 1
        ss = deque(ls2[i])
        next = deque()
        explored = {i}
        while True:
            if not ss:
                count +=1
                ss = next
                next = deque()
            now =ss.popleft()
            if now in explored:
                continue
            if now == j:
                return count
            else:
                explored.add(now)
                next.extend(ls2[now])
    ans = 0
    for i in range(h * w - 1):
        if ls2[i] == []:
            continue
        for j in range(i+1, h * w):
            if ls2[j] == []:
                continue
            else:
                ans = max(ans, route(i, j))
    print(ans)
if __name__ == '__main__':
    main()

Fixed submission:
from collections import deque

def main():
    h, w = map(int, input().split())
    ls = [input() for _ in range(h)]

    def trans(a, b):
        return a * w + b

    ls2 = [[] for _ in range(h * w)]

    for i in range(h):
        for j in range(w):
            # check ij
            if ls[i][j] == '#':
                continue
            if i >= 1 and ls[i-1][j] != '#':
                ls2[trans(i, j)].append(trans(i-1, j))
            if i <= h-2 and ls[i+1][j] != '#':
                ls2[trans(i, j)].append(trans(i + 1, j))
            if j <= w-2 and ls[i][j+1] != '#':
                ls2[trans(i, j)].append(trans(i, j + 1))
            if j >= 1 and ls[i][j-1] != '#':
                ls2[trans(i, j)].append(trans(i, j - 1))


    #print(ls2)


    def route(i):
        count = 1
        ss = deque(ls2[i])
        next = deque()
        explored = {i}
        while ss or next:
            if not ss:
                count +=1
                ss = next
                next = deque()
            now =ss.popleft()
            if now in explored:
                continue
            else:
                explored.add(now)
                next.extend(ls2[now])
        return count-1
    ans = 0
    for i in range(h * w - 1):
        if ls2[i] == []:
            continue
        ans = max(ans, route(i))

    print(ans)
if __name__ == '__main__':
    main()

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

def main():
    h, w = map(int, input().split())
    ls = []
    for _ in range(h):
        ls1 = []
        s = input()
        for i in range(w):
            if s[i] == '.':
                ls1.append(0)
            else:
                ls1.append(1)
        ls.append(ls1)

    def trans(a, b):
        return a * w + b

    ls2 = [[] for _ in range(h * w)]

    for i in range(h):
        for j in range(w):
            # check ij
            if ls[i][j] == 1:
                continue
            if 0 <= i < h and 0 <= j - 1 < w and ls[i][j - 1] == 0:
                ls2[trans(i, j)].append(trans(i, j - 1))
                #print(ls2)
            if 0 <= i + 1 < h and 0 <= j < w and ls[i + 1][j] == 0:
                ls2[trans(i, j)].append(trans(i + 1, j))
                #print(ls2)
            if 0 <= i - 1 < h and 0 <= j < w and ls[i - 1][j] == 0:
                ls2[trans(i, j)].append(trans(i - 1, j))
                #print(ls2)
            if 0 <= i < h and 0 <= j + 1 < w and ls[i][j + 1] == 0:
                ls2[trans(i, j)].append(trans(i, j + 1))

    def route(i, j):
        count = 1
        ss = deque(ls2[i])
        next = deque()
        explored = {i}
        while True:
            if not ss:
                count +=1
                ss = next
                next = deque()
            now =ss.popleft()
            if now in explored:
                continue
            if now == j:
                return count
            else:
                explored.add(now)
                next.extend(ls2[now])
    ans = 0
    for i in range(h * w - 1):
        if ls2[i] == []:
            continue
        for j in range(i+1, h * w):
            if ls2[j] == []:
                continue
            else:
                ans = max(ans, route(i, j))
    print(ans)
if __name__ == '__main__':
    main()

Fixed submission:
from collections import deque

def main():
    h, w = map(int, input().split())
    ls = [input() for _ in range(h)]

    def trans(a, b):
        return a * w + b

    ls2 = [[] for _ in range(h * w)]

    for i in range(h):
        for j in range(w):
            # check ij
            if ls[i][j] == '#':
                continue
            if i >= 1 and ls[i-1][j] != '#':
                ls2[trans(i, j)].append(trans(i-1, j))
            if i <= h-2 and ls[i+1][j] != '#':
                ls2[trans(i, j)].append(trans(i + 1, j))
            if j <= w-2 and ls[i][j+1] != '#':
                ls2[trans(i, j)].append(trans(i, j + 1))
            if j >= 1 and ls[i][j-1] != '#':
                ls2[trans(i, j)].append(trans(i, j - 1))


    #print(ls2)


    def route(i):
        count = 1
        ss = deque(ls2[i])
        next = deque()
        explored = {i}
        while ss or next:
            if not ss:
                count +=1
                ss = next
                next = deque()
            now =ss.popleft()
            if now in explored:
                continue
            else:
                explored.add(now)
                next.extend(ls2[now])
        return count-1
    ans = 0
    for i in range(h * w - 1):
        if ls2[i] == []:
            continue
        ans = max(ans, route(i))

    print(ans)
if __name__ == '__main__':
    main()

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

def main():
    h, w = map(int, input().split())
    ls = []
    for _ in range(h):
        ls1 = []
        s = input()
        for i in range(w):
            if s[i] == '.':
                ls1.append(0)
            else:
                ls1.append(1)
        ls.append(ls1)

    def trans(a, b):
        return a * w + b

    ls2 = [[] for _ in range(h * w)]

    for i in range(h):
        for j in range(w):
            # check ij
            if ls[i][j] == 1:
                continue
            if 0 <= i < h and 0 <= j - 1 < w and ls[i][j - 1] == 0:
                ls2[trans(i, j)].append(trans(i, j - 1))
                #print(ls2)
            if 0 <= i + 1 < h and 0 <= j < w and ls[i + 1][j] == 0:
                ls2[trans(i, j)].append(trans(i + 1, j))
                #print(ls2)
            if 0 <= i - 1 < h and 0 <= j < w and ls[i - 1][j] == 0:
                ls2[trans(i, j)].append(trans(i - 1, j))
                #print(ls2)
            if 0 <= i < h and 0 <= j + 1 < w and ls[i][j + 1] == 0:
                ls2[trans(i, j)].append(trans(i, j + 1))

    def route(i, j):
        count = 1
        ss = deque(ls2[i])
        next = deque()
        explored = {i}
        while True:
            if not ss:
                count +=1
                ss = next
                next = deque()
            now =ss.popleft()
            if now in explored:
                continue
            if now == j:
                return count
            else:
                explored.add(now)
                next.extend(ls2[now])
    ans = 0
    for i in range(h * w - 1):
        if ls2[i] == []:
            continue
        count = 0
        for j in range(i+1, h * w):
            if ls2[j] == []:
                continue
            else:
                count = max(count, route(i, j))
        ans = max(count, ans)
    print(ans)
if __name__ == '__main__':
    main()

Fixed submission:
from collections import deque

def main():
    h, w = map(int, input().split())
    ls = [input() for _ in range(h)]

    def trans(a, b):
        return a * w + b

    ls2 = [[] for _ in range(h * w)]

    for i in range(h):
        for j in range(w):
            # check ij
            if ls[i][j] == '#':
                continue
            if i >= 1 and ls[i-1][j] != '#':
                ls2[trans(i, j)].append(trans(i-1, j))
            if i <= h-2 and ls[i+1][j] != '#':
                ls2[trans(i, j)].append(trans(i + 1, j))
            if j <= w-2 and ls[i][j+1] != '#':
                ls2[trans(i, j)].append(trans(i, j + 1))
            if j >= 1 and ls[i][j-1] != '#':
                ls2[trans(i, j)].append(trans(i, j - 1))


    #print(ls2)


    def route(i):
        count = 1
        ss = deque(ls2[i])
        next = deque()
        explored = {i}
        while ss or next:
            if not ss:
                count +=1
                ss = next
                next = deque()
            now =ss.popleft()
            if now in explored:
                continue
            else:
                explored.add(now)
                next.extend(ls2[now])
        return count-1
    ans = 0
    for i in range(h * w - 1):
        if ls2[i] == []:
            continue
        ans = max(ans, route(i))

    print(ans)
if __name__ == '__main__':
    main()

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

def main():
    h, w = map(int, input().split())
    ls = []
    for _ in range(h):
        ls1 = []
        s = input()
        for i in range(w):
            if s[i] == '.':
                ls1.append(0)
            else:
                ls1.append(1)
        ls.append(ls1)

    def trans(a, b):
        return a * w + b

    ls2 = [[] for _ in range(h * w)]

    for i in range(h):
        for j in range(w):
            # check ij
            if ls[i][j] == 1:
                continue
            if 0 <= i < h and 0 <= j - 1 < w and ls[i][j - 1] == 0:
                ls2[trans(i, j)].append(trans(i, j - 1))
                #print(ls2)
            if 0 <= i + 1 < h and 0 <= j < w and ls[i + 1][j] == 0:
                ls2[trans(i, j)].append(trans(i + 1, j))
                #print(ls2)
            if 0 <= i - 1 < h and 0 <= j < w and ls[i - 1][j] == 0:
                ls2[trans(i, j)].append(trans(i - 1, j))
                #print(ls2)
            if 0 <= i < h and 0 <= j + 1 < w and ls[i][j + 1] == 0:
                ls2[trans(i, j)].append(trans(i, j + 1))

    def route(i, j):
        count = 1
        ss = deque(ls2[i])
        next = deque()
        explored = {i}
        while True:
            if not ss:
                count +=1
                ss = next
                next = deque()
            now =ss.popleft()
            if now in explored:
                continue
            if now == j:
                return count
            else:
                explored.add(now)
                next.extend(ls2[now])
    ans = 0
    for i in range(h * w - 1):
        if ls2[i] == []:
            continue
        count = 0
        for j in range(i+1, h * w):
            if ls2[j] == []:
                continue
            else:
                count = max(count, route(i, j))
        ans = max(count, ans)
    print(ans)
if __name__ == '__main__':
    main()

Fixed submission:
from collections import deque

def main():
    h, w = map(int, input().split())
    ls = [input() for _ in range(h)]

    def trans(a, b):
        return a * w + b

    ls2 = [[] for _ in range(h * w)]

    for i in range(h):
        for j in range(w):
            # check ij
            if ls[i][j] == '#':
                continue
            if i >= 1 and ls[i-1][j] != '#':
                ls2[trans(i, j)].append(trans(i-1, j))
            if i <= h-2 and ls[i+1][j] != '#':
                ls2[trans(i, j)].append(trans(i + 1, j))
            if j <= w-2 and ls[i][j+1] != '#':
                ls2[trans(i, j)].append(trans(i, j + 1))
            if j >= 1 and ls[i][j-1] != '#':
                ls2[trans(i, j)].append(trans(i, j - 1))


    #print(ls2)


    def route(i):
        count = 1
        ss = deque(ls2[i])
        next = deque()
        explored = {i}
        while ss or next:
            if not ss:
                count +=1
                ss = next
                next = deque()
            now =ss.popleft()
            if now in explored:
                continue
            else:
                explored.add(now)
                next.extend(ls2[now])
        return count-1
    ans = 0
    for i in range(h * w - 1):
        if ls2[i] == []:
            continue
        ans = max(ans, route(i))

    print(ans)
if __name__ == '__main__':
    main()

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

def main():
    h, w = map(int, input().split())
    ls = []
    for _ in range(h):
        ls1 = []
        s = input()
        for i in range(w):
            if s[i] == '.':
                ls1.append(0)
            else:
                ls1.append(1)
        ls.append(ls1)

    def trans(a, b):
        return a * w + b

    ls2 = [[] for _ in range(h * w)]

    for i in range(h):
        for j in range(w):
            # check ij
            if ls[i][j] == 1:
                continue
            if 0 <= i < h and 0 <= j - 1 < w and ls[i][j - 1] == 0:
                ls2[trans(i, j)].append(trans(i, j - 1))
                #print(ls2)
            if 0 <= i + 1 < h and 0 <= j < w and ls[i + 1][j] == 0:
                ls2[trans(i, j)].append(trans(i + 1, j))
                #print(ls2)
            if 0 <= i - 1 < h and 0 <= j < w and ls[i - 1][j] == 0:
                ls2[trans(i, j)].append(trans(i - 1, j))
                #print(ls2)
            if 0 <= i < h and 0 <= j + 1 < w and ls[i][j + 1] == 0:
                ls2[trans(i, j)].append(trans(i, j + 1))

    def route(i, j):
        count = 1
        ss = deque(ls2[i])
        next = deque()
        explored = {i}
        while True:
            if not ss:
                count +=1
                ss = next
                next = deque()
            now =ss.popleft()
            if now in explored:
                continue
            if now == j:
                return count
            else:
                explored.add(now)
                next.extend(ls2[now])

    ls3 = [0] * (h * w)

    for i in range(h * w - 1):
        if ls2[i] == []:
            continue
        count = 0
        for j in range(i+1, h * w):
            if ls2[j] == []:
                continue
            else:
                count = max(count, route(i, j))
        ls3[i] = count
    print(max(ls3))
if __name__ == '__main__':
    main()

Fixed submission:
from collections import deque

def main():
    h, w = map(int, input().split())
    ls = [input() for _ in range(h)]

    def trans(a, b):
        return a * w + b

    ls2 = [[] for _ in range(h * w)]

    for i in range(h):
        for j in range(w):
            # check ij
            if ls[i][j] == '#':
                continue
            if i >= 1 and ls[i-1][j] != '#':
                ls2[trans(i, j)].append(trans(i-1, j))
            if i <= h-2 and ls[i+1][j] != '#':
                ls2[trans(i, j)].append(trans(i + 1, j))
            if j <= w-2 and ls[i][j+1] != '#':
                ls2[trans(i, j)].append(trans(i, j + 1))
            if j >= 1 and ls[i][j-1] != '#':
                ls2[trans(i, j)].append(trans(i, j - 1))


    #print(ls2)


    def route(i):
        count = 1
        ss = deque(ls2[i])
        next = deque()
        explored = {i}
        while ss or next:
            if not ss:
                count +=1
                ss = next
                next = deque()
            now =ss.popleft()
            if now in explored:
                continue
            else:
                explored.add(now)
                next.extend(ls2[now])
        return count-1
    ans = 0
    for i in range(h * w - 1):
        if ls2[i] == []:
            continue
        ans = max(ans, route(i))

    print(ans)
if __name__ == '__main__':
    main()

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

def main():
    h, w = map(int, input().split())
    ls = []
    for _ in range(h):
        ls1 = []
        s = input()
        for i in range(w):
            if s[i] == '.':
                ls1.append(0)
            else:
                ls1.append(1)
        ls.append(ls1)

    def trans(a, b):
        return a * w + b

    ls2 = [[] for _ in range(h * w)]

    for i in range(h):
        for j in range(w):
            # check ij
            if ls[i][j] == 1:
                continue
            if 0 <= i < h and 0 <= j - 1 < w and ls[i][j - 1] == 0:
                ls2[trans(i, j)].append(trans(i, j - 1))
                #print(ls2)
            if 0 <= i + 1 < h and 0 <= j < w and ls[i + 1][j] == 0:
                ls2[trans(i, j)].append(trans(i + 1, j))
                #print(ls2)
            if 0 <= i - 1 < h and 0 <= j < w and ls[i - 1][j] == 0:
                ls2[trans(i, j)].append(trans(i - 1, j))
                #print(ls2)
            if 0 <= i < h and 0 <= j + 1 < w and ls[i][j + 1] == 0:
                ls2[trans(i, j)].append(trans(i, j + 1))

    def route(i, j):
        count = 1
        ss = deque(ls2[i])
        next = deque()
        explored = {i}
        while True:
            if not ss:
                count +=1
                ss = next
                next = deque()
            now =ss.popleft()
            if now in explored:
                continue
            if now == j:
                return count
            else:
                explored.add(now)
                next.extend(ls2[now])

    ls3 = [0] * (h * w)

    for i in range(h * w):
        if ls2[i] == []:
            continue
        count = 0
        for j in range(h * w):
            if ls2[j] == [] or i == j:
                continue
            else:
                count = max(count, route(i, j))
        ls3[i] = count
    print(max(ls3))
if __name__ == '__main__':
    main()

Fixed submission:
from collections import deque

def main():
    h, w = map(int, input().split())
    ls = [input() for _ in range(h)]

    def trans(a, b):
        return a * w + b

    ls2 = [[] for _ in range(h * w)]

    for i in range(h):
        for j in range(w):
            # check ij
            if ls[i][j] == '#':
                continue
            if i >= 1 and ls[i-1][j] != '#':
                ls2[trans(i, j)].append(trans(i-1, j))
            if i <= h-2 and ls[i+1][j] != '#':
                ls2[trans(i, j)].append(trans(i + 1, j))
            if j <= w-2 and ls[i][j+1] != '#':
                ls2[trans(i, j)].append(trans(i, j + 1))
            if j >= 1 and ls[i][j-1] != '#':
                ls2[trans(i, j)].append(trans(i, j - 1))


    #print(ls2)


    def route(i):
        count = 1
        ss = deque(ls2[i])
        next = deque()
        explored = {i}
        while ss or next:
            if not ss:
                count +=1
                ss = next
                next = deque()
            now =ss.popleft()
            if now in explored:
                continue
            else:
                explored.add(now)
                next.extend(ls2[now])
        return count-1
    ans = 0
    for i in range(h * w - 1):
        if ls2[i] == []:
            continue
        ans = max(ans, route(i))

    print(ans)
if __name__ == '__main__':
    main()

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

def main():
    h, w = map(int, input().split())
    ls = []
    for _ in range(h):
        ls1 = []
        s = input()
        for i in range(w):
            if s[i] == '.':
                ls1.append(0)
            else:
                ls1.append(1)
        ls.append(ls1)

    def trans(a, b):
        return a * w + b

    ls2 = [[] for _ in range(h * w)]

    for i in range(h):
        for j in range(w):
            # check ij
            if ls[i][j] == 1:
                continue
            if 0 <= i < h and 0 <= j - 1 < w and ls[i][j - 1] == 0:
                ls2[trans(i, j)].append(trans(i, j - 1))
                #print(ls2)
            if 0 <= i + 1 < h and 0 <= j < w and ls[i + 1][j] == 0:
                ls2[trans(i, j)].append(trans(i + 1, j))
                #print(ls2)
            if 0 <= i - 1 < h and 0 <= j < w and ls[i - 1][j] == 0:
                ls2[trans(i, j)].append(trans(i - 1, j))
                #print(ls2)
            if 0 <= i < h and 0 <= j + 1 < w and ls[i][j + 1] == 0:
                ls2[trans(i, j)].append(trans(i, j + 1))

    def route(i, j):
        count = 1
        ss = deque(ls2[i])
        next = deque()
        now = i
        explored = {i}
        while True:
            #print(now, i, j)
            if not ss:
                count +=1
                ss = next
                next = deque()
            now =ss.popleft()
            if now in explored:
                continue
            if now == j:
                #print(count)
                return count
            else:
                explored.add(now)
                next.extend(ls2[now])

    ls3 = [0] * (h * w)

    for i in range(h * w):
        if ls2[i] == []:
            continue
        count = 0
        for j in range(h * w):
            if ls2[j] == [] or i == j:
                continue
            else:
                count = max( count, route(i, j))
        ls3[i] = count

    print(max(ls3))

if __name__ == '__main__':
    main()

Fixed submission:
from collections import deque

def main():
    h, w = map(int, input().split())
    ls = [input() for _ in range(h)]

    def trans(a, b):
        return a * w + b

    ls2 = [[] for _ in range(h * w)]

    for i in range(h):
        for j in range(w):
            # check ij
            if ls[i][j] == '#':
                continue
            if i >= 1 and ls[i-1][j] != '#':
                ls2[trans(i, j)].append(trans(i-1, j))
            if i <= h-2 and ls[i+1][j] != '#':
                ls2[trans(i, j)].append(trans(i + 1, j))
            if j <= w-2 and ls[i][j+1] != '#':
                ls2[trans(i, j)].append(trans(i, j + 1))
            if j >= 1 and ls[i][j-1] != '#':
                ls2[trans(i, j)].append(trans(i, j - 1))


    #print(ls2)


    def route(i):
        count = 1
        ss = deque(ls2[i])
        next = deque()
        explored = {i}
        while ss or next:
            if not ss:
                count +=1
                ss = next
                next = deque()
            now =ss.popleft()
            if now in explored:
                continue
            else:
                explored.add(now)
                next.extend(ls2[now])
        return count-1
    ans = 0
    for i in range(h * w - 1):
        if ls2[i] == []:
            continue
        ans = max(ans, route(i))

    print(ans)
if __name__ == '__main__':
    main()

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

def main():
    h, w = map(int, input().split())
    ls = []
    for _ in range(h):
        ls1 = []
        s = input()
        for i in range(w):
            if s[i] == '.':
                ls1.append(0)
            else:
                ls1.append(1)
        ls.append(ls1)

    def trans(a, b):
        return a * w + b

    ls2 = [[] for _ in range(h * w)]

    for i in range(h):
        for j in range(w):
            # check ij
            if ls[i][j] == 1:
                continue
            if 0 <= i < h and 0 <= j - 1 < w and ls[i][j - 1] == 0:
                ls2[trans(i, j)].append(trans(i, j - 1))
                #print(ls2)
            if 0 <= i + 1 < h and 0 <= j < w and ls[i + 1][j] == 0:
                ls2[trans(i, j)].append(trans(i + 1, j))
                #print(ls2)
            if 0 <= i - 1 < h and 0 <= j < w and ls[i - 1][j] == 0:
                ls2[trans(i, j)].append(trans(i - 1, j))
                #print(ls2)
            if 0 <= i < h and 0 <= j + 1 < w and ls[i][j + 1] == 0:
                ls2[trans(i, j)].append(trans(i, j + 1))

    def route(i, j):
        if i == j:
            return 0
        count = 1
        ss = deque(ls2[i])
        next = deque()
        now = i
        explored = {i}
        while True:
            #print(now, i, j)
            if not ss:
                count +=1
                ss = next
                next = deque()
            now =ss.popleft()
            if now in explored:
                continue
            if now == j:
                #print(count)
                return count
            else:
                explored.add(now)
                next.extend(ls2[now])

    ls3 = [0] * (h * w)

    for i in range(h * w):
        if ls2[i] == []:
            continue
        count = 0
        for j in range(h * w):
            if ls2[j] == []:
                continue
            else:
                count = max( count, route(i, j))
        ls3[i] = count

    print(max(ls3))

if __name__ == '__main__':
    main()

Fixed submission:
from collections import deque

def main():
    h, w = map(int, input().split())
    ls = [input() for _ in range(h)]

    def trans(a, b):
        return a * w + b

    ls2 = [[] for _ in range(h * w)]

    for i in range(h):
        for j in range(w):
            # check ij
            if ls[i][j] == '#':
                continue
            if i >= 1 and ls[i-1][j] != '#':
                ls2[trans(i, j)].append(trans(i-1, j))
            if i <= h-2 and ls[i+1][j] != '#':
                ls2[trans(i, j)].append(trans(i + 1, j))
            if j <= w-2 and ls[i][j+1] != '#':
                ls2[trans(i, j)].append(trans(i, j + 1))
            if j >= 1 and ls[i][j-1] != '#':
                ls2[trans(i, j)].append(trans(i, j - 1))


    #print(ls2)


    def route(i):
        count = 1
        ss = deque(ls2[i])
        next = deque()
        explored = {i}
        while ss or next:
            if not ss:
                count +=1
                ss = next
                next = deque()
            now =ss.popleft()
            if now in explored:
                continue
            else:
                explored.add(now)
                next.extend(ls2[now])
        return count-1
    ans = 0
    for i in range(h * w - 1):
        if ls2[i] == []:
            continue
        ans = max(ans, route(i))

    print(ans)
if __name__ == '__main__':
    main()

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

def main():
    h, w = map(int, input().split())
    ls = []
    for _ in range(h):
        ls1 = []
        s = input()
        for i in range(w):
            if s[i] == '.':
                ls1.append(0)
            else:
                ls1.append(1)
        ls.append(ls1)

    def trans(a, b):
        return a * w + b

    ls2 = [[] for _ in range(h * w)]

    for i in range(h):
        for j in range(w):
            # check ij
            if ls[i][j] == 1:
                continue
            if 0 <= i < h and 0 <= j - 1 < w and ls[i][j - 1] == 0:
                ls2[trans(i, j)].append(trans(i, j - 1))
                #print(ls2)
            if 0 <= i + 1 < h and 0 <= j < w and ls[i + 1][j] == 0:
                ls2[trans(i, j)].append(trans(i + 1, j))
                #print(ls2)
            if 0 <= i - 1 < h and 0 <= j < w and ls[i - 1][j] == 0:
                ls2[trans(i, j)].append(trans(i - 1, j))
                #print(ls2)
            if 0 <= i < h and 0 <= j + 1 < w and ls[i][j + 1] == 0:
                ls2[trans(i, j)].append(trans(i, j + 1))

    def route(i, j):
        if i == j:
            return 0
        count = 1
        ss = deque(ls2[i])
        next = deque()
        now = i
        explored = {i}
        while True:
            #print(now, i, j)
            if not ss:
                count +=1
                ss = next
                next = deque()
            now =ss.popleft()
            if now in explored:
                continue
            if now == j:
                #print(count)
                return count
            else:
                explored.add(now)
                next.extend(ls2[now])

    ls3 = [0] * (h * w)

    for i in range(h * w):
        if ls2[i] == []:
            continue
        count = 0
        for j in range(h * w):
            if ls2[j] == []:
                continue
            else:
                count = max( count, route(i, j))
        ls3[i] = count

    print(max(ls3))

if __name__ == '__main__':
    main()

Fixed submission:
from collections import deque

def main():
    h, w = map(int, input().split())
    ls = [input() for _ in range(h)]

    def trans(a, b):
        return a * w + b

    ls2 = [[] for _ in range(h * w)]

    for i in range(h):
        for j in range(w):
            # check ij
            if ls[i][j] == '#':
                continue
            if i >= 1 and ls[i-1][j] != '#':
                ls2[trans(i, j)].append(trans(i-1, j))
            if i <= h-2 and ls[i+1][j] != '#':
                ls2[trans(i, j)].append(trans(i + 1, j))
            if j <= w-2 and ls[i][j+1] != '#':
                ls2[trans(i, j)].append(trans(i, j + 1))
            if j >= 1 and ls[i][j-1] != '#':
                ls2[trans(i, j)].append(trans(i, j - 1))


    #print(ls2)


    def route(i):
        count = 1
        ss = deque(ls2[i])
        next = deque()
        explored = {i}
        while ss or next:
            if not ss:
                count +=1
                ss = next
                next = deque()
            now =ss.popleft()
            if now in explored:
                continue
            else:
                explored.add(now)
                next.extend(ls2[now])
        return count-1
    ans = 0
    for i in range(h * w - 1):
        if ls2[i] == []:
            continue
        ans = max(ans, route(i))

    print(ans)
if __name__ == '__main__':
    main()

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

h, w = map(int, input().split())
ls = []
for _ in range(h):
    ls1 = []
    s = input()
    for i in range(w):
        if s[i] == '.':
            ls1.append(0)
        else:
            ls1.append(1)
    ls.append(ls1)

def trans(a, b):
    return a * w + b

ls2 = [[] for _ in range(h * w)]

for i in range(h):
    for j in range(w):
        # check ij
        if ls[i][j] == 1:
            continue
        if 0 <= i < h and 0 <= j - 1 < w and ls[i][j - 1] == 0:
            ls2[trans(i, j)].append(trans(i, j - 1))
            #print(ls2)
        if 0 <= i + 1 < h and 0 <= j < w and ls[i + 1][j] == 0:
            ls2[trans(i, j)].append(trans(i + 1, j))
            #print(ls2)
        if 0 <= i - 1 < h and 0 <= j < w and ls[i - 1][j] == 0:
            ls2[trans(i, j)].append(trans(i - 1, j))
            #print(ls2)
        if 0 <= i < h and 0 <= j + 1 < w and ls[i][j + 1] == 0:
            ls2[trans(i, j)].append(trans(i, j + 1))

def route(i, j):
    if i == j:
        return 0
    count = 1
    ss = deque(ls2[i])
    next = deque()
    now = i
    explored = set([i])
    while True:
        #print(now, i, j)
        if not ss:
            count +=1
            ss = next
            next = deque()
        now =ss.popleft()
        if now in explored:
            continue
        if now == j:
            #print(count)
            return count
        else:
            explored.add(now)
            next.extend(ls2[now])

ls3 = [0] * (h * w)

for i in range(h * w):
    if ls2[i] == []:
        continue
    count = 0
    for j in range(h * w):
        if ls2[j] == []:
            continue
        else:
            count = max( count, route(i, j))
    ls3[i] = count

print(max(ls3))

Fixed submission:
from collections import deque

def main():
    h, w = map(int, input().split())
    ls = [input() for _ in range(h)]

    def trans(a, b):
        return a * w + b

    ls2 = [[] for _ in range(h * w)]

    for i in range(h):
        for j in range(w):
            # check ij
            if ls[i][j] == '#':
                continue
            if i >= 1 and ls[i-1][j] != '#':
                ls2[trans(i, j)].append(trans(i-1, j))
            if i <= h-2 and ls[i+1][j] != '#':
                ls2[trans(i, j)].append(trans(i + 1, j))
            if j <= w-2 and ls[i][j+1] != '#':
                ls2[trans(i, j)].append(trans(i, j + 1))
            if j >= 1 and ls[i][j-1] != '#':
                ls2[trans(i, j)].append(trans(i, j - 1))


    #print(ls2)


    def route(i):
        count = 1
        ss = deque(ls2[i])
        next = deque()
        explored = {i}
        while ss or next:
            if not ss:
                count +=1
                ss = next
                next = deque()
            now =ss.popleft()
            if now in explored:
                continue
            else:
                explored.add(now)
                next.extend(ls2[now])
        return count-1
    ans = 0
    for i in range(h * w - 1):
        if ls2[i] == []:
            continue
        ans = max(ans, route(i))

    print(ans)
if __name__ == '__main__':
    main()

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

h, w = map(int, input().split())
ls = []
for _ in range(h):
    ls1 = []
    s = input()
    for i in range(w):
        if s[i] == '.':
            ls1.append(0)
        else:
            ls1.append(1)
    ls.append(ls1)

def trans(a, b):
    return a * w + b

ls2 = [[] for _ in range(h * w)]

for i in range(h):
    for j in range(w):
        # check ij
        if ls[i][j] == 1:
            continue
        if 0 <= i < h and 0 <= j - 1 < w and ls[i][j - 1] == 0:
            ls2[trans(i, j)].append(trans(i, j - 1))
            #print(ls2)
        if 0 <= i + 1 < h and 0 <= j < w and ls[i + 1][j] == 0:
            ls2[trans(i, j)].append(trans(i + 1, j))
            #print(ls2)
        if 0 <= i - 1 < h and 0 <= j < w and ls[i - 1][j] == 0:
            ls2[trans(i, j)].append(trans(i - 1, j))
            #print(ls2)
        if 0 <= i < h and 0 <= j + 1 < w and ls[i][j + 1] == 0:
            ls2[trans(i, j)].append(trans(i, j + 1))

def route(i, j):
    if i == j:
        return 0
    count = 1
    ss = deque(ls2[i])
    next = deque()
    now = i
    explored = [i]
    while True:
        #print(now, i, j)
        if not ss:
            count +=1
            ss = next
            next = deque()
        now =ss.popleft()
        if now in explored:
            continue
        if now == j:
            #print(count)
            return count
        else:
            next.extend(ls2[now])

ls3 = [0] * (h * w)

for i in range(h * w):
    if ls2[i] == []:
        continue
    count = 0
    for j in range(h * w):
        if ls2[j] == []:
            continue
        else:
            count = max( count, route(i, j))
    ls3[i] = count

print(max(ls3))

Fixed submission:
from collections import deque

def main():
    h, w = map(int, input().split())
    ls = [input() for _ in range(h)]

    def trans(a, b):
        return a * w + b

    ls2 = [[] for _ in range(h * w)]

    for i in range(h):
        for j in range(w):
            # check ij
            if ls[i][j] == '#':
                continue
            if i >= 1 and ls[i-1][j] != '#':
                ls2[trans(i, j)].append(trans(i-1, j))
            if i <= h-2 and ls[i+1][j] != '#':
                ls2[trans(i, j)].append(trans(i + 1, j))
            if j <= w-2 and ls[i][j+1] != '#':
                ls2[trans(i, j)].append(trans(i, j + 1))
            if j >= 1 and ls[i][j-1] != '#':
                ls2[trans(i, j)].append(trans(i, j - 1))


    #print(ls2)


    def route(i):
        count = 1
        ss = deque(ls2[i])
        next = deque()
        explored = {i}
        while ss or next:
            if not ss:
                count +=1
                ss = next
                next = deque()
            now =ss.popleft()
            if now in explored:
                continue
            else:
                explored.add(now)
                next.extend(ls2[now])
        return count-1
    ans = 0
    for i in range(h * w - 1):
        if ls2[i] == []:
            continue
        ans = max(ans, route(i))

    print(ans)
if __name__ == '__main__':
    main()

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


h, w = map(int, input().split())
ls = []  # [[0, 0, 0, 1, 0], [0, 1, 0, 1, 0], [0, 1, 0, 0, 0]]
for _ in range(h):
    ls1 = []
    s = input()
    for i in range(w):
        if s[i] == '.':
            ls1.append(0)
        else:
            ls1.append(1)
    ls.append(ls1)

def trans(a, b):
    return a * w + b

ls2 = [[] for _ in range(h * w)]


for i in range(h):
    for j in range(w):
        # check ij
        if ls[i][j] == 1:
            continue

        if 0 <= i < h and 0 <= j - 1 < w and ls[i][j - 1] == 0:
            ls2[trans(i, j)].append(trans(i, j - 1))
            #print(ls2)
        if 0 <= i + 1 < h and 0 <= j < w and ls[i + 1][j] == 0:
            ls2[trans(i, j)].append(trans(i + 1, j))
            #print(ls2)
        if 0 <= i - 1 < h and 0 <= j < w and ls[i - 1][j] == 0:
            ls2[trans(i, j)].append(trans(i - 1, j))
            #print(ls2)
        if 0 <= i < h and 0 <= j + 1 < w and ls[i][j + 1] == 0:
            ls2[trans(i, j)].append(trans(i, j + 1))

#print(ls2)


def route(i, j):
    if i == j:
        return 0
    count = 1
    ss = deque(ls2[i])
    next = deque()
    now = i
    explored = [i]
    while True:
        #print(now, i, j)
        if not ss:
            count +=1
            ss = next
            next = deque()
        now =ss.popleft()
        if now in explored:
            continue
        if now == j:
            #print(count)
            return count
        else:
            next.extend(ls2[now])


ls3 = [0] * (h * w)


for i in range(h * w):
    if ls2[i] == []:
        continue
    count = 0
    for j in range(h * w):
        if ls2[j] == []:
            continue
        else:
            count = max( count, route(i, j))
    ls3[i] = count

print(max(ls3))

Fixed submission:
from collections import deque

def main():
    h, w = map(int, input().split())
    ls = [input() for _ in range(h)]

    def trans(a, b):
        return a * w + b

    ls2 = [[] for _ in range(h * w)]

    for i in range(h):
        for j in range(w):
            # check ij
            if ls[i][j] == '#':
                continue
            if i >= 1 and ls[i-1][j] != '#':
                ls2[trans(i, j)].append(trans(i-1, j))
            if i <= h-2 and ls[i+1][j] != '#':
                ls2[trans(i, j)].append(trans(i + 1, j))
            if j <= w-2 and ls[i][j+1] != '#':
                ls2[trans(i, j)].append(trans(i, j + 1))
            if j >= 1 and ls[i][j-1] != '#':
                ls2[trans(i, j)].append(trans(i, j - 1))


    #print(ls2)


    def route(i):
        count = 1
        ss = deque(ls2[i])
        next = deque()
        explored = {i}
        while ss or next:
            if not ss:
                count +=1
                ss = next
                next = deque()
            now =ss.popleft()
            if now in explored:
                continue
            else:
                explored.add(now)
                next.extend(ls2[now])
        return count-1
    ans = 0
    for i in range(h * w - 1):
        if ls2[i] == []:
            continue
        ans = max(ans, route(i))

    print(ans)
if __name__ == '__main__':
    main()