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:
# coding:utf-8

(h, w) = [int(i) for i in input().rstrip().split(" ")]
map = [input().rstrip() for i in range(h)]

index_map = [[i * w + j for j in range(w)] for i in range(h)]
rev_index_map = [(int(i / w), i % w) for i in range(h * w)]
diff = [[h * w + 1 for i in range(h * w)] for j in range(h * w)]


def isSame(r1, c1, r2, c2):
    if r1 != r2:
        return False
    if c1 != c2:
        return False
    return True


def isNear(r1, c1, r2, c2):
    if r1 == r2:
        if abs(c1 - c2) == 1:
            return True
    elif abs(r1 - r2) == 1:
        if c1 == c2:
            return True
    return False


ans = 0

for idx1 in range(h * w):
    for idx2 in range(h * w):
        (r2, c2) = rev_index_map[idx2]
        if map[r2][c2] == "#":
            continue
        for idx3 in range(h * w):
            (r3, c3) = rev_index_map[idx3]
            if map[r3][c3] == "#":
                continue
            if isSame(r2, c2, r3, c3):
                diff[idx2][idx3] = 0
            elif isNear(r2, c2, r3, c3):
                diff[idx2][idx3] = 1
                diff[idx3][idx2] = 1
            else:
                tmp = diff[idx2][idx1] + diff[idx1][idx3]
                if diff[idx2][idx3] > tmp:
                    diff[idx2][idx3] = tmp
                    diff[idx3][idx2] = tmp

for i in range(h*w):
    for j in range(i+1, h*w):
        num = diff[i][j]
        if num > h*w:
            continue
        ans = max(ans,num)
print(ans)

Submission status: Time Limit Exceeded

Solution code:
# coding:utf-8

(h, w) = [int(i) for i in input().rstrip().split(" ")]
map = [input().rstrip() for i in range(h)]

index_map = [[i * w + j for j in range(w)] for i in range(h)]
rev_index_map = [(int(i / w), i % w) for i in range(h * w)]
diff = [[h * w + 1 for i in range(h * w)] for j in range(h * w)]


def isSame(r1, c1, r2, c2):
    if r1 != r2:
        return False
    if c1 != c2:
        return False
    return True


def isNear(r1, c1, r2, c2):
    if r1 == r2:
        if abs(c1 - c2) == 1:
            return True
    elif abs(r1 - r2) == 1:
        if c1 == c2:
            return True
    return False


ans = 0

for idx1 in range(h * w):
    for idx2 in range(h * w):
        (r2, c2) = rev_index_map[idx2]
        if map[r2][c2] == "#":
            continue
        for idx3 in range(h * w):
            (r3, c3) = rev_index_map[idx3]
            if map[r3][c3] == "#":
                continue
            if isSame(r2, c2, r3, c3):
                diff[idx2][idx3] = 0
            elif isNear(r2, c2, r3, c3):
                diff[idx2][idx3] = 1
                diff[idx3][idx2] = 1
            else:
                tmp = diff[idx2][idx1] + diff[idx1][idx3]
                if diff[idx2][idx3] > tmp:
                    diff[idx2][idx3] = tmp
                    diff[idx3][idx2] = tmp

for i in diff:
    for j in i:
        if j > h*w:
            continue
        ans = max(ans,j)
print(ans)

Submission status: Time Limit Exceeded

Solution code:
# coding:utf-8

(h, w) = [int(i) for i in input().rstrip().split(" ")]
map = [input().rstrip() for i in range(h)]

index_map = [[i * w + j for j in range(w)] for i in range(h)]
rev_index_map = [(int(i / w), i % w) for i in range(h * w)]
diff = [[h * w + 1 for i in range(h * w)] for j in range(h * w)]


def isSame(r1, c1, r2, c2):
    if r1 != r2:
        return False
    if c1 != c2:
        return False
    return True


def isNear(r1, c1, r2, c2):
    if r1 == r2:
        if abs(c1 - c2) == 1:
            return True
    elif abs(r1 - r2) == 1:
        if c1 == c2:
            return True
    return False


ans = 0

for idx1 in range(h * w):
    for idx2 in range(h * w):
        (r2, c2) = rev_index_map[idx2]
        if map[r2][c2] == "#":
            continue
        for idx3 in range(h * w):
            (r3, c3) = rev_index_map[idx3]
            if map[r3][c3] == "#":
                continue
            if isSame(r2, c2, r3, c3):
                diff[idx2][idx3] = 0
            elif isNear(r2, c2, r3, c3):
                diff[idx2][idx3] = 1
            else:
                tmp = diff[idx2][idx1] + diff[idx1][idx3]
                if diff[idx2][idx3] > tmp:
                    diff[idx2][idx3] = tmp

for i in diff:
    for j in i:
        if j > h*w:
            continue
        ans = max(ans,j)
print(ans)

Submission status: Time Limit Exceeded

Solution code:
# coding:utf-8

(h,w) = [int(i) for i in input().rstrip().split(" ")]
map = [input().rstrip() for i in range(h)]

index_map = [[i*w + j for j in range(w)] for i in range(h)]
diff = [[h*w+1 for i in range(h*w)] for j in range(h*w)]

for r1 in range(h):
    for c1 in range(w):
        idx1 = index_map[r1][c1]
        for r2 in range(h):
            for c2 in range(w):
                idx2 = index_map[r2][c2]
                if map[r2][c2] == "#":
                    continue
                for r3 in range(h):
                    for c3 in range(w):
                        idx3 = index_map[r3][c3]
                        if map[r3][c3] == "#":
                            continue
                        if r2 == r3 and c2 == c3:
                            diff[idx2][idx3] = 0
                        elif r2 == r3 and abs(c2 - c3) == 1:
                            diff[idx2][idx3] = 1
                        elif c2 == c3 and abs(r2-r3) == 1:
                            diff[idx2][idx3] = 1
                        elif map[r1][c1] == "#":
                            continue
                        elif diff[idx2][idx3] > diff[idx2][idx1] + diff[idx1][idx3]:
                            diff[idx2][idx3] = diff[idx2][idx1] + diff[idx1][idx3]

ans = 0
for i in diff:
    for j in i:
        if j > h*w:
            continue
        ans = max(ans, j)
print(ans)

Submission status: Time Limit Exceeded

Solution code:
H, W = map(int, input().split())
m = [list(input()) for _ in range(H)]
inf = float("inf")

V = H*W
maze = [[inf]*V for _ in range(V)]

for i in range(V):
    maze[i][i] = 0

for py in range(H):
    for px in range(W):
        if m[py][px] == "#":
            continue
        candidate = [(px+1, py), (px-1, py), (px, py+1), (px, py-1)]
        for x, y in candidate:
            if not (0<=x<W and 0<=y<H):continue
            if m[y][x] == "#":
                continue
            maze[W*py+px][W*y+x] = 1

for k in range(V):
    for i in range(V):
        for j in range(V):
            maze[i][j] = min(maze[i][j], maze[i][k]+maze[k][j])

print(max([j for i in maze for j in i if j!=inf]))

Submission status: Time Limit Exceeded

Solution code:
H, W = map(int, input().split())
m = [input() for _ in range(H)]
inf = float("inf")

V = H*W
maze = [[inf]*V for _ in range(V)]

for i in range(V):
    maze[i][i] = 0

for py in range(H):
    for px in range(W):
        if m[py][px] == "#":
            continue
        candidate = [(px+1, py), (px-1, py), (px, py+1), (px, py-1)]
        for x, y in candidate:
            if not (0<=x<W and 0<=y<H):continue
            if m[y][x] == "#":
                continue
            maze[W*py+px][W*y+x] = 1

for k in range(V):
    for i in range(V):
        for j in range(V):
            maze[i][j] = min(maze[i][j], maze[i][k]+maze[k][j])

print(max([j for i in maze for j in i if j!=inf]))

Submission status: Time Limit Exceeded

Solution code:
class Maze():
    def __init__(self, H, W, road='.', wall='#'):
        self.H=H
        self.W=W
        self.road='.'
        self.wall='#'
        self.S=[]
    
    def __len__(self):
        return len(self.S)

    def __getitem__(self, index):
        return self.S[index]

    def get(self,i,j):
        if len(self.S)!=self.H*self.W:
            raise ValueError('Maze is not ready')

        if i<0 or i>=self.H or j<0 or j>=self.W:
            return ''
        else:
            return self.S[self.W*i + j]

    def add_row(self,Si):
        if len(Si) != self.W:
            raise ValueError
        self.S.extend(Si)

    def get_adjacent(self, index):
        out = []
        i, j = self._index_to_ij(index)
        if self.get(i-1,j)!='':
            out.append(tuple([self._ij_to_index(i-1,j), self.get(i-1,j)]))
        if self.get(i+1,j)!='':
            out.append(tuple([self._ij_to_index(i+1,j), self.get(i+1,j)]))
        if self.get(i,j-1)!='':
            out.append(tuple([self._ij_to_index(i,j-1), self.get(i,j-1)]))
        if self.get(i,j+1)!='':
            out.append(tuple([self._ij_to_index(i,j+1), self.get(i,j+1)]))
        return out

    def _index_to_ij(self, index):
        i = index // self.W
        j = index % self.W
        return i, j

    def _ij_to_index(self, i, j):
        return self.W*i + j
      

def maze_to_graph(maze):
    G = []
    for node_index in range(len(maze)):
        Gi = []
        if maze[node_index]=='#':
            out = []
        else:
            out = maze.get_adjacent(node_index)
       
        for index, mark in out:
            if mark=='#':
                pass
            elif mark=='.':
                Gi.append(index)
        G.append(tuple(Gi))

    return G
  
  
from collections import deque

def bfs(G, init):
    seen = [False for i in range(len(G))]
    todo = deque()
    todo.append(init)

    dist = [-1 for i in range(len(G))]
    dist[init] = 0

    while todo:
        node_index = todo.popleft()
        seen[node_index] = True
        if dist[node_index]==-1:
            raise ValueError
        for i in G[node_index]:
            if seen[i] == True:
                pass
            else:
                todo.append(i)
                if dist[i]==-1:
                    dist[i]=dist[node_index]+1

    return dist
  
H, W = map(int, input().split())
input_maze = []
for i in range(H):
    input_maze.append(input())
maze = Maze(H,W)
for Si in input_maze:
    maze.add_row(Si)

G = maze_to_graph(maze)
dists = []
for i in range(len(G)):
    dists.append(max(bfs(G, i)))

print(max(dists))

Submission status: Time Limit Exceeded

Solution code:
from collections import deque
import sys
import numpy as np

input = sys.stdin.readline
H, W = map(int, input().split())

maze = np.zeros((H, W), dtype='int')


for y in range(H):
    for x, s in enumerate(input()):
        if s=="#":
            maze[y][x] = 1


def bfs(x, y):
    queue = deque([(x, y, 0)])
    visited = np.zeros_like(maze, dtype='int') 

    max_cost = 0
    while queue:
        px, py, cost = queue.popleft()
        max_cost = max(max_cost, cost)

        candidate = ((px + 1, py), (px - 1, py), (px, py + 1), (px, py - 1))
        visited[py][px] = 1
        for cx, cy in candidate:
            if 0 <= cx < W and 0 <= cy < H and visited[cy][cx] == 0 and maze[cy][cx] == 0:
                queue.append((cx, cy, cost + 1))
    
    return max_cost


ans = 0
for y in range(H):
    for x in range(W):
        if maze[y][x] == 0:
            ans = max(ans, bfs(x, y))

print(ans)

Submission status: Time Limit Exceeded

Solution code:
from collections import deque
import sys

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


def bfs(x, y):
    queue = deque([(x, y, 0)])
    visited = [[False]*W for _ in range(H)]

    max_cost = 0
    while queue:
        px, py, cost = queue.popleft()
        max_cost = max(max_cost, cost)

        candidate = ((px + 1, py), (px - 1, py), (px, py + 1), (px, py - 1))
        visited[py][px] = True
        for cx, cy in candidate:
            if 0 <= cx < W and 0 <= cy < H and not visited[cy][cx] and maze[cy][cx] == ".":
                queue.append((cx, cy, cost + 1))
    return max_cost


ans = 0
for y in range(H):
    for x in range(W):
        if maze[y][x] == ".":
            ans = max(ans, bfs(x, y))

print(ans)

Submission status: Time Limit Exceeded

Solution code:
from collections import deque
H, W = map(int, input().split())
maze = [list(input()) for _ in range(H)]


def bfs(x, y):
    queue = deque([(x, y, 0)])
    visited = [[False]*W for _ in range(H)]

    max_cost = 0
    while queue:
        px, py, cost = queue.popleft()
        max_cost = max(max_cost, cost)

        candidate = ((px + 1, py), (px - 1, py), (px, py + 1), (px, py - 1))
        visited[py][px] = True
        for cx, cy in candidate:
            if 0 <= cx < W and 0 <= cy < H and not visited[cy][cx] and maze[cy][cx] == ".":
                queue.append((cx, cy, cost + 1))
    return max_cost


ans = 0
for y in range(H):
    for x in range(W):
        if maze[y][x] == ".":
            ans = max(ans, bfs(x, y))

print(ans)

Submission status: Time Limit Exceeded

Solution code:
from collections import deque
H, W = map(int, input().split())
maze = [list(input()) for _ in range(H)]


def bfs(x, y):
    queue = deque([(x, y, 0)])
    visited = [[False]*W for _ in range(H)]

    max_cost = 0
    while queue:
        px, py, cost = queue.popleft()
        max_cost = max(max_cost, cost)

        candidate = ((px + 1, py), (px - 1, py), (px, py + 1), (px, py - 1))
        visited[py][px] = True
        for cx, cy in candidate:
            if 0 <= cx < W and 0 <= cy < H and not visited[cy][cx] and maze[cy][cx] == ".":
                queue.append((cx, cy, cost + 1))
    return max_cost


ans = 0
for y in range(H):
    for x in range(W):
        if maze[y][x] == ".":
            ans = max(ans, bfs(x, y))

print(ans)

Submission status: Time Limit Exceeded

Solution code:
from collections import deque
H, W = map(int, input().split())
maze = [input() for _ in range(H)]


def bfs(x, y):
    queue = deque([(x, y, 0)])
    visited = [[False]*W for _ in range(H)]

    max_cost = 0
    while queue:
        px, py, cost = queue.popleft()
        max_cost = max(max_cost, cost)

        candidate = ((px + 1, py), (px - 1, py), (px, py + 1), (px, py - 1))
        visited[py][px] = True
        for cx, cy in candidate:
            if 0 <= cx < W and 0 <= cy < H and not visited[cy][cx] and maze[cy][cx] == ".":
                queue.append((cx, cy, cost + 1))
    return max_cost


ans = 0
for y in range(H):
    for x in range(W):
        if maze[y][x] == ".":
            ans = max(ans, bfs(x, y))

print(ans)

Submission status: Time Limit Exceeded

Solution code:
from collections import deque
H, W = map(int, input().split())
maze = [input() for _ in range(H)]


def bfs(x, y):
    visited, queue = [], deque([(x, y, 0)])
    max_cost = 0
    while queue:
        px, py, cost = queue.popleft()
        max_cost = max(max_cost, cost)

        candidate = ((px + 1, py), (px - 1, py), (px, py + 1), (px, py - 1))
        visited.append((px, py))
        for cx, cy in candidate:
            if 0 <= cx < W and 0 <= cy < H and not (cx, cy) in visited and maze[cy][cx] == ".":
                queue.append((cx, cy, cost + 1))
    return max_cost


ans = 0
for y in range(H):
    for x in range(W):
        if maze[y][x] == ".":
            ans = max(ans, bfs(x, y))

print(ans)

Submission status: Time Limit Exceeded

Solution code:
from collections import deque
H, W = map(int, input().split())
maze = [list(input()) for _ in range(H)]


def bfs(x, y):
    visited, queue = [], deque([(x, y, 0)])
    max_cost = 0
    while queue:
        px, py, cost = queue.popleft()
        max_cost = max(max_cost, cost)

        candidate = ((px + 1, py), (px - 1, py), (px, py + 1), (px, py - 1))
        visited.append((px, py))
        for cx, cy in candidate:
            if 0 <= cx < W and 0 <= cy < H and not (cx, cy) in visited and maze[cy][cx] == ".":
                queue.append((cx, cy, cost + 1))
    return max_cost


ans = 0
for y in range(H):
    for x in range(W):
        if maze[y][x] == ".":
            ans = max(ans, bfs(x, y))

print(ans)

Submission status: Time Limit Exceeded

Solution code:
from collections import deque
H, W = map(int, input().split())
maze = [list(input()) for _ in range(H)]


def bfs(x, y):
    visited, queue = [], deque([(x, y, 0)])
    max_cost = 0
    while queue:
        px, py, cost = queue.popleft()
        max_cost = max(max_cost, cost)

        candidate = ((px + 1, py), (px - 1, py), (px, py + 1), (px, py - 1))
        visited.append((px, py))
        for cx, cy in candidate:
            if 0 <= cx < W and 0 <= cy < H and not (cx, cy) in visited and maze[cy][cx] == ".":
                queue.append((cx, cy, cost + 1))
    return max_cost


ans = 0
for y in range(H):
    for x in range(W):
        if maze[y][x] == "#":
            continue
        ans = max(ans, bfs(x, y))

print(ans)

Submission status: Time Limit Exceeded

Solution code:
from collections import deque

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


def bfs(x, y):
    visited, queue = [], deque([(x, y, 0)])
    max_cost = 0
    while queue:
        px, py, cost = queue.popleft()
        max_cost = max(max_cost, cost)

        candidate = ((px + 1, py), (px - 1, py), (px, py + 1), (px, py - 1))
        visited.append((px, py))
        for cx, cy in candidate:
            if not (0 <= cx < W and 0 <= cy < H):
                continue
            if (cx, cy) in visited:
                continue
            if maze[cy][cx] == "#":
                continue
            queue.append((cx, cy, cost + 1))
    return max_cost


ans = 0
for y in range(H):
    for x in range(W):
        if maze[y][x] == "#":
            continue
        ans = max(ans, bfs(x, y))

print(ans)

Submission status: Time Limit Exceeded

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


def bfs(x, y):
    visited, queue = [], [(x, y, 0)]
    max_cost = 0
    while queue:
        px, py, cost = queue.pop(0)
        max_cost = max(max_cost, cost)

        candidate = ((px + 1, py), (px - 1, py), (px, py + 1), (px, py - 1))
        visited.append((px, py))
        for cx, cy in candidate:
            if not (0 <= cx < W and 0 <= cy < H):
                continue
            if (cx, cy) in visited:
                continue
            if maze[cy][cx] == "#":
                continue
            queue.append((cx, cy, cost + 1))
    return max_cost


ans = 0
for y in range(H):
    for x in range(W):
        if maze[y][x] == "#":
            continue
        ans = max(ans, bfs(x, y))

print(ans)

Submission status: Time Limit Exceeded

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


def bfs(x, y):
    visited, queue = [], [(x, y, 0)]
    max_cost = 0
    while queue:
        px, py, cost = queue.pop(0)
        max_cost = max(max_cost, cost)

        candidate = ((px + 1, py), (px - 1, py), (px, py + 1), (px, py - 1))
        visited.append((px, py))
        for cx, cy in candidate:
            if not (0 <= cx < W and 0 <= cy < H):
                continue
            if maze[cy][cx] == "#":
                continue
            if (cx, cy) in visited:
                continue
            queue.append((cx, cy, cost + 1))
    return max_cost


ans = 0
for y in range(H):
    for x in range(W):
        if maze[y][x] == "#":
            continue
        ans = max(ans, bfs(x, y))

print(ans)

Submission status: Time Limit Exceeded

Solution code:
from collections import deque
import numpy as np
 
 
H,W= [int(x) for x in input().split()]
n=[]
T=np.zeros((H,W),dtype=int)
S=np.zeros_like(T,dtype=int) 
for i in range(H):
  n=input()
  for j in range(W):
    if n[j]=="#": T[i][j]=1
#B=T.copy()    
d=0
for i in range(H):
    for j in range(W):
         
        if T[i][j] ==1:continue
 
        c=deque()    
        s=0
        c.append((i,j))
        #c.append(j)
        
        t=len(c)
        while t>0:
                s+=1
                t=len(c)
                for k in range(t):
                        m,n = c.popleft()
                        #n = c.popleft()
                        S[m][n]=s
                        for kk in [(m+1,n),(m-1,n),(m,n+1),(m,n-1)]:
                          if not (0<=kk[0]<H and 0<= kk[1] <W): continue
                          if T[kk]==1:continue
                          if S[kk] == 0:
                            c.append(kk)
                            
                        
 
        #print(T)
        d = max(d,s)
        S=np.zeros_like(T,dtype=int)
        
print(d-2)

Submission status: Time Limit Exceeded

Solution code:
from collections import deque
import numpy as np
 
 
H,W= [int(x) for x in input().split()]
n=[]
T=np.zeros((H,W),dtype=int)
S=np.zeros_like(T,dtype=int) 
for i in range(H):
  n=input()
  for j in range(W):
    if n[j]=="#": T[i][j]=1
#B=T.copy()    
d=0
for i in range(H):
    for j in range(W):
         
        if T[i][j] ==1:continue
 
        c=deque()    
        s=0
        c.append(i)
        c.append(j)
        
        t=len(c)//2
        while t>0:
                s+=1
                t=len(c)//2
                for k in range(t):
                        m = c.popleft()
                        n = c.popleft()
                        S[m][n]=s
                        for kk in [(m+1,n),(m-1,n),(m,n+1),(m,n-1)]:
                          if not (0<=kk[0]<H and 0<= kk[1] <W): continue
                          if T[kk]==1:continue
                          if S[kk] == 0:
                            c.append(kk[0])
                            c.append(kk[1])
                        
 
        #print(T)
        d = max(d,s)
        S=np.zeros_like(T,dtype=int)
        
print(d-2)

Submission status: Time Limit Exceeded

Solution code:
from collections import deque
import numpy as np
 
 
H,W= [int(x) for x in input().split()]
n=[]
T=np.zeros((H,W),dtype=int)
S=np.zeros_like(T,dtype=int) 
for i in range(H):
  n=input()
  for j in range(W):
    if n[j]=="#": T[i][j]=1
#B=T.copy()    
d=0
for i in range(H):
    for j in range(W):
         
        if T[i][j] ==1:continue
 
        c=deque()    
        s=0
        c.append(i)
        c.append(j)
        
        t=len(c)//2
        while t>0:
                s+=1
                t=len(c)//2
                for k in range(t):
                        m = c.popleft()
                        n = c.popleft()
                        S[m][n]=s
                        if m != H-1:
                                if T[m+1][n]==0 and S[m+1][n]==0:
                                        c.append(m+1)
                                        c.append(n)
                        if m != 0:
                                if T[m-1][n]==0 and S[m-1][n]==0:
                                        c.append(m-1)
                                        c.append(n)
 
                        if n != W-1:
                                if T[m][n+1]==0 and S[m][n+1]==0:
                                        c.append(m)
                                        c.append(n+1)
                                        
                        if n != 0:
                                if T[m][n-1]==0 and S[m][n-1]==0:
                                        c.append(m)
                                        c.append(n-1)
 
        #print(T)
        d = max(d,s)
        S=np.zeros_like(T,dtype=int)
        
print(d-2)

Submission status: Time Limit Exceeded

Solution code:
from collections import deque
import numpy as np
 
 
H,W= [int(x) for x in input().split()]
n=[]
T=np.zeros((H,W),dtype=int)
S=np.zeros_like(T,dtype=int) 
for i in range(H):
  n=input()
  for j in range(W):
    if n[j]=="#": T[i][j]=1
B=T.copy()    
d=0
for i in range(H):
    for j in range(W):
         
        if T[i][j] ==1:continue
 
        c=deque()    
        s=0
        c.append(i)
        c.append(j)
        
        t=len(c)//2
        while t>0:
                s+=1
                t=len(c)//2
                for k in range(t):
                        m = c.popleft()
                        n = c.popleft()
                        S[m][n]=s
                        if m != H-1:
                                if T[m+1][n]==0 and S[m+1][n]==0:
                                        c.append(m+1)
                                        c.append(n)
                        if m != 0:
                                if T[m-1][n]==0 and S[m-1][n]==0:
                                        c.append(m-1)
                                        c.append(n)
 
                        if n != W-1:
                                if T[m][n+1]==0 and S[m][n+1]==0:
                                        c.append(m)
                                        c.append(n+1)
                                        
                        if n != 0:
                                if T[m][n-1]==0 and S[m][n-1]==0:
                                        c.append(m)
                                        c.append(n-1)
 
        #print(T)
        d = max(d,s)
        S=np.zeros_like(T,dtype=int)
        
print(d-2)

Submission status: Time Limit Exceeded

Solution code:
from collections import deque
import numpy as np
 
 
H,W= [int(x) for x in input().split()]
n=[]
T=np.zeros((H,W),dtype=int)

for i in range(H):
  n=input()
  for j in range(W):
    if n[j]=="#": T[i][j]=1
S= np.zeros_like(T, dtype='bool')
d=0
c=deque()

for i in range(H):
    for j in range(W):
         
        if T[i][j] ==1:continue

        c=deque()    
        s=0
        c+=[i,j]
        
        t=len(c)//2
        while t>0:
                s+=1
                t=len(c)//2
                for k in range(t):
                        m = c.popleft()
                        n = c.popleft()
                        S[m][n]=True
                        if m != H-1:
                                if T[m+1][n]==0 and S[m+1][n]==0:c+=[m+1,n]
                        if m != 0:
                                if T[m-1][n]==0 and S[m-1][n]==0:c+=[m-1,n] 
                        if n != W-1:
                                if T[m][n+1]==0 and S[m][n+1]==0:c+=[m,n+1]
                        if n != 0:
                                if T[m][n-1]==0 and S[m][n-1]==0:c+=[m,n-1]
                        #c += (chk_route([c.popleft(),c.popleft()]))
                
        #print(T)
        d = max(d,s)
        S= np.zeros_like(T, dtype='bool')
        
print(d-2)

Submission status: Time Limit Exceeded

Solution code:
from collections import deque
import numpy as np
 
 
H,W= [int(x) for x in input().split()]
n=[]
T=np.zeros((H,W),dtype=int)

for i in range(H):
  n=input()
  for j in range(W):
    if n[j]=="#": T[i][j]=1
S= np.zeros_like(T, dtype='bool')
d=0
c=deque()

for i in range(H):
    for j in range(W):
         
        if T[i][j] ==1:continue

        c=deque()    
        s=0
        c+=[i,j]
        
        t=len(c)//2
        while t>0:
                s+=1
                t=len(c)//2
                for k in range(t):
                        m = c.popleft()
                        n = c.popleft()
                        S[m][n]=s
                        if m != H-1:
                                if T[m+1][n]==0 and S[m+1][n]==0:c+=[m+1,n]
                        if m != 0:
                                if T[m-1][n]==0 and S[m-1][n]==0:c+=[m-1,n] 
                        if n != W-1:
                                if T[m][n+1]==0 and S[m][n+1]==0:c+=[m,n+1]
                        if n != 0:
                                if T[m][n-1]==0 and S[m][n-1]==0:c+=[m,n-1]
                        #c += (chk_route([c.popleft(),c.popleft()]))
                
        #print(T)
        d = max(d,s)
        S= np.zeros_like(T, dtype='bool')
        
print(d-2)

Submission status: Time Limit Exceeded

Solution code:
from collections import deque

H, W = list(map(int, input().split()))
S = []
for i in range(H):
  S.append(input())

def bfs(x,y):
  d = deque()
  d.append([x,y,0])
  visited = [[False for i in range(W)] for j in range(H)]

  maxd = -1
  while len(d) > 0:
    x,y,w = d.popleft()
    visited[x][y] = True
    next_point = []
    for dx,dy in [[1,0], [-1,0], [0,-1], [0,1]]:
      if 0 <= x + dx and x + dx < H and 0 <= y + dy and y + dy < W and S[x+dx][y+dy] == ".":
        next_point.append([x + dx, y + dy])
    for p, q in next_point:
      if visited[p][q]:
        continue
      d.append([p,q,w+1])
      maxd = max(maxd, w+1)

  return maxd

ans = -1
for X in range(H):
  for Y in range(W):
    if S[X][Y] == "#":
      continue
    ans = max(bfs(X,Y), ans)
    
print(ans)

Submission status: Time Limit Exceeded

Solution code:
from collections import deque
import numpy as np
 
 
H,W= [int(x) for x in input().split()]
n=[]
T=np.zeros((H,W),dtype=int)
 
for i in range(H):
  n=input()
  for j in range(W):
    if n[j]=="#": T[i][j]=1
B=T.copy()    
d=0
c=deque() 
for i in range(H):
    for j in range(W):
         
        if T[i][j] ==1:continue

        s=0
        c+=[i,j]
        t=len(c)//2
        while t>0:
                s+=1
                t=len(c)//2
                for k in range(t):
                        m = c.popleft()
                        n = c.popleft()
                        T[m][n]=s
                        if m != H-1:
                                if T[m+1][n]==0:c+=[m+1,n]
                        if m != 0:
                                if T[m-1][n]==0:c+=[m-1,n] 
                        if n != W-1:
                                if T[m][n+1]==0:c+=[m,n+1]
                        if n != 0:
                                if T[m][n-1]==0:c+=[m,n-1]
                        #c += (chk_route([c.popleft(),c.popleft()]))
                
        #print(T)
        d = max(d,s)
        T=B.copy()
        
print(d-2)