Problem:


Score : 400 points

Problem Statement

Takahashi has a maze, which is a grid of H \times W squares with H horizontal rows and W vertical columns.

The square at the i-th row from the top and the j-th column is a "wall" square if S_{ij} is #, and a "road" square if S_{ij} is ..

From a road square, you can move to a horizontally or vertically adjacent road square.

You cannot move out of the maze, move to a wall square, or move diagonally.

Takahashi will choose a starting square and a goal square, which can be any road squares, and give the maze to Aoki.

Aoki will then travel from the starting square to the goal square, in the minimum number of moves required.

In this situation, find the maximum possible number of moves Aoki has to make.

Constraints

1 \leq H,W \leq 20

S_{ij} is . or #.

S contains at least two occurrences of ..

Any road square can be reached from any road square in zero or more moves.

Input

Input is given from Standard Input in the following format:

H W
S_{11}...S_{1W}
:
S_{H1}...S_{HW}

Output

Print the maximum possible number of moves Aoki has to make.

Sample Input 1

3 3
...
...
...

Sample Output 1

4

If Takahashi chooses the top-left square as the starting square and the bottom-right square as the goal square, Aoki has to make four moves.

Sample Input 2

3 5
...#.
.#.#.
.#...

Sample Output 2

10

If Takahashi chooses the bottom-left square as the starting square and the top-right square as the goal square, Aoki has to make ten moves.



Sample input:
3 3
...
...
...


Sample output:
4

Submission status: Time Limit Exceeded

Solution code:
from collections import deque
import sys
input = sys.stdin.readline


def bfs(table, i, j):
    dq = deque()
    seen = []
    if table[i][j] == "#":
        return -1
    h = len(table)
    w = len(table[0])
    ans = 0
    dq.append((i, j, 0))  # x,y,distance
    while dq:
        x, y, dis = dq.popleft()
        ans = max(ans, dis)
        seen.append((x, y))
        if x > 0 and (x - 1, y) not in seen and table[x - 1][y] != "#":
            dq.append((x - 1, y, dis + 1))
        if x < h - 1 and (x + 1, y) not in seen and table[x + 1][y] != "#":
            dq.append((x + 1, y, dis + 1))
        if y > 0 and (x, y - 1) not in seen and table[x][y - 1] != "#":
            dq.append((x, y - 1, dis + 1))
        if y < w - 1 and (x, y + 1) not in seen and table[x][y + 1] != "#":
            dq.append((x, y + 1, dis + 1))
    return ans


def main():
    h, w = [int(i) for i in input().strip().split()]
    grid = [input().strip() for _ in range(h)]
    ans = -1
    for i in range(h):
        for j in range(w):
            if grid[i][j] == ".":
                max_path = bfs(grid, i, j)
                ans = max(ans, max_path)

    print(ans)


if __name__ == "__main__":
    main()

Submission status: Time Limit Exceeded

Solution code:
from collections import deque

H, W = [int(s) for s in input().split()]
fields = []
for _ in range(H):
    fields.append(list(input()))

fields_num = []
for i in range(H):
    for j in range(W):
        fields_num.append((i, j))


def get_search_obj(i, j):
    ret = []
    moving_range = [[1, 0], [0, 1], [-1, 0], [0, -1]]
    for dx, dy in moving_range:
        if 0 <= (i + dx) <= (H - 1) and 0 <= (j + dy) <= (W - 1) and fields[i + dx][j + dy] == '.':
            ret.append((i + dx, j + dy))
    return ret


def bfs(start, end):
    start = [start[0], start[1], 0]
    dq = deque([start])
    visited = [[False] * W for _ in range(H)]
    while dq:
        sx, sy, cnt = dq.pop()
        for x, y in get_search_obj(sx, sy):
            if x == end[0] and y == end[1]:
                return cnt + 1
            if not visited[x][y]:
                dq.appendleft([x, y, cnt + 1])
                visited[x][y] = True
    return 0


max_hop = 0
for i in range(H * W):
    for j in range(i + 1, H * W):
        h1, w1 = fields_num[i]
        h2, w2 = fields_num[j]
        if fields[h1][w1] == '.' and fields[h2][w2] == '.':
            max_hop = max(max_hop, bfs((h1, w1), (h2, w2)))

print(max_hop)

Submission status: Time Limit Exceeded

Solution code:
import os, sys, re, math


(H, W) = [int(n) for n in input().split()]


def set_moves(x, y, moves):
    if S[y + 1][x + 1] != '.':
        return
    if 0 <= grid[y][x] < moves:
        return

    grid[y][x] = moves
    set_moves(x, y - 1, moves + 1)
    set_moves(x, y + 1, moves + 1)
    set_moves(x - 1, y, moves + 1)
    set_moves(x + 1, y, moves + 1)


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

result = 0
for gy in range(H):
    for gx in range(W):
        if S[gy + 1][gx + 1] != '.':
            continue
        grid = []
        for i in range(H):
            grid.append([-1] * W)
        set_moves(gx, gy, 0)

        result = max(result, max([max(row) for row in grid]))

print(result)

Submission status: Time Limit Exceeded

Solution code:
import os, sys, re, math


(H, W) = [int(n) for n in input().split()]


def set_moves(x, y, moves):
    if S[y + 1][x + 1] != '.':
        return
    if 0 <= grid[y][x] < moves:
        return

    grid[y][x] = moves
    set_moves(x, y - 1, moves + 1)
    set_moves(x, y + 1, moves + 1)
    set_moves(x - 1, y, moves + 1)
    set_moves(x + 1, y, moves + 1)


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

result = 0
for gy in range(H):
    for gx in range(W):
        grid = []
        for i in range(H):
            grid.append([-1] * W)
        set_moves(gx, gy, 0)

        result = max(result, max([max(row) for row in grid]))

print(result)

Submission status: Time Limit Exceeded

Solution code:
def resolve():
    H, W = list(map(int, input().split()))
    S = [list(input()) for _ in range(H)]
    import collections
    maxdist = 0

    for startx in range(H):
        for starty in range(W):
            if S[startx][starty] == "#":
                continue
            visited = [[-1 for _ in range(W)] for __ in range(H)]
            q = collections.deque([(startx, starty, 0)])
            while q:
                x, y, dist = q.pop()
                visited[x][y] = dist
                for dx, dy in [(-1, 0), (1, 0), (0, -1), (0, 1)]:
                    nx = x + dx
                    ny = y + dy
                    if 0 <= nx < H and 0 <= ny < W and S[nx][ny] != "#" and visited[nx][ny] == -1:
                        q.appendleft((nx, ny, dist+1))
            maxdist = max(maxdist,max(sum(visited, [])))
    print(maxdist)
    

if '__main__' == __name__:
    resolve()

Submission status: Time Limit Exceeded

Solution code:
MOD = 10**9+7
def resolve():
    H, W = list(map(int, input().split()))
    S = [list(input()) for _ in range(H)]
    import collections
    maxdist = 0
    for startx in range(H):
        for starty in range(W):
            if S[startx][starty] == "#":
                continue
            visited = [[-1 for _ in range(W)] for __ in range(H)]
            q = collections.deque([(startx, starty, 0)])
            while q:
                x, y, dist = q.pop()
                visited[x][y] = dist
                for dx, dy in [[-1, 0], [1, 0], [0, -1], [0, 1]]:
                    nx = x + dx
                    ny = y + dy
                    if 0 <= nx < H and 0 <= ny < W and S[nx][ny] != "#" and visited[nx][ny] == -1:
                        q.appendleft((nx, ny, dist+1))
            maxdist = max(maxdist,max(sum(visited, [])))
    print(maxdist)
    

if '__main__' == __name__:
    resolve()

Submission status: Time Limit Exceeded

Solution code:
MOD = 10**9+7
def resolve():
    H, W = list(map(int, input().split()))
    S = [list(input()) for _ in range(H)]
    import collections
    maxdist = 0
    for startx in range(H):
        for starty in range(W):
            if S[startx][starty] == "#":
                continue
            visited = [[False for _ in range(W)] for __ in range(H)]
            q = collections.deque([(startx, starty, 0)])
            while q:
                x, y, dist = q.pop()
                visited[x][y] = True
                maxdist = max(maxdist, dist)
                for dx, dy in [[-1, 0], [1, 0], [0, -1], [0, 1]]:
                    nx = x + dx
                    ny = y + dy
                    if 0 <= nx < H and 0 <= ny < W and S[nx][ny] != "#" and visited[nx][ny] is False:
                        q.appendleft((nx, ny, dist+1))
    print(maxdist)
    

if '__main__' == __name__:
    resolve()

Submission status: Time Limit Exceeded

Solution code:
MOD = 10**9+7
def resolve():
    H, W = list(map(int, input().split()))
    S = [list(input()) for _ in range(H)]
    import collections
    maxdist = 0
    for startx in range(H):
        for starty in range(W):
            if S[startx][starty] == "#":
                continue
            visited = [[False for _ in range(W)] for __ in range(H)]
            q = collections.deque([(startx, starty, 0)])
            while q:
                x, y, dist = q.pop()
                visited[x][y] = True
                maxdist = max(maxdist, dist)
                for dx, dy in [[-1, 0], [1, 0], [0, -1], [0, 1]]:
                    nx = x + dx
                    ny = y + dy
                    if 0 <= nx < H and 0 <= ny < W and S[nx][ny] != "#" and visited[nx][ny] is False:
                        q.appendleft((nx, ny, dist+1))
    print(maxdist)
    

if '__main__' == __name__:
    resolve()

Submission status: Time Limit Exceeded

Solution code:
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()

Submission status: Time Limit Exceeded

Solution code:
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()

Submission status: Time Limit Exceeded

Solution code:
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()

Submission status: Time Limit Exceeded

Solution code:
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()

Submission status: Time Limit Exceeded

Solution code:
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()

Submission status: Time Limit Exceeded

Solution code:
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()

Submission status: Time Limit Exceeded

Solution code:
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()

Submission status: Time Limit Exceeded

Solution code:
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()

Submission status: Time Limit Exceeded

Solution code:
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()

Submission status: Time Limit Exceeded

Solution code:
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))

Submission status: Time Limit Exceeded

Solution code:
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))