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: Wrong Answer

Solution code:

def s0():return input()
def s1():return input().split()
def s2(n):return [input() for x in range(n)]
def s3(n):return [input().split() for _ in range(n)]
def s4(n):return [[x for x in s] for s in s2(n)]
def n0():return int(input())
def n1():return [int(x) for x in input().split()]
def n2(n):return [int(input()) for _ in range(n)]
def n3(n):return [[int(x) for x in input().split()] for _ in range(n)]
def t3(n):return [tuple(int(x) for x in input().split()) for _ in range(n)]
def p0(b,yes="Yes",no="No"): print(yes if b else no)
# from sys import setrecursionlimit
# setrecursionlimit(1000000)
from collections import Counter,deque,defaultdict
import itertools
# import math
# import networkx as nx
# from bisect import bisect_left,bisect_right
# from heapq import heapify,heappush,heappop

h,w=n1()
s=s4(h)
def make_around(s,h):
    sss=[[-1]*(w+2)]
    for line in s:
        sss.append([-1]+[-1 if i=="#" else 0 for i in line ]+[-1])
    sss.append([-1]*(w+2))
    return sss

s=make_around(s,h)
d=[]
for i in range(1,h+1):
    for j in range(1,w+1):
        if s[i][j]==0:
            d.append((i,j))

from copy import deepcopy
q=deque()
ans=0
for i in range(len(d)):
    ss=deepcopy(s)
    q.append(d[i])
    while len(q)>0:
        x,y=q.popleft()
        if ss[x+1][y]==0:
            ss[x+1][y]=ss[x][y]+1
            q.append((x+1,y))
        if ss[x-1][y]==0:
            ss[x-1][y]=ss[x][y]+1
            q.append((x-1,y))
        if ss[x][y+1]==0:
            ss[x][y+1]=ss[x][y]+1
            q.append((x,y+1))
        if ss[x][y-1]==0:
            ss[x][y-1]=ss[x][y]+1
            q.append((x,y-1))
    ans=max(ans,max(list(itertools.chain.from_iterable(ss))))
print(ans)

Submission status: Wrong Answer

Solution code:
#bfs(両端キュー)
from collections import deque
import copy

h,w=map(int, input().split())
d=[list(input()) for i in range(h)]
inf=10**7

def bfs(sx,sy):
    a=copy.deepcopy(d)
    for i in range(h):
        for j in range(w):
            if a[i][j]=='.':
                a[i][j]=inf
    que=deque([[sy,sx]])
    a[sy][sx]=0

    while que:
        y,x=que.popleft()
        for i,j in [(1,0),(0,1),(-1,0),(0,-1)]:
            ny,nx=y+i,x+j
            if nx>=0 and nx<w and ny>=0 and ny<h:
                dist=a[ny][nx]
                if dist!='#':
                    if dist>a[y][x]+1:
                        a[ny][nx]=a[y][x]+1
                        que.append([ny,nx])

    m=0
    for i in range(h):
        for j in range(w):
            if a[i][j]!='#':
                if a[i][j]>m:
                    m=a[i][j]
    return m
    
# BFSのstartを決める
ans=0
for i in range(h):
    for j in range(w):
        if ans<bfs(j,i):
            ans=bfs(j,i)
print(ans)

Submission status: Wrong Answer

Solution code:
# ＢＦＳ
# グリッド
# スタート位置 と ゴール位置 の全パターンの経路を探索 → 最長経路
 # ※ 要削除

from collections import deque

h,w = map(int,input().split())
pic = [list(input()) for i in range(h)]
visited = [[0 for i in range(w)] for i in range(h)] # 各位置までに必要な移動回数記録用メモ
queue = deque([]) # 探索候補のキューを dequeオブジェクトとして初期化
print(h,w) # ※
print(pic) # ※
print(visited) # ※

ans = 0

for i in range(h):
    for j in range(w):
        if pic[i][j] == '.': # スタート位置を決めて...
            visited[i][j] = 1 # 訪問 '1' を記録して..._後で '1' 引く
            queue.append([i,j]) # 探索候補に積む

            dy_dx = [[1,0],[0,1],[-1,0],[0,-1]] # 探索子、4方向

            while queue:
                now = queue.popleft() # 現在地を探索候補から popleft する
                for k in range(4): # 現在地から4方向の探索先を生成
                    y = now[0]+dy_dx[k][0] # 探索子を使う
                    x = now[1]+dy_dx[k][1] # 〃
                    if 0 <= y < h and 0 <= x < w: # 探索の条件4つを2行で確認
                        if pic[y][x] != '#' and visited[y][x] == 0:
                            visited[y][x] = visited[now[0]][now[1]] + 1 # 移動回数のカウント_ひとつ前の回数に'1'を足す
                            queue.append([y,x]) # 現在地を探索候補に積む
        print(visited) # ※ 
        for l in range(h): # 一探索ごとに'max'を比較 → 記録する
            for m in range(w):
                ans = max(ans,visited[l][m])

        queue = deque([]) # 探索候補のキューを初期化
        visited = [[0 for i in range(w)] for i in range(h)] # 移動回数記録用メモを初期化

print(ans-1)

Submission status: Wrong Answer

Solution code:
from collections import deque

def bfs(maze, sy, sx, gy, gx, H, W):
    """
    maze>>迷路
    visited>>探索済み
    sy, sx>>スタート地点
    gy, gx>>ゴール地点
    """
    queue = deque([[sy, sx]])
    visited = [[-1]*W for _ in range(H)]
    visited[sy][sx] = 0
    while queue:
        y, x = queue.popleft()
        if [y, x] == [gy, gx]:
            return visited[y][x]
        for j, k in ([1, 0], [-1, 0], [0, 1], [0, -1]):
            new_y, new_x = y+j, x+k

            if new_y < 0 or H -1 < new_y or new_x < 0 or W -1 < new_x:
                continue
            elif maze[new_y][new_x] != '#' and visited[new_y][new_x] == -1:
                visited[new_y][new_x] = visited[y][x] + 1
                queue.append([new_y, new_x])


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

answer = 0
for sy in range(H):
    for sx in range(W):
        for gy in range(H):
            for gx in range(W):
                if maze[sy][sx] == '#':
                    break
                if maze[gy][gx] == '#':
                    break
                answer = max(answer, bfs(maze, sy, sx, gy, gx, H, W))

print(answer)

Submission status: Wrong Answer

Solution code:
from collections import deque

def bfs(maze, sy, sx, gy, gx, H, W):
    """
    maze>>迷路
    visited>>探索済み
    sy, sx>>スタート地点
    gy, gx>>ゴール地点
    """
    queue = deque([[sy, sx]])
    visited = [[-1]*W for _ in range(H)]
    visited[sy][sx] = 0
    while queue:
        y, x = queue.popleft()
        if [y, x] == [gy, gx]:
            return visited[y][x]
        for j, k in ([1, 0], [-1, 0], [0, 1], [0, -1]):
            new_y, new_x = y+j, x+k

            if new_y < 0 or H -1 < new_y or new_x < 0 or W -1 < new_x:
                continue
            elif maze[new_y][new_x] != '#' and visited[new_y][new_x] == -1:
                visited[new_y][new_x] = visited[y][x] + 1
                queue.append([new_y, new_x])


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

answer = 0
for sy in range(H):
    for sx in range(W):
        if maze[sy][sx] == '#':
            break
        for gy in range(H):
            for gx in range(W):
                if maze[gy][gx] == '#':
                    break
                answer = max(answer, bfs(maze, sy, sx, gy, gx, H, W))

print(answer)

Submission status: Wrong Answer

Solution code:
from collections import deque
H,W = map(int,input().split())
maze= []
for i in range(H):
    maze.append(input())
nl = [(1,0), (0,1), (-1,0),(0,-1)]
ans = 0

def bfs(sy,sx):
    dist = [[-1]*W for _ in range(H)]
    q = deque([])
    q.append((sy,sx))
    dist[sy][sx] = 0
    maxd = 0
    while len(q)>0:
        cy, cx = q.popleft()
        for ny, nx in nl:
            ny += cy
            nx += cx
            if (0 <= ny < H) and (0 <= nx < W):
                if dist[ny][nx] == -1 and maze[ny][nx] == ".":
                    q.append((ny,nx))
                    dist[ny][nx] = dist[cy][cx] +1
                    if maxd < dist[ny][nx]:
                        maxd = dist[ny][nx]
    return maxd

for i in range(H):
    for j in range(W):
        maxn = bfs(i,j)
        if maxn > ans:
            ans = maxn
print(ans)

Submission status: Wrong Answer

Solution code:
from collections import deque
import copy
H, W = map(int, input().split())
field = []
for i in range(H):
    field.append(list(input()))
f = copy.deepcopy(field)

def BFS(x, y, field):
    cnt = [[0 for _ in range(W)] for _ in range(H)]
    queue = deque()
    queue.append((x, y))
    while queue:
        x, y = queue.popleft()
        if(field[x][y] == "#"):
            continue        
            
        field[x][y] = "#"
        for dx, dy in ((-1, 0), (1, 0), (0, -1), (0, 1)):
            nx = x + dx
            ny = y + dy
            if(nx < 0 or nx >= H or ny < 0 or ny >= W):
                continue
            queue.append((nx, ny))
            if(field[nx][ny] != "#"):
                cnt[nx][ny] = cnt[x][y] + 1
    return max(max(cnt))

ans = 0
for i in range(H):
    for j in range(W):
        if(field[i][j] == "#"):
            continue
        ans = max(ans, BFS(i, j, f))
        f = copy.deepcopy(field)
print(ans)

Submission status: Wrong Answer

Solution code:
import numpy as np
H,W = map(int, input().split())
S = []
for i in range(H):
  tmp = input()
  S.append(tmp)

S_label = np.zeros([H,W], dtype=int)
label = 1
for h in range(H):
  for w in range(W):
    S_label[h][w] = label
    label+=1
root = []
for h in range(H):
  for w in range(W):
    if S[h][w] == '.':
      tmp_h, tmp_w = h, w+1	# 右
      #print(tmp_h, tmp_w)
      if tmp_w < W and S[tmp_h][tmp_w] == '.':
        root.append([S_label[h][w],S_label[tmp_h][tmp_w]])
      tmp_h, tmp_w = h+1, w	# 下
      if tmp_h < H and S[tmp_h][tmp_w] == '.':
        root.append([S_label[h][w],S_label[tmp_h][tmp_w]])
#print(root)

Q = [1]
visited = np.zeros(H*W, dtype=int)
visited[0] = 1
depth = 2
while len(Q) != 0:
  q = Q.pop(0)
  for i in range(len(root)):
    if q == root[i][0] and visited[root[i][1]-1] == 0:
      depth = visited[root[i][0]-1]+1
      visited[root[i][1]-1] = depth
      Q.append(root[i][1])
    elif q == root[i][1] and visited[root[i][0]-1] == 0:
      depth = visited[root[i][1]-1]+1
      visited[root[i][0]-1] = depth
      Q.append(root[i][0])
  depth += 1
print(max(visited)-1)

Submission status: Wrong Answer

Solution code:
from collections import deque

h, w = map(int, input().split())
s = [str(input()) for i in range(h)]
ans = 0
for sy in range(h):
    for sx in range(w):
        for gy in range(h):
            for gx in range(w):
                reached = [[False for i in range(w)] for j in range(h)]
                dist = 0
                d = deque([])
                d.append([sx, sy, dist])
                while(d):
                    x, y, dist = d.popleft()
                    if s[y][x] == '#':
                        continue
                    if reached[y][x]:
                        continue
                    if x == gx and y == gy:
                        break 
                    reached[y][x] = True
                    dist += 1
                    if x+1 < w and not reached[y][x+1]:
                        d.append([x+1, y, dist])
                    if x-1 >= 0 and not reached[y][x-1]:
                        d.append([x-1, y, dist])
                    if y+1 < h and not reached[y+1][x]:
                        d.append([x, y+1, dist])
                    if y-1 >= 0 and not reached[y-1][x]: 
                        d.append([x, y-1, dist])
                ans = max(ans, dist)


print(ans)

Submission status: Wrong Answer

Solution code:
from collections import deque

h, w = map(int, input().split())

maze = [input() for i in range(h)]

ans = 0
for j in range(h):
    for i in range(w):
        if maze[j][i] == ".":
            cnt = [[0] * w for _ in range(h)] 
            q = deque([(j,i)])
            while q:
                y,x = q.popleft()
                for by,bx in ((0,1),(0,-1),(1,0),(-1,0)):
                    ny = y + by
                    nx = x + bx
                    if 0 <= ny < h and 0 <= nx < w:
                        if maze[ny][nx] == "." and cnt[ny][nx] == 0:
                            q += [(ny,nx)]
                            cnt[ny][nx] += cnt[y][x] + 1
                            ans = max(ans, cnt[ny][nx])


print(ans)

Submission status: Wrong Answer

Solution code:
import collections
h,w = map(int, raw_input().split(' '))
mat = [raw_input() for _ in range(h)]


def dfs(cur, vis, mat):
	i,j = cur

	temp = 0
	for nei in [(i+1, j), (i-1,j), (i,j-1), (i,j+1)]:
		if 0 <= nei[0]< len(mat) and 0<=nei[1]< len(mat[nei[0]]) and nei not in vis and mat[nei[0]][nei[1]] == '.':
			vis.add(nei)
			temp = max(temp, 1 + dfs(nei, vis, mat))
			vis.remove(nei)
	return temp

m = 0
for i in range(len(mat)):
	for j in range(len(mat[0])):
		if mat[i][j] == '.':
			m = max(m, dfs((i,j), set([(i,j)]), mat))
print m

Submission status: Wrong Answer

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

M = []
for i in range(H):
    M.append(list(input()))

from collections import deque
def bfs(maze,r,c,sx,sy,gx,gy):
    d = [[float("inf")] * c for i in range(r)]
    dx = [1, 0, -1, 0]
    dy = [0, 1, 0, -1]
    que = deque([])
    que.append((sy, sx))
    d[sy][sx] = 0
    while que:
        p = que.popleft()
        if p[0] == gy and p[1] == gx:
            break
        for i in range(4):
            nx = p[0] + dx[i]
            ny = p[1] + dy[i]
            if (0<=nx<r)and(0<=ny<c)and(maze[nx][ny]!="#")and(d[nx][ny]==float("inf")):
                que.append((nx, ny))
                d[nx][ny] = d[p[0]][p[1]] + 1
    return d[gy][gx]

out = 0
for sx in range(0,W):
    for gx in range(0,W):
        for sy in range(0,H):
            for gy in range(0,H):
                if (sx==gx)and(sy==gy):
                    continue
                d = bfs(M,H,W,sx,sy,gx,gy)
                if out<d<H*W:
                    out=d
print(out)

Submission status: Wrong Answer

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

from collections import deque

que=deque()
maxx=0

for i in range(H):
    for j in range(W):
        if S[i][j]=="#":
            break
        ans=[[500]*W for _ in range(H)]
        ans[i][j]=0
        que.append((i,j))
        while que:
            h,w=que.popleft()
            if h!=0 and S[h-1][w]==".":
                if ans[h-1][w]==500:
                    ans[h-1][w]=min(ans[h-1][w],ans[h][w]+1)
                    que.append((h-1,w))
            if h!=H-1 and S[h+1][w]==".":
                if ans[h+1][w]==500:
                    ans[h+1][w]=min(ans[h+1][w],ans[h][w]+1)
                    que.append((h+1,w))
            if w!=0 and S[h][w-1]==".":
                if ans[h][w-1]==500:
                    ans[h][w-1]=min(ans[h][w-1],ans[h][w]+1)
                    que.append((h,w-1))
            if w!=W-1 and S[h][w+1]==".":
                if ans[h][w+1]==500:
                    ans[h][w+1]=min(ans[h][w+1],ans[h][w]+1)
                    que.append((h,w+1))
            maxx=max(maxx,ans[h][w])
print(maxx)

Submission status: Wrong Answer

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

from collections import deque

que=deque()
maxx=0

for i in range(H):
    for j in range(W):
        if S[i][j]=="#":
            break
        ans=[[100]*W for _ in range(H)]
        ans[i][j]=0
        que.append((i,j))
        while que:
            h,w=que.popleft()
            if h!=0 and S[h-1][w]==".":
                if ans[h-1][w]==100:
                    ans[h-1][w]=min(ans[h-1][w],ans[h][w]+1)
                    que.append((h-1,w))
            if h!=H-1 and S[h+1][w]==".":
                if ans[h+1][w]==100:
                    ans[h+1][w]=min(ans[h+1][w],ans[h][w]+1)
                    que.append((h+1,w))
            if w!=0 and S[h][w-1]==".":
                if ans[h][w-1]==100:
                    ans[h][w-1]=min(ans[h][w-1],ans[h][w]+1)
                    que.append((h,w-1))
            if w!=W-1 and S[h][w+1]==".":
                if ans[h][w+1]==100:
                    ans[h][w+1]=min(ans[h][w+1],ans[h][w]+1)
                    que.append((h,w+1))
            maxx=max(maxx,ans[h][w])
print(maxx)

Submission status: Wrong Answer

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

from collections import deque

que=deque()
maxx=0

for i in range(H):
    for j in range(W):
        ans=[[100]*W for _ in range(H)]
        ans[i][j]=0
        que.append((i,j))
        while que:
            h,w=que.popleft()
            if h!=0 and S[h-1][w]==".":
                if ans[h-1][w]==100:
                    ans[h-1][w]=min(ans[h-1][w],ans[h][w]+1)
                    que.append((h-1,w))
            if h!=H-1 and S[h+1][w]==".":
                if ans[h+1][w]==100:
                    ans[h+1][w]=min(ans[h+1][w],ans[h][w]+1)
                    que.append((h+1,w))
            if w!=0 and S[h][w-1]==".":
                if ans[h][w-1]==100:
                    ans[h][w-1]=min(ans[h][w-1],ans[h][w]+1)
                    que.append((h,w-1))
            if w!=W-1 and S[h][w+1]==".":
                if ans[h][w+1]==100:
                    ans[h][w+1]=min(ans[h][w+1],ans[h][w]+1)
                    que.append((h,w+1))
            maxx=max(maxx,ans[h][w])
print(maxx)

Submission status: Wrong Answer

Solution code:
import copy
h,w = map(int,input().split())

s = [['#' for _ in range(w+2)]]
for i in range(h):
    si = ['#']
    sij = list(input())
    si.extend(sij)
    si.append('#')
    s.append(si)
s.append(['#' for _ in range(w+2)])
print(s)

def bfs(i,j):
    root = copy.deepcopy(s)
    q = []
    q.append([i,j])
    root[i][j] = 0
    while q:
        ij = q.pop(0)
        i = ij[0]
        j = ij[1]
        
        if root[i-1][j] == '.': #左
            q.append([i-1,j])
            root[i-1][j] = root[i][j] + 1
        if root[i+1][j] == '.': #右
            q.append([i+1,j])
            root[i+1][j] = root[i][j] + 1
        if root[i][j+1] == '.': #上
            q.append([i,j+1])
            root[i][j+1] = root[i][j] + 1
        if root[i][j-1] == '.': #下
            q.append([i,j-1])
            root[i][j-1] = root[i][j] + 1
    print(root)
    max = 0
    for i in range(1,h+1):
        for j in range(1,w+1):
            if root[i][j] != '#' and root[i][j] > max:
                max = root[i][j]
    return max

ans = -1
for i in range(1,h+1):
    for j in range(1,w+1):
        if s[i][j] == '.':
            k = bfs(i,j)
            if ans < k:
                ans = k
print(ans)

Submission status: Wrong Answer

Solution code:
from collections import deque

def bfs(s, visited, sy, sx):

    queue = deque([sy, sx])
    visited[sy][sx] = 0 

    while queue:
        
        y = queue.popleft()
        x = queue.popleft()
        
        for (i, j) in [(1, 0), (-1, 0), (0, 1), (0, -1)]:
            
            new_y = y + i
            new_x = x + j

            if (0 <= new_y < h) and (0 <= new_x < w):

                if s[new_y][new_x] != '#' and visited[new_y][new_x] == -1:
                        
                    visited[new_y][new_x] = visited[y][x] + 1
                    queue.append(new_y)
                    queue.append(new_x)

    return max(visited)


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

position = []
for i in range(h):
    for j in range(w):
        if s[i][j] != '#':
            position.append([i, j])

ans = []
for i in position:
    visited = [[-1]*w for _ in range(h)]
    ans.append(max(bfs(s, visited, i[0], i[1])))

print(max(ans))

Submission status: Wrong Answer

Solution code:
from collections import deque

def bfs(s, visited, sy, sx):

    queue = deque([sy, sx])
    visited[sy][sx] = 0 

    while queue:
        
        y = queue.popleft()
        x = queue.popleft()
        
        for (i, j) in [(1, 0), (-1, 0), (0, 1), (0, -1)]:
            
            new_y = y + i
            new_x = x + j

            if (0 <= new_y < h) and (0 <= new_x < w):

                if s[new_y][new_x] == '.' and visited[new_y][new_x] == -1:
                        
                    visited[new_y][new_x] = visited[y][x] + 1
                    queue.append(new_y)
                    queue.append(new_x)
                    
    return max(visited)


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

position = []
for i in range(h):
    for j in range(w):
        if s[i][j] != '#':
            position.append([i, j])

ans = []
for i in position:
    visited = [[-1]*w for _ in range(h)]
    ans.append(max(bfs(s, visited, i[0], i[1])))

print(max(ans))

Submission status: Wrong Answer

Solution code:
from collections import deque

def bfs(s, visited, sy, sx):

    queue = deque([sy, sx])
    visited[sy][sx] = 0 

    while queue:
        
        y = queue.popleft()
        x = queue.popleft()
        
        for (i, j) in [(1, 0), (-1, 0), (0, 1), (0, -1)]:
            
            new_y = y + i
            new_x = x + j

            if (0 <= new_y < h) and (0 <= new_x < w):

                if s[new_y][new_x] == '.' and visited[new_y][new_x] == -1:
                        
                    visited[new_y][new_x] = visited[y][x] + 1
                    queue.append(new_y)
                    queue.append(new_x)
    return max(visited)


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

position = []
for i in range(h):
    for j in range(w):
        if s[i][j] != '#':
            position.append([i, j])

ans = []
for i in position:
    visited = [[-1]*w for _ in range(h)]
    ans.append(max(bfs(s, visited, i[0], i[1])))

Submission status: Wrong Answer

Solution code:
# D - Maze Master
import copy
from collections import deque


def bfs(s, f):
    f[s[0]][s[1]] = 0
    moves = [[1, 0], [-1, 0], [0, 1], [0, -1]]

    q = deque([s])
    while q:
        c = q.popleft()
        for m in moves:
            my, mx = c[0] + m[0], c[1] + m[1]

            if f[my][mx] == ".":
                f[my][mx] = f[c[0]][c[1]] + 1
                q.append([my, mx])
                goal = [my, mx]
            else:
                continue

    return goal, f[goal[0]][goal[1]]


def main():
    H, W = map(int, input().split())
    field = []

    for h in range(H + 2):
        field.append(["#"] * (W + 2))
        if 1 <= h <= (H):
            field[h][1:-1] = list(map(str, input().rstrip()))

    ans = 0
    for y in range(1, H + 1):
        if "." in field[y]:
            start = [y, field[y].index(".")]
    goal, _ = bfs(start, copy.deepcopy(field))
    _, ans = bfs(goal, field)

    print(ans)


if __name__ == '__main__':
    main()

Submission status: Wrong Answer

Solution code:
import copy
from collections import deque


def bfs(s, f):
    f[s[0]][s[1]] = 0
    moves = [[1, 0], [-1, 0], [0, 1], [0, -1]]

    q = deque([s])
    while q:
        c = q.popleft()
        for m in moves:
            my, mx = c[0] + m[0], c[1] + m[1]

            if f[my][mx] == ".":
                f[my][mx] = f[c[0]][c[1]] + 1
                q.append([my, mx])
                goal = [my, mx]
            else:
                continue

    return goal, f[goal[0]][goal[1]]


def main():
    H, W = map(int, input().split())
    field = []

    for h in range(H + 2):
        field.append(["#"] * (W + 2))
        if 1 <= h <= (H):
            field[h][1:-1] = list(map(str, input().rstrip()))

    ans = 0
    for y in range(1, H + 1):
        if "." in field[y]:
            start = [y, field[y].index(".")]
    goal, _ = bfs(start, copy.deepcopy(field))
    _, ans = bfs(goal, copy.deepcopy(field))

    print(ans)


if __name__ == '__main__':
    main()

Submission status: Wrong Answer

Solution code:
import copy
from collections import deque


def bfs(s, f):
    f[s[0]][s[1]] = 0
    moves = [[1, 0], [-1, 0], [0, 1], [0, -1]]

    q = deque([s])
    while q:
        c = q.popleft()
        for m in moves:
            my, mx = c[0] + m[0], c[1] + m[1]

            if f[my][mx] == ".":
                f[my][mx] = f[c[0]][c[1]] + 1
                q.append([my, mx])
                goal = [my, mx]
            else:
                continue

    return goal, f[goal[0]][goal[1]]


def main():
    H, W = map(int, input().split())
    field = []

    for h in range(H + 2):
        field.append(["#"] * (W + 2))
        if 1 <= h <= (H):
            field[h][1:-1] = list(map(str, input().rstrip()))

    ans = 0
    for y in range(1, H + 1):
        if "." in field[y]:
            start = [y, field[y].index(".")]
    goal, _ = bfs(start, copy.deepcopy(field))
    _, ans = bfs(goal, field)

    print(ans)


if __name__ == '__main__':
    main()

Submission status: Wrong Answer

Solution code:
import copy
from collections import deque


def bfs(s, f):
    f[s[0]][s[1]] = 0
    moves = [[1, 0], [-1, 0], [0, 1], [0, -1]]

    q = deque([s])
    while q:
        c = q.popleft()
        for m in moves:
            my, mx = c[0] + m[0], c[1] + m[1]

            if f[my][mx] == ".":
                f[my][mx] = f[c[0]][c[1]] + 1
                q.append([my, mx])
            else:
                continue

    f = sum(f, [])
    return max([x for x in f if type(x) == int])


def main():
    H, W = map(int, input().split())
    field = []

    for h in range(H + 2):
        field.append(["#"] * (W + 2))
        if 1 <= h <= (H):
            field[h][1:-1] = list(map(str, input().rstrip()))

    ans = 0
    for y in range(1, H + 1):
        if "." in field[y]:
            ans = max(ans, bfs([y, field[y].index(".")], copy.deepcopy(field)))

    print(ans)


if __name__ == '__main__':
    main()

Submission status: Wrong Answer

Solution code:
from collections import deque
from copy import deepcopy
h, w = map(int, input().split())
table = [[] for _ in range(h)]
for i in range(h):
    table[i] = list(input())

steps = [0, 1, 0, -1, 0]
ans = 0
for y in range(h):
    for x in range(w):
        t = deepcopy(table)
        t[y][x] = 0
        queue = deque()
        queue.append((y, x))
        while len(queue) > 0:
            cur = queue.popleft()
            for i in range(4):
                ny, nx = cur[0] + steps[i], cur[1] + steps[i + 1]
                if 0 <= ny and ny < h and 0 <= nx and nx < w and t[ny][
                        nx] == ".":
                    t[ny][nx] = t[cur[0]][cur[1]] + 1
                    queue.append((ny, nx))
        tmp = 0
        for i in range(h):
            for j in range(w):
                if str(t[i][j]).isnumeric():
                    tmp = max(tmp, t[i][j])
        ans = max(ans, tmp)
print(ans)

Submission status: Wrong Answer

Solution code:
def main():
    from copy import deepcopy
    from collections import deque
    h, w = map(int, input().split())
    wall = ["#"] * (w + 2)
    maze = [wall]
    for _ in range(h):
        line = ["#"] + list(input()) + ["#"]
        maze.append(line)
    maze.append(wall)

    ans = 0
    for i in range(1, h + 1):
        for j in range(1, w + 1):
            m = deepcopy(maze)
            que = deque()
            que.append((i, j, 0))
            tmp = 0
            while que:
                (x, y, c) = que.popleft()
                if m[x][y] == "#":
                    continue
                m[x][y] = "#"
                tmp = max(tmp, c)

                for v, w in [(1, 0), (0, 1), (-1, 0), (0, -1)]:
                    que.append((x + v, y + w, c + 1))

            ans = max(ans, tmp)
    print(ans)


if __name__ == '__main__':
    main()

Submission status: Wrong Answer

Solution code:
def main():
    from copy import deepcopy
    from collections import deque
    h, w = map(int, input().split())
    wall = ["#"] * (w + 2)
    maze = [wall]
    for _ in range(h):
        line = ["#"] + list(input()) + ["#"]
        maze.append(line)
    maze.append(wall)

    ans = 0
    for i in range(1, h + 1):
        for j in range(1, w + 1):
            m = deepcopy(maze)
            que = deque()
            que.append((i, j, 0))
            tmp = 0
            while que:
                (x, y, c) = que.popleft()
                if m[x][y] == "#":
                    continue
                if m[x][y] == ".":
                    m[x][y] = "#"
                tmp = max(tmp, c)

                for v, w in [(1, 0), (0, 1), (-1, 0), (0, -1)]:
                    que.appendleft((x + v, y + w, c + 1))

            ans = max(ans, tmp)
    print(ans)


if __name__ == '__main__':
    main()

Submission status: Wrong Answer

Solution code:
def main():
    from copy import deepcopy
    from collections import deque
    h, w = map(int, input().split())
    wall = ["#"] * (w + 2)
    maze = [wall]
    for _ in range(h):
        line = ["#"] + list(input()) + ["#"]
        maze.append(line)
    maze.append(wall)

    ans = 0
    for i in range(1, h + 1):
        for j in range(1, w + 1):
            m = deepcopy(maze)
            que = deque()
            que.append((i, j, 0))
            tmp = 0
            while que:
                (x, y, c) = que.popleft()
                if m[x][y] == "#":
                    continue
                if m[x][y] == ".":
                    m[x][y] = "#"
                tmp = max(tmp, c)

                for v, w in [(1, 0), (0, 1), (-1, 0), (0, -1)]:
                    que.append((x + v, y + w, c + 1))

            ans = max(ans, tmp)
    print(ans)


if __name__ == '__main__':
    main()

Submission status: Wrong Answer

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

graph = [deque([]) for _ in range(H*W)]
for i in range(H):
    for j in range(W):
        if S[i][j] == ".":
            if j+1 < W:
                if S[i][j+1] == ".":
                    graph[i*W+j].append(i*W+j+1)
                    graph[i*W+j+1].append(i*W+j)
            if i+1 < H:
                if S[i+1][j] == ".":
                    graph[i*W+j].append((i+1)*W+j)
                    graph[(i+1)*W+j].append(i*W+j)

def bfs(v, graph):
    d = [-1]*(H*W)
    stack = deque([v])
    d[v] = 0
    while stack:
        v = stack[-1]
        if graph[v]:
            w = graph[v].popleft()
            if d[w] == -1:
                d[w] = d[v]+1
                stack.appendleft(w)
        else:
            stack.pop()
    max_d = [0, 0]
    for i in range(H*W):
        if d[i] > max_d[0]:
            max_d = [d[i], i]
    return max_d

res = bfs(0, copy.deepcopy(graph))
res = bfs(res[1], graph)
print(res[0])