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: Runtime Error

Solution code:
H,W = map(int, input().split())

S = ["" for _ in range(H)]
for i in range(H):
    S[i] = input()

INF = float("inf")
dp = [[INF for _ in range(H*W)] for _ in range(H*W)]

for i in range(H):
    for j in range(W):
        
        if j != W-1 and S[i][j] == "." and S[i][j+1] == ".":
            dp[(H*i)+j][(H*i)+j+1] = 1
            dp[(H*i)+j+1][(H*i)+j] = 1

        if i != H-1 and S[i][j] == "." and S[i+1][j] == ".":
            dp[(H*i)+j][(H*(i+1))+j] = 1
            dp[(H*(i+1))+j][(H*i)+j] = 1
    
        dp[(H*i)+j][(H*i)+j] = 0


N = H*W
for k in range(N):
    for i in range(N):
        for j in range(N):
            dp[i][j] = min(dp[i][j], dp[i][k] + dp[k][j])

ans = 0
for i in range(N):
    for j in range(N):
        if dp[i][j] != INF:
            ans = max(ans, dp[i][j])
 
print(ans)

Submission status: Runtime Error

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

for i in range(H):
    S[i]=input()
    
def warshall_floyd(d):
    #d[i][j]: iからjへの最短距離
    for k in range(n):
        for i in range(n):
            for j in range(n):
                d[i][j] = min(d[i][j],d[i][k] + d[k][j])
    return d

n=H*W
inf=10**9

d = [[inf]*n for _ in range(n)] 
#d[u][v] : 辺uvのコスト(存在しないときはinf)
for i in range(H):
    for j in range(W):
        if S[i][j]==".":
            if j+1<W:
                if S[i][j+1]==".":
                    d[i*H+j][i*H+j+1]=1
                    d[i*H+j+1][i*H+j]=1
            if i+1<H:
                if S[i+1][j]==".":
                    d[i*H+j][(i+1)*H+j]=1
                    d[(i+1)*H+j][i*H+j]=1
    
        
for i in range(n):
    d[i][i] = 0 #自身のところに行くコストは０
ans=0

warshall_floyd(d)

for i in range(n):
    for j in range(n):
        if d[i][j]>=ans and d[i][j]<=inf-10:
            ans=d[i][j]

print(ans)

Submission status: Runtime Error

Solution code:
#幅優先探索


from queue import Queue

ans = 0
h,w = map(int, input().split())
maps = [input() for _ in range(h)]
for i in range(h):
    for j in range(w):
        if maps[i][j] == '.':
            visited = [[-1]*w for _ in range(h)]
            q = Queue()
            q.put([i,j])
            visited[i][j] = 0
            while not q.empty():
                y,x = q.get()
                for k,l in [[1,0],[-1,0],[0,1],[0,-1]]:
                    if 0 <= y+k < h and 0<= x+l < w:
                        new_y,new_x = y+k,x+l
                    else: continue
                    if visited[new_y][new_x]== -1 and maps[new_y][new_x]==".":
                        visited[new_y][new_x]=visited[y][x]+1
                        q.put([new_y,new_x])
                    elif visited[new_y][new_x] > visited[y][x]+1:
                        visited[new_y][new_x]=visited[y][x]+1
                        q.put([new_y,new_x])
          
         
    for ii in range(h):
        for jj in range(w):
            ans = max(ans,visited[ii][jj])
        
print(ans)

Submission status: Runtime Error

Solution code:
def d_maze_master(INF=float('inf')):
    from collections import deque
    H, W = [int(i) for i in input().split()]
    Maze = [list(input()) for _ in range(H)]

    start_row, start_col = None, None
    for row in range(H):
        for col in range(W):
            if Maze[row][col] == '.':
                start_row, start_col = row, col
                break
        else:
            continue
        break

    diff = ((-1, 0), (0, 1), (1, 0), (0, -1))

    def is_inside(row, col):
        return 0 <= row < H and 0 <= col < W

    queue = deque([(start_row, start_col)])
    dist = [[INF for _ in range(W)] for _ in range(H)]
    dist[start_row][start_col] = 0
    while queue:
        row, col = queue.pop()
        for dr, dc in diff:
            nr, nc = row + dr, col + dc
            if is_inside(nr, nc) and Maze[nr][nc] != '#' and dist[nr][nc] == INF:
                queue.appendleft((nr, nc))
                dist[nr][nc] = dist[row][col] + 1

    start_row, start_col = None, None
    maximum = 0
    for row in range(H):
        for col in range(W):
            if Maze[row][col] != '#' and dist[row][col] > maximum:
                maximum = dist[row][col]
                start_row, start_col = row, col

    queue = deque([(start_row, start_col)])
    dist = [[INF for _ in range(W)] for _ in range(H)]
    dist[start_row][start_col] = 0
    while queue:
        row, col = queue.pop()
        for dr, dc in diff:
            nr, nc = row + dr, col + dc
            if is_inside(nr, nc) and Maze[nr][nc] != '#' and dist[nr][nc] == INF:
                queue.appendleft((nr, nc))
                dist[nr][nc] = dist[row][col] + 1

    ans = 0
    for row in range(H):
        for col in range(W):
            if Maze[row][col] != '#' and dist[row][col] > ans:
                ans = dist[row][col]
    return ans

print(c_welcome_to_atcoder())

Submission status: Runtime Error

Solution code:
from collections import deque

H, W = map(int, input().split())
S = [input() for _ in range(H)]
def bfs(s, t):
    dist = [[None] * W for _ in range(H)]
    que = deque([(s, t)])
    dist[s][t] = 0
    while que:
        u, v = que.popleft()
        d = dist[u][v]
        G = []
        if u > 0 and S[u-1][v] == ".":
            G.append((u-1, v))
        if u < H - 1 and S[u+1][v] == ".":
            G.append((u+1, v))
        if v > 0 and S[u][v-1] == ".":
            G.append((u, v-1))
        if v < W - 1 and S[u][v+1] == ".":
            G.append((u, v+1))
        for w, x in G:
            if dist[w][x] is not None:
                continue
            dist[w][x] = d + 1
            que.append((w, x))
    return max(max((d, (i, j)) for j, d in enumerate(di) if d is not None) for i, di in enumerate(dist))

for i in range(H):
    j = S[i].find(".")
    if j >= 0:
        break


_, (u, v) = bfs(i, j)
d, _ = bfs(u, v)
print(d)

Submission status: Runtime Error

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

def dfs(maze,visited, sh, sw):
    a = 0
    stack = deque([[sh, sw]])
    visited[sh][sw] = 0
    while stack:
        h, w = stack.popleft()
        print(visited)
        for j, k in ([1, 0], [-1, 0], [0, 1], [0, -1]): #4近傍について探索
            new_h, new_w = h+j, w+k
            if new_h < 0 or new_h >= H or \
               new_w < 0 or new_w >= W: #リスト外でないならelif条件へ
                continue
            elif maze[new_h][new_w] != "#" and \
                visited[new_h][new_w] == 0: #行ったことがないかつ、壁でないなら
                visited[new_h][new_w] = visited[h][w]+1
                stack.append([new_h, new_w])
    a = 0
    for s in range(H):
        for x in range(W):
            a = max(a,visited[s][x])
    return a

ans = 0

for i in range(H):
    for q in range(W):
        visited = [[0]*W for _ in range(H)]
        if lst1[i][q] == ".":
            ans = max(ans,dfs(lst1,visited,i,q))
print(ans)

Submission status: Runtime Error

Solution code:
from collections import deque

H, W = map(int, input().split())
S = [input() for _ in range(H)]
for i in range(H):
    j = S[i].find(".")
    if j >= 0:
        break
def bfs(s, t):
    dist = [[None] * W for _ in range(H)]
    que = deque([(s, t)])
    dist[s][t] = 0
    while que:
        u, v = que.popleft()
        d = dist[u][v]
        G = []
        if u > 0 and S[u-1][v] == ".":
            G.append((u-1, v))
        if u < H - 1 and S[u+1][v] == ".":
            G.append((u+1, v))
        if v > 0 and S[u][v-1] == ".":
            G.append((u, v-1))
        if v < W - 1 and S[u][v+1] == ".":
            G.append((u, v+1))
        for w, x in G:
            if dist[w][x] is not None:
                continue
            dist[w][x] = d + 1
            que.append((w, x))
    return max(max((d, (i, j)) for j, d in enumerate(di) if d is not None) for i, di in enumerate(dist))
_, (u, v) = bfs(i, j)
d, _ = bfs(u, v)
print(d)

Submission status: Runtime Error

Solution code:
from collections import deque
n,m = map(int,input().split())
l = []
for i in range(n):
    a = list(input())
    l.append(a)
print(l[0][1])
ans = 0
for i in range(n):
    for j in range(m):
        if l[i][j] == ".":
            bfs = [[-1]*m for k in range(n)]
            bfs[i][j] = 0
            q = deque([])
            q.append((i,j))
            dx = [0,0,-1,1]
            dy = [-1,1,0,0]
            while q:
                x,y = q.popleft()

                for t in range(4):
                    nx = x+dx[t]
                    ny = y + dy[t]

                    if 0 <= nx < n and 0 <= ny < m and bfs[nx][ny] == -1 and l[nx][ny] != "#":
                        q.append((nx,ny))
                        bfs[nx][ny] = bfs[x][y] +1

            for bf in bfs:
                ans = max(ans,max(bf))
            
print(ans)

Submission status: Runtime Error

Solution code:
h,w = map(int,input().split())
sl = []
for _ in range(h):
   s = list(input())
   sl.append(s)

G = [[] for i in range(h*w)]
for i in range(h):
    for j in range(w):
        if sl[i][j] == "#":
            continue
        a = i*h + j
        if j != w - 1:
            if sl[i][j+1] != "#":
                b = i*h + j + 1
                G[a].append(b)
        if j != 0:
            if sl[i][j-1] != "#":
                b = i*h + j - 1
                G[a].append(b)
        if i != 0:
            if sl[i-1][j] != "#":
                b = i*h + j - h
                G[a].append(b)
        if i != h - 1:
            if sl[i+1][j] != "#":
                b = i*h + j + h
                G[a].append(b)

# G[v]: 頂点vに隣接する頂点list
# N: 頂点数

ans = 0
from collections import deque
for i in range(h*w):
    if sl[i//w][i%w] == "#":
        continue
    dist = [-1]*(w*h)
    que = deque([i])
    dist[i] = 0
    while que:
        v = que.popleft()
        d = dist[v]
        for p in G[v]:
            if dist[p] > -1:
                continue
            dist[p] = d + 1
            que.append(p)
    ans = max(ans, max(dist))
    
print(ans)

Submission status: Runtime Error

Solution code:
H, W = list(map(int, input().split()))
S = [list(input()) for _ in range(H)]
import queue
import copy
class gridMan:
  def __init__(s, L):
    s.L = L
  def makeWall(s, x):
    w = len(s.L[0]) + 2
    s.L = [[x] * w] + [[x] + i + [x] for i in s.L] + [[x] * w]
  def dist(s, S, G):
    T = copy.deepcopy(s.L)

    T[S[0] + 1][S[1] + 1] = 1
    q = queue.Queue()
    q.put([S[0] + 1, S[1] + 1])
    while not q.empty():
      h, w = q.get()
      k = T[h][w]
      for i, j in [[1, 0], [-1, 0], [0, 1], [0, -1]]:
        hh = h + i
        ww = w + j
        if T[hh][ww] == -1:
          continue
        t = T[hh][ww]
        if t == 0:
          q.put([hh, ww])
          T[hh][ww] = k + 1
    return T[G[0] + 1][G[1] + 1] - 1

for i in range(H):
  for j in range(W):
    if S[i][j] == "#":
      S[i][j] = -1
    else:
      S[i][j] = 0

g = gridMan(S)
g.makeWall(-1)
ans = 0
for sx in range(H):
  for sy in range(W):
    if S[sx][sy] == -1:
      continue
    for gx in range(sx, H):
      for gy in range(W):
        if S[sx][sy] == -1:
          continue
        if sx == gx and sy == gy: continue
        ans = max(ans, g.dist([sx, sy], [gx, gy]))

g.dist([0, 0], [2, 2])
print(ans)

Submission status: Runtime Error

Solution code:
from collections import deque

def bfs(grid, start, wall, h, w):
    visited = [[False for _ in range(w+2)] for _ in range(h+2)]
    visited[start[0]][start[1]] = True
    queue = deque([])
    queue.append([start[0], start[1], 0])
    dxy = [(1,0), (-1,0), (0,1), (0,-1)]
    cost = 0
    while queue:
        y,x,cnt = queue.popleft()
        for dy, dx in dxy:
            y_,x_ = y+dy,x+dx
            if grid[y_][x_] == wall or visited[y_][x_]:
                continue
            queue.append([y_,x_,cnt+1])
            if cnt+1 > cost:
                cost = cnt + 1
            visited[y_][x_] = True
    return cost

h,w = map(int, input().split())
wall = '#'
edges = [wall] * (w+2)
grid = [edges]
for i in range(h):
    s = input()
    grid.append([wall] + list(s) + [wall])
grid.append(edges)


cand = {2:[], 3:[]}
dxy = [(1,0), (-1,0), (0,1), (0,-1)]
for y in range(1,h+1):
    for x in range(1,w+1):
        wl = 0
        for dy, dx in dxy:
            y_,x_ = y+dy,x+dx
            if grid[y_][x_] == wall:
                wl += 1
        if wl > 1:
            cand[wl].append((y,x))

ans = 0
starts = cand[3] if cand[3] != [] else cand[2]
for start in starts:
    cnt = bfs(grid, start, wall, h, w)
    if cnt > ans:
        ans = cnt
print(ans)

Submission status: Runtime Error

Solution code:
import sys
def input():
    return sys.stdin.readline()[:-1]
h,w=map(int,input().split())
s=[input() for i in range(h)]
# print(40**3)

def warshall_floyd(d):
    #d[i][j]: iからjへの最短距離
    for k in range(n):
        for i in range(n):
            for j in range(n):
                d[i][j] = min(d[i][j],d[i][k] + d[k][j])
    return d

# n,w = map(int,input().split()) #n:頂点数　w:辺の数
n=h*w
d = [[float("inf") for i in range(h*w)] for i in range(h*w)]
# print(d)
#d[u][v] : 辺uvのコスト(存在しないときはinf)
for i in range(h):
    for j in range(w):
        if i+1<h:
            if s[i][j]!="#" and s[i+1][j]!="#":
                d[i*h+j][(i+1)*h+j]=1
                d[(i+1)*h+j][i*h+j]=1
        if j+1<w:
            if s[i][j]!="#" and s[i][j+1]!="#":
                d[i*h+j][i*h+j+1]=1
                d[i*h+j+1][i*h+j]=1
# for i in range(w):
#     x,y,z = map(int,input().split())
#     d[x][y] = 1
#     d[y][x] = 1
for i in range(h*w):
    d[i][i] = 0 #自身のところに行くコストは０
li=warshall_floyd(d)
ans=0
for i in range(len(li)):
    for j in range(len(li[i])):
        if li[i][j]!=float("inf"):
            ans=max(ans,li[i][j])

print(ans)

Submission status: Runtime Error

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


def f(i, j):
    t = [[-1] * W for _ in range(W)]
    t[i][j] = 0
    q = [(i, j)]
    while q:
        y, x = q.pop(0)
        if y - 1 >= 0 and S[y - 1][x] != '#' and t[y - 1][x] == -1:
            t[y - 1][x] = t[y][x] + 1
            q.append((y - 1, x))
        if y + 1 < H and S[y + 1][x] != '#' and t[y + 1][x] == -1:
            t[y + 1][x] = t[y][x] + 1
            q.append((y + 1, x))
        if x - 1 >= 0 and S[y][x - 1] != '#' and t[y][x - 1] == -1:
            t[y][x - 1] = t[y][x] + 1
            q.append((y, x - 1))
        if x + 1 < W and S[y][x + 1] != '#' and t[y][x + 1] == -1:
            t[y][x + 1] = t[y][x] + 1
            q.append((y, x + 1))
    return max(max(tt) for tt in t)


result = 0
for i in range(H):
    for j in range(W):
        if S[i][j] != '#':
            result = max(result, f(i, j))
print(result)

Submission status: Runtime Error

Solution code:
import sys
def input():
    return sys.stdin.readline()[:-1]
h,w=map(int,input().split())
s=[input() for i in range(h)]
print(40**3)

def warshall_floyd(d):
    #d[i][j]: iからjへの最短距離
    for k in range(n):
        for i in range(n):
            for j in range(n):
                d[i][j] = min(d[i][j],d[i][k] + d[k][j])
    return d

# n,w = map(int,input().split()) #n:頂点数　w:辺の数
n=h*w
d = [[float("inf") for i in range(h*w)] for i in range(h*w)]
# print(d)
#d[u][v] : 辺uvのコスト(存在しないときはinf)
for i in range(h):
    for j in range(w):
        if i+1<h:
            if s[i][j]!="#" and s[i+1][j]!="#":
                d[i*h+j][(i+1)*h+j]=1
                d[(i+1)*h+j][i*h+j]=1
        if j+1<w:
            if s[i][j]!="#" and s[i][j+1]!="#":
                d[i*h+j][i*h+j+1]=1
                d[i*h+j+1][i*h+j]=1
# for i in range(w):
#     x,y,z = map(int,input().split())
#     d[x][y] = 1
#     d[y][x] = 1
for i in range(h*w):
    d[i][i] = 0 #自身のところに行くコストは０
li=warshall_floyd(d)
ans=0
for i in range(len(li)):
    for j in range(len(li[i])):
        if li[i][j]!=float("inf"):
            ans=max(ans,li[i][j])

print(ans)

Submission status: Runtime Error

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


def f(i, j):
    t = [[-1] * W for _ in range(W)]
    t[i][j] = 0
    q = [(i, j)]
    while q:
        y, x = q.pop(0)
        if y - 1 >= 0 and S[y - 1][x] != '#' and t[y - 1][x] == -1:
            t[y - 1][x] = t[y][x] + 1
            q.append((y - 1, x))
        if y + 1 < H and S[y + 1][x] != '#' and t[y + 1][x] == -1:
            t[y + 1][x] = t[y][x] + 1
            q.append((y + 1, x))
        if x - 1 >= 0 and S[y][x - 1] != '#' and t[y][x - 1] == -1:
            t[y][x - 1] = t[y][x] + 1
            q.append((y, x - 1))
        if x + 1 < W and S[y][x + 1] != '#' and t[y][x + 1] == -1:
            t[y][x + 1] = t[y][x] + 1
            q.append((y, x - 1))
    return max(max(tt) for tt in t)

result = 0
for i in range(H):
    for j in range(W):
        if S[i][j] != '#':
            result = max(result, f(i, j))
print(result)

Submission status: Runtime Error

Solution code:
h,w = map(int,input().split())
sl = []
for _ in range(h):
   s = list(input())
   sl.append(s)

G = [[] for i in range(h*w)]
for i in range(h):
    for j in range(w):
        if sl[i][j] == "#":
            continue
        a = i*h + j
        if j != w - 1:
            if sl[i][j+1] != "#":
                b = i*h + j + 1
                G[a].append(b)
        if j != 0:
            if sl[i][j-1] != "#":
                b = i*h + j - 1
                G[a].append(b)
        if i != 0:
            if sl[i-1][j] != "#":
                b = i*h + j - h
                G[a].append(b)
        if i != h - 1:
            if sl[i+1][j] != "#":
                b = i*h + j + h
                G[a].append(b)

# G[v]: 頂点vに隣接する頂点list
# N: 頂点数

ans = 0
from collections import deque
for i in range(h*w):
    dist = [-1]*(w*h)
    que = deque([i])
    dist[i] = 0
    while que:
        v = que.popleft()
        d = dist[v]
        for p in G[v]:
            if dist[p] > -1:
                continue
            dist[p] = d + 1
            que.append(p)
    ans = max(ans, max(dist))
    


print(ans)

Submission status: Runtime Error

Solution code:
#include<bits/stdc++.h>
using namespace std;
using ll = long long;
using PP = pair<pair<ll,ll>,ll>;

int main(){
        ll h, w; scanf("%lld %lld", &h, &w);
        char fig[h+2][w+2];
        for(ll i=0; i<h+2; i++){
                fig[i][0] = '#';
                fig[i][w+1] = '#';
        }
        for(ll i=0; i<w+2; i++){
                fig[0][i] = '#';
                fig[h+1][i] = '#';
        }


        for(ll i=0; i<h; i++){
                string s; cin >> s;
                for(ll j=0; j<w; j++) fig[i+1][j+1] = s[j];
        }


        ll ans = 0;
        ll dx[] = {0, 1, 0, -1};
        ll dy[] = {1, 0, -1, 0};
        for(ll i=1; i<=h; i++){
                for(ll j=1; j<=w; j++){
                        ll used[h+2][w+2];
                        for(ll i=0; i<h+2; i++)for(ll j=0; j<w+2; j++)used[i][j]=0;

                        queue<PP> que;
                        que.push({{i, j}, 0});
                        while(!que.empty()){
                                PP pp = que.front(); que.pop();
                                ll I = pp.first.first, J = pp.first.second, dist = pp.second;
                                used[I][J] = 1;
                                ans = max(ans, dist);
                                for(ll d=0; d<4; d++){
                                        ll ti = I + dx[d], tj = J + dy[d];
                                        if(fig[ti][tj]!='#' and !used[ti][tj]){
                                                que.push({{ti, tj}, dist+1});
                                        }
                                }
                        }
                }
        }

        printf("%lld\n", ans);
        return 0;
}

Submission status: Runtime Error

Solution code:
# coding=utf-8
get = lambda: int(raw_input().strip())
gets = lambda: map(int, raw_input().strip().split())
getss = lambda x: [gets() for _ in xrange(x)]
getl = lambda x: [gets() for _ in xrange(x)]

m, n = gets()
A0 = [list(raw_input().strip()) for _ in xrange(n)]

for i in xrange(m):
    for j in xrange(n):
        if A0[i][j] == '.':
            A0[i][j] = 0
        else:
            A0[i][j] = -1


def test(i, j):
    # print '\ntest ', i, j
    A = [row[:] for row in A0]
    bfs = [[i, j]]
    A[i][j] = 1
    dep = 0
    while bfs:
        dep += 1
        bfs2 = []
        for i, j in bfs:
            for di, dj in [[0, -1], [0, 1], [-1, 0], [1, 0]]:
                i2, j2 = i + di, j + dj
                if 0 <= i2 < m and 0 <= j2 < n and A[i2][j2] == 0:
                    A[i2][j2] = dep + 1
                    bfs2.append([i2, j2])
        bfs = bfs2
    # print A
    return dep


res = 0
for i in xrange(m):
    for j in xrange(n):
        if A0[i][j] < 0: continue
        res = max(res, test(i, j) - 1)
print res

Submission status: Runtime Error

Solution code:
h,w=map(int,input().split())
l=[]
for i in range(h):
    l.append(list(input()))
for i in range(h):
    for j in range(w):
        if l[i][j]=='.':
            s=[i,j]
v=[[0 for i in range(w)] for j in range(h)]       
v[s[0]][s[1]]=1
mc=0
ms=s
q=[s,0]
while q:
    p=q.pop(0)
    x=p[0][0]
    y=p[0][1]
    c=p[1]
    k=[[0,1],[0,-1],[1,0],[-1,0]]
    for j in k:
        nx=x+j[0]
        ny=y+j[1]
        if 0<=nx<h and 0<=ny<w and l[nx][ny]=='.' and v[nx][ny]==0:
            v[nx][ny]=1
            q.append([nx,ny],c+1)
            if mc<c+1:
                mc=c+1
                ms=[nx,ny]
v=[[0 for i in range(w)] for j in range(h)]       
v[ms[0]][ms[1]]=1
mc=0
q=[ms,0]
while q:
    p=q.pop(0)
    x=p[0][0]
    y=p[0][1]
    c=p[1]
    k=[[0,1],[0,-1],[1,0],[-1,0]]
    for j in k:
        nx=x+j[0]
        ny=y+j[1]
        if 0<=nx<h and 0<=ny<w and l[nx][ny]=='.' and v[nx][ny]==0:
            v[nx][ny]=1
            q.append([nx,ny],c+1)
            if mc<c+1:
                mc=c+1
                ms=[nx,ny]
print(mc)

Submission status: Runtime Error

Solution code:
h,w = map(int,input().split())
s = [input() for _ in range(h)]

ans = 0

for i in range(h):
    for j in range(w):
        if s[i][j] == '.':
            dis = [[-1] * w for _ in range(h)]
            dis[i][j] = 0
            q = [(i,j)]
            while q:
                l0,l1 = q.pop(0)
                if l0 > 0:
                    if s[l0-1][l1] == '.':
                        if dis[l0-1][l1] > dis[l0][l1] + 1 or dis[l0-1][l1] == -1:
                            dis[l0-1][l1] = dis[l0][l1] + 1
                            q.append((l0-1,l1))
                if l0 < h - 1:
                    if s[l0+1][l1] == '.':
                        if dis[l0+1][l1] > dis[l0][l1] + 1 or dis[l0+1][l1] == -1:
                            dis[l0+1][l1] = dis[l0][l1] + 1
                            q.append((l0+1,l1))
                if l1 > 0:
                    if s[l0][l1-1] == '.':
                        if dis[l0][l1-1] > dis[l0][l1] + 1 or dis[l0][l1-1] == -1:
                            dis[l0][l1-1] = dis[l0][l1] + 1
                            q.append((l0,l1-1))
                if l1 < w - 1:
                    if s[l0][l1+1] == '.':
                        if dis[l0][l1+1] > dis[l0][l1] + 1 or dis[l0][l1+1] == -1:
                            dis[l0][l1+1] = dis[l0][l1] + 1
                            q.append((l0,l1+1))
        dis_max = 0
        for k in dis:
            dis_max = max(max(k), dis_max)
        ans = max(dis_max,ans)

print(ans)

Submission status: Runtime Error

Solution code:
h,w = map(int,input().split())
S = [input() for i in range(h)]

import numpy as np
from scipy.sparse.csgraph import floyd_warshall

m = h*w

N = [[float("inf")]* m for i in range(m)]

for i in range(m):
    N[i][i] = 0

for i in range(h):
    for j in range(w):
        t=h*i+j
        if S[i][j] != "#":
            for dt in [[-1,0],[1,0],[0,1],[0,-1]]:
                di = dt[0]
                dj=dt[1]
                ni = i+di
                nj = j+dj
                if 0 <= ni < h and 0 <= nj < w and S[ni][nj] == ".":
                    N[t][h*ni+nj] = 1

# print(N)

# for k in range(m):
#     for i in range(m):
#         for j in range(m):
#             N[i][j] = min(N[i][j], N[i][k]+N[k][j])

N = floyd_warshall(N)


for i in range(m):
    for j in range(m):
        if N[i][j] == float("inf"):
            N[i][j] = -1
ans = 0
# print(N)
for i in range(m):
    ans = max(ans, max(N[i]))
print(int(ans))

Submission status: Runtime Error

Solution code:
h,w = map(int,input().split())
S = [input() for i in range(h)]

m = h*w

N = [[float("inf")]* m for i in range(m)]

for i in range(m):
    N[i][i] = 0

for i in range(h):
    for j in range(w):
        t=h*i+j
        if S[i][j] != "#":
            for dt in [[-1,0],[1,0],[0,1],[0,-1]]:
                di = dt[0]
                dj=dt[1]
                ni = i+di
                nj = j+dj
                if 0 <= ni < h and 0 <= nj < w and S[ni][nj] == ".":
                    N[t][h*ni+nj] = 1

# print(N)

for k in range(m):
    for i in range(m):
        for j in range(m):
            N[i][j] = min(N[i][j], N[i][k]+N[k][j])


for i in range(m):
    for j in range(m):
        if N[i][j] == float("inf"):
            N[i][j] = -1
ans = 0
# print(N)
for i in range(m):
    ans = max(ans, max(N[i]))
print(ans)

Submission status: Runtime Error

Solution code:
import os
import sys

import numpy as np
from scipy.sparse.csgraph._shortest_path import floyd_warshall

if os.getenv("LOCAL"):
    sys.stdin = open("_in.txt", "r")

sys.setrecursionlimit(10 ** 9)
INF = float("inf")
IINF = 10 ** 18
MOD = 10 ** 9 + 7
# MOD = 998244353

H, W = list(map(int, sys.stdin.buffer.readline().split()))
S = [sys.stdin.buffer.readline().decode().rstrip() for _ in range(H)]


def to_i(h, w):
    return h * W + w


N = H * W
graph = [[0] * N for _ in range(N)]
for h in range(H):
    for w in range(W):
        if S[h][w] == '.':
            if 0 <= h - 1 < H and S[h - 1][w] == '.':
                graph[to_i(h - 1, w)][to_i(h, w)] = 1
                graph[to_i(h, w)][to_i(h - 1, w)] = 1
            if 0 <= h + 1 < H and S[h + 1][w] == '.':
                graph[to_i(h + 1, w)][to_i(h, w)] = 1
                graph[to_i(h, w)][to_i(h + 1, w)] = 1
            if 0 <= w - 1 < W and S[h][w - 1] == '.':
                graph[to_i(h, w - 1)][to_i(h, w)] = 1
                graph[to_i(h, w)][to_i(h, w - 1)] = 1
            if 0 <= w + 1 < H and S[h][w + 1] == '.':
                graph[to_i(h, w + 1)][to_i(h, w)] = 1
                graph[to_i(h, w)][to_i(h, w + 1)] = 1
dist = floyd_warshall(graph, directed=False)
dist[dist == np.inf] = 0
print(int(dist.max()))