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:
#import sys
MOD = 10 ** 9 + 7
#input = sys.stdin.readline
import math

import itertools

#import bisect

import copy


def sosuhante(n):
    for k in range(2, int(math.sqrt(n))+1):
        if n% k ==0:
            return False
    return True
def cmb(n, r):
    if n - r < r: r = n - r
    if r == 0: return 1
    if r == 1: return n

    numerator = [n - r + k + 1 for k in range(r)]
    denominator = [k + 1 for k in range(r)]

    for p in range(2,r+1):
        pivot = denominator[p - 1]
        if pivot > 1:
            offset = (n - r) % p
            for k in range(p-1,r,p):
                numerator[k - offset] /= pivot
                denominator[k] /= pivot

    result = 1
    for k in range(r):
        if numerator[k] > 1:
            result *= int(numerator[k])

    return result

def kingaku(a,b,n):
    keta=len(str(n))
    return a*n+b*keta

def my_index(l, x, default=False):
    if x in l:
        return l.index(x)
    else:
        return default

#    h,w,a,b = map(int, input().split())
#    c = [[0 for j in range(n)] for i in range(n)]
def main():
    h,w=map(int,input().split())
    s=[input() for i in range(h)]
    for i in range(h):
        s[i]=list(s[i])
    for i in range(h):
        for j in range(w):
            if s[i][j]==".":
                s[i][j]=1000
            else:
                s[i][j]=9999
 #   print(s)
    start=[0,0]
    for i in range(h):
        start[1]=s[i].index(min(s[i]))
        if min(s[i]) >= 1111:
            continue
        else:
            start[0]=i
            break
 #   print(start)
    s[start[0]][start[1]]=0

    for kk in range(1000):

        for i in range(h):
            for j in range(w):
                if s[i][j]>1000:
                    continue
                if i+1<h:
                    if s[i][j]>s[i+1][j] and s[i+1][j]<1000:
                            s[i][j]=s[i+1][j]+1
                if i-1>=0:
                    if s[i][j] > s[i - 1][j] and s[i - 1][j] < 1000:
                            s[i][j] = s[i - 1][j] + 1
                if j+1<w:
                    if s[i][j] > s[i][j+1] and s[i][j+1] < 1000:
                            s[i][j] = s[i][j+1] + 1
                if j-1>=0:
                    if s[i][j] > s[i][j - 1] and s[i][j - 1] < 1000:
                            s[i][j] = s[i][j - 1] + 1

 #   print(s)

    temp=[0,0]
    tempm=0
    for i in range(h):
        for j in range(w):
            if s[i][j]>tempm and s[i][j]<9999:
                tempm=s[i][j]
                temp=[i,j]
            if s[i][j]<9999:
                s[i][j]=1000
    start=[temp[0],temp[1]]
    s[start[0]][start[1]]=0

    for kk in range(1000):

        for i in range(h):
            for j in range(w):
                if s[i][j]>1000:
                    continue
                if i+1<h:
                    if s[i][j]>s[i+1][j] and s[i+1][j]<1000:
                            s[i][j]=s[i+1][j]+1
                if i-1>=0:
                    if s[i][j] > s[i - 1][j] and s[i - 1][j] < 1000:
                            s[i][j] = s[i - 1][j] + 1
                if j+1<w:
                    if s[i][j] > s[i][j+1] and s[i][j+1] < 1000:
                            s[i][j] = s[i][j+1] + 1
                if j-1>=0:
                    if s[i][j] > s[i][j - 1] and s[i][j - 1] < 1000:
                            s[i][j] = s[i][j - 1] + 1

#    print(s)
#    for i in range(h):
#        print(s[i])

    temp = [0, 0]
    tempm = 0
    for i in range(h):
        for j in range(w):
            if s[i][j] > tempm and s[i][j] < 9999:
                tempm = s[i][j]
                temp = [i, j]
            if s[i][j] < 9999:
                s[i][j] = 1000

    start = [temp[0], temp[1]]
    s[start[0]][start[1]] = 0

    for kk in range(1000):

        for i in range(h):
            for j in range(w):
                if s[i][j] > 1000:
                    continue
                if i + 1 < h:
                    if s[i][j] > s[i + 1][j] and s[i + 1][j] < 1000:
                        s[i][j] = s[i + 1][j] + 1
                if i - 1 >= 0:
                    if s[i][j] > s[i - 1][j] and s[i - 1][j] < 1000:
                        s[i][j] = s[i - 1][j] + 1
                if j + 1 < w:
                    if s[i][j] > s[i][j + 1] and s[i][j + 1] < 1000:
                        s[i][j] = s[i][j + 1] + 1
                if j - 1 >= 0:
                    if s[i][j] > s[i][j - 1] and s[i][j - 1] < 1000:
                        s[i][j] = s[i][j - 1] + 1

    #    print(s)
    #    for i in range(h):
    #        print(s[i])

    temp = [0, 0]
    tempm = 0
    for i in range(h):
        for j in range(w):
            if s[i][j] > tempm and s[i][j] < 9999:
                tempm = s[i][j]
                temp = [i, j]
            if s[i][j] < 9999:
                s[i][j] = 1000
    print(tempm)

if __name__ == "__main__":
    main()

Submission status: Wrong Answer

Solution code:
import sys
import copy
sys.setrecursionlimit(10**7)

h,w = map(int,input().split())
s = [list(input()) for i in range(h)]
count = 0

def dfs(x,y,gx,gy,c):
    global count
    global ans
    if not(0 <= x < h) or not(0 <= y < w) or c[x][y] == "#":
        return
    else:
        count += 1
    if x == gx and y == gy:
        ans.append(count)
        #print(count)
        return count
    c[x][y] = "#"
    dfs(x+1,y, gx, gy,c)
    dfs(x-1,y, gx, gy,c)
    dfs(x,y+1, gx, gy,c)
    dfs(x,y-1, gx, gy,c)

sx, sy = 0, 0
for i in range(h):
    for j in range(w):
        if s[i][j] == ".":
            sx, sy = i, j
            break
        
ans = []

for i in range(h):
    for j in range(w):
        if s[i][j] == ".":
            gx, gy = i, j
            c = copy.deepcopy(s)
            (dfs(sx, sy, gx, gy, c))
            count = 0

print(max(ans)-1)

Submission status: Wrong Answer

Solution code:
from collections import deque
h, w = map(int, input().split())
grid = []
for i in range(h):
    grid.append(list(input()))
def check(s,t):
    if (0 <= s <= w - 1) and (0 <= t <= h - 1):
        return True
    else:
        return False
def bfs(x, y, cost):
    if check(x,y):
        if grid[y][x] != "#":
            if cost < graph[y][x]:
                graph[y][x] = cost
                bfs(x+1,y,cost+1)
                bfs(x,y+1,cost+1)
                bfs(x-1,y,cost+1)
                bfs(x,y-1,cost+1)
global ans
ans = 0
inf = 50
for i in range(h):
    for j in range(w):
        graph = [[inf for p in range(w)] for q in range(h)]
        bfs(j, i ,0)
        g_max = 0
        for i in range(h):
            for j in range(w):
                if graph[i][j] != inf:
                    g_max = max(g_max, graph[i][j])
        ans = max(g_max, ans)
print(ans)

Submission status: Wrong Answer

Solution code:
h,w=map(int,input().split())
d=[]
for i in range(h):
    s=input()
    d.append(s)
#p={}
x=h*w
p=[[500]*x for i in range(x)]
for i in range(x):
    for j in range(x):
        for k in range(x):
            ih,iw=i//w,i%w
            jh,jw=j//w,j%w
            kh,kw=k//w,k%w
            if "."==d[ih][iw]==d[jh][jw] and ih==jh and abs(iw-jw)==1:
                p[i][j]=1
                p[j][i]=1
            if "."==d[ih][iw]==d[jh][jw] and iw==jw and abs(ih-jh)==1:
                p[i][j]=1
                p[j][i]=1
            if "."==d[ih][iw]==d[kh][kw] and ih==kh and abs(iw-kw)==1:
                p[i][k]=1
                p[k][i]=1
            if "."==d[ih][iw]==d[kh][kw] and iw==kw and abs(ih-kh)==1:
                p[i][k]=1
                p[k][i]=1
            if "."==d[jh][jw]==d[kh][kw] and jh==kh and abs(jw-kw)==1:
                p[j][k]=1
                p[k][j]=1
            if "."==d[jh][jw]==d[kh][kw] and jw==kw and abs(jh-kh)==1:
                p[j][k]=1
                p[k][j]=1


            p[j][k]=min(p[j][k],p[j][i]+p[i][k])
ans=0
for i in p:
    for j in i:
        if j!=500:
            ans=max(ans,j)
print(ans)

Submission status: Wrong Answer

Solution code:
import copy

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

def solve(sx,sy):
	flg = copy.deepcopy(flg_orig)
	flg[sx][sy] = 0
	q = [[sx,sy]]
	cnt = 0
	while q != []:
		cx, cy = q.pop(0)
		cd = flg[cx][cy]
		nps = [[cx+1,cy],[cx-1,cy],[cx,cy+1],[cx,cy-1]]
		for nx,ny in nps:
			if not (0 <= nx < H and 0 <= ny < W):
				continue
			if flg[nx][ny] == "#":
				continue
			if flg[nx][ny] <= cd+1:
				continue
			else:
				flg[nx][ny] = cd + 1

			q.append([nx,ny])
			cnt = max(cnt, cd+1)
	return cnt

INF = float("inf")
flg_orig = [[INF for _ in range(W)] for _ in range(H)]
for i in range(H):
	for j in range(W):
		if S[i][j] == "#":
			flg_orig[i][j] = "#"


ans = 0
for start_x in range(H):
	for start_y in range(W):
		tmp_cnt = solve(start_x, start_y)
		ans = max(ans, tmp_cnt)

print(ans)

Submission status: Wrong Answer

Solution code:
import sys
from copy import deepcopy
input = sys.stdin.readline
h, w = [int(x) for x in input().strip().split()]
m = [['#' for _ in range(w+2)] for _ in range(h+2)]

for hi in range(1, h+1):
    for wi, s in enumerate(input().strip(), start=1):
        m[hi][wi] = s
        if s == '.':
            hs, ws = hi, wi

def bfs(hs, ws, m):
    cost = 0
    q = []
    m[hs][ws] = '#'
    for offset in [(0, 1), (-1, 0), (0, -1), (1, 0)]:
        if m[hs+offset[0]][ws+offset[1]] == '.':
            q.append((hs+offset[0], ws+offset[1]))
    while q:
        cost += 1
        q_ = q
        q = []
        for h, w in q_:
            m[h][w] = '#'
            for offset in [(0, 1), (-1, 0), (0, -1), (1, 0)]:
                if m[h+offset[0]][w+offset[1]] == '.':
                    q.append((h+offset[0], w+offset[1]))

    return cost, h, w

maxi = -1
maxi_ = 0
while maxi != maxi_:
    maxi_ = maxi
    maxi, hs, ws = bfs(hs, ws, deepcopy(m[:]))

print(maxi)

Submission status: Wrong Answer

Solution code:
from collections import deque

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

# ある地点を渡すと、その地点から到達可能かつ距離未計算の地点のリストを返す
def not_investigated_positions(current_position):
    output = []
    for udrl in [[0, 1], [0, -1], [1, 0], [-1, 0]]:
        y = current_position[0]+udrl[0]
        x = current_position[1]+udrl[1]
        if 0 <= y <= H-1 and 0 <= x <= W-1:
            if S[y][x] == "." and distance[y][x] == -1:
                output.append([y, x])
    return output

ans = 0

for sy in range(H):
    for sx in range(W):
        if S[sy][sx] == "#":
            continue
        distance = [[-1]*W for _ in range(H)]
        current_position = [sy, sx]
        distance[sy][sx] = 0
        next_positions = deque()
        next_positions.append(current_position)

        while next_positions:
            current_position = next_positions.popleft()
            for q in not_investigated_positions(current_position):
                distance[q[0]][q[1]] = distance[current_position[0]][current_position[1]] + 1
                next_positions.append(q)

        ans = max(ans, max(max(distance)))

print(ans)

Submission status: Wrong Answer

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

Map = []
Map.append(list('@'*(W+2)))
for i in range(H):
  s = list('@'+input()+'@')
  Map.append(s)
Map.append(list('@'*(W+2)))
  
#400*400 = 160000 = 1.6*10**5
#スタートとゴール用
select = []
for i in range(H+2):
 if((i != 0)and(i != H+1)):
  for j in range(W+2):
   if((j != 0)and(j != W+1)):
    for k in range(H+2):
     if((k != 0)and(k != H+1)):
      for l in range(W+2):
       if((l != 0)and(l != W+1)):
         select.append([i,j,k,l])
#print(len(select))          

#幅優先探索
def bfs(sy,sx,gy,gx,Map):
  qy,qx = queue.Queue(),queue.Queue()
  qy.put(sy),qx.put(sx)
  ans = 0
  while(1):
    if(qx.empty() == True): return ans
    #座標の取り出し
    y,x = qy.get(),qx.get()
    if((y == gy)and(x == gx)): #ゴールなら終了
      ans = Map[y][x]
      #for i in range(H+2):
      #   print(Map[i])
      #print('______________')
      return ans
    
    #次に進むところ
    vec_y = [0,0,1,-1]
    vec_x = [1,-1,0,0]
    for i in range(4):
      if(Map[y+vec_y[i]][x+vec_x[i]] == '.'):
        Map[y+vec_y[i]][x+vec_x[i]] = Map[y][x] + 1
        qy.put(y+vec_y[i]),qx.put(x+vec_x[i])
           
ans = 0 
for s in range(len(select)):
  sy,sx = select[s][0], select[s][1] 
  gy,gx = select[s][2], select[s][3]
  
  Map2 = []
  for i in range(H+2):
    Map2.append(Map[i])
    
  #スタートとゴールの設置
  if((Map2[sy][sx] == '.')and(Map2[gy][gx] == '.')):
   Map2[sy][sx] = 0
   #幅優先探索
   tmp_ans = bfs(sy,sx,gy,gx,Map2)
   #print(sy,sx,gy,gx)
   #print(tmp_ans)
   ans = max(ans,tmp_ans)

print(ans)

Submission status: Wrong Answer

Solution code:
h,w = map(int,input().split())
s = [list(input()) for i in range(h)]
p = [(0,1),(1,0),(-1,0),(0,-1)]
mx = 0
for i in range(h):
    for j in range(w):
        if s[i][j] != '#':
            corse = {}
            start = (i,j)
            n1 ={start}
            cnt = 1
            while n1 != set():
                n2 = set()
                for k in n1:
                    for l in p:
                        nk = (k[0]+l[0],k[1]+l[1])
                        if 0<=nk[0]<h and 0<=nk[1]<w:
                         if s[nk[0]][nk[1]] != '#' and corse.get(nk) == None:
                            corse[nk] = 1
                            mx = max(cnt,mx)
                            n2.add(nk)
                cnt += 1
                n1 = n2
print(mx)

Submission status: Wrong Answer

Solution code:

from collections import deque
from copy import deepcopy


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

step=[[1,0],[-1,0],[0,1],[0,-1]]

mx=0
for h in range(H):
    for w in range(W):
        if S[h][w]=="#":continue
        chk=deepcopy(S)
        q=deque([[h,w,0]])
        chk[h][w]="#"
        while q:
            y,x,c=q.popleft()
            for dh,dw in step:
                if y+dh<0 or y+dh>=H:continue
                if x+dw<0 or x+dw>=W:continue
                if chk[y+dh][x+dw]==".":
                    c+=1
                    chk[y+dh][x+dw]="#"
                    q.append([y+dh,x+dw,c])
                else:
                    mx=max(mx,c)

print(mx)

Submission status: Wrong Answer

Solution code:
from collections import defaultdict as dd, deque

h,w = map(int,input().split())
M = [input().strip()+'#' for _ in range(h)]
M.append(w*'#')

start = None
for i in range(h):
    for j in range(w):
        if M[i][j] == '.':
            start = (j,i)

Q = deque()
Q.append((0, start[0], start[1]))
seen = set()

while Q:
    d,x,y = Q.popleft()

    if (x,y) in seen: continue
    seen.add((x,y))

    for nx,ny in [(x+1,y), (x-1,y), (x,y+1), (x,y-1)]:
        if M[ny][nx] == '.':
            Q.append((d+1, nx, ny))


Q = deque()
Q.append((0, x, y))
seen = set()

while Q:
    d,x,y = Q.popleft()

    if (x,y) in seen: continue
    seen.add((x,y))

    for nx,ny in [(x+1,y), (x-1,y), (x,y+1), (x,y-1)]:
        if M[ny][nx] == '.':
            Q.append((d+1, nx, ny))

print(d)

Submission status: Wrong Answer

Solution code:
# coding: utf-8
# Your code here!

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

ans = 0
for _ in range(h):
    for s in input():
        if s == '.':
            ans += 1
            
print(ans - 1)

Submission status: Wrong Answer

Solution code:
print(39)

Submission status: Wrong Answer

Solution code:
#import sys
MOD = 10 ** 9 + 7
#input = sys.stdin.readline
import math

import itertools

#import bisect

import copy


def sosuhante(n):
    for k in range(2, int(math.sqrt(n))+1):
        if n% k ==0:
            return False
    return True
def cmb(n, r):
    if n - r < r: r = n - r
    if r == 0: return 1
    if r == 1: return n

    numerator = [n - r + k + 1 for k in range(r)]
    denominator = [k + 1 for k in range(r)]

    for p in range(2,r+1):
        pivot = denominator[p - 1]
        if pivot > 1:
            offset = (n - r) % p
            for k in range(p-1,r,p):
                numerator[k - offset] /= pivot
                denominator[k] /= pivot

    result = 1
    for k in range(r):
        if numerator[k] > 1:
            result *= int(numerator[k])

    return result

def kingaku(a,b,n):
    keta=len(str(n))
    return a*n+b*keta

def my_index(l, x, default=False):
    if x in l:
        return l.index(x)
    else:
        return default

#    h,w,a,b = map(int, input().split())
#    c = [[0 for j in range(n)] for i in range(n)]
def main():
    h,w=map(int,input().split())
    s=[input() for i in range(h)]
    for i in range(h):
        s[i]=list(s[i])
    for i in range(h):
        for j in range(w):
            if s[i][j]==".":
                s[i][j]=1000
            else:
                s[i][j]=9999
 #   print(s)
    start=[0,0]
    for i in range(h):
        start[1]=s[i].index(min(s[i]))
        if min(s[i]) >= 1111:
            continue
        else:
            start[0]=i
            break
 #   print(start)
    s[start[0]][start[1]]=0

    for kk in range(1000):

        for i in range(h):
            for j in range(w):
                if s[i][j]>1000:
                    continue
                if i+1<h:
                    if s[i][j]>s[i+1][j] and s[i+1][j]<1000:
                            s[i][j]=s[i+1][j]+1
                if i-1>=0:
                    if s[i][j] > s[i - 1][j] and s[i - 1][j] < 1000:
                            s[i][j] = s[i - 1][j] + 1
                if j+1<w:
                    if s[i][j] > s[i][j+1] and s[i][j+1] < 1000:
                            s[i][j] = s[i][j+1] + 1
                if j-1>=0:
                    if s[i][j] > s[i][j - 1] and s[i][j - 1] < 1000:
                            s[i][j] = s[i][j - 1] + 1

 #   print(s)

    temp=[0,0]
    tempm=0
    for i in range(h):
        for j in range(w):
            if s[i][j]>tempm and s[i][j]<9999:
                tempm=s[i][j]
                temp=[i,j]
            if s[i][j]<9999:
                s[i][j]=1000
    start=[temp[0],temp[1]]
    s[start[0]][start[1]]=0

    for kk in range(1000):

        for i in range(h):
            for j in range(w):
                if s[i][j]>1000:
                    continue
                if i+1<h:
                    if s[i][j]>s[i+1][j] and s[i+1][j]<1000:
                            s[i][j]=s[i+1][j]+1
                if i-1>=0:
                    if s[i][j] > s[i - 1][j] and s[i - 1][j] < 1000:
                            s[i][j] = s[i - 1][j] + 1
                if j+1<w:
                    if s[i][j] > s[i][j+1] and s[i][j+1] < 1000:
                            s[i][j] = s[i][j+1] + 1
                if j-1>=0:
                    if s[i][j] > s[i][j - 1] and s[i][j - 1] < 1000:
                            s[i][j] = s[i][j - 1] + 1

#    print(s)
#    for i in range(h):
#        print(s[i])

    temp = [0, 0]
    tempm = 0
    for i in range(h):
        for j in range(w):
            if s[i][j] > tempm and s[i][j] < 9999:
                tempm = s[i][j]
                temp = [i, j]
            if s[i][j] < 9999:
                s[i][j] = 1000
    print(tempm)

if __name__ == "__main__":
    main()

Submission status: Wrong Answer

Solution code:
H, W = map(int, input().split())
S=[['' for i in range(W)] for j in range(H)]
index=[[0 for i in range(W)] for j in range(H)]
LUT = list(range(1))
LUT[0] = 0

for i in range(H):
  column=list(input())
  for j in range(W):
    S[i][j]=column[j]

for i in range(H):
  for j in range(W):
    if S[i][j]==".":
      up=S[i][j-1]
      left=S[i-1][j]
      leftIndex=index[i-1][j]
      upIndex=index[i][j-1]

      #上と左どちらも#の場合
      if up=="#" or j-1<0:
        if left == "#" or i-1<0:
          newIndex = len(LUT)
          LUT.append(newIndex)
          index[i][j] = newIndex
          continue
      #左が白の場合
      if up=="#" or j-1<0:
        if left == ".":
          index[i][j] = leftIndex
          continue

      #上が白の場合
      if up==".":
        if left == "#" or i-1<0:
          index[i][j] = upIndex
          continue

      #上と左どちらも白の場合
      if up=="." and left==".":
        if LUT[upIndex]<LUT[leftIndex]:
          index[i][j] = LUT[upIndex]
          keyLUT = LUT[leftIndex]
          for k in range(len(LUT)):
            if LUT[k]==keyLUT:
              LUT[k] = LUT[upIndex]
        else:
          index[i][j] = LUT[leftIndex]
          keyLUT = LUT[upIndex]
          for k in range(len(LUT)):
            if LUT[k]==keyLUT:
              LUT[k] = LUT[leftIndex]

indexCount = 0;
for i in range(len(LUT)):
  if LUT[i]>indexCount:
    keyLUT = LUT[i]
    indexCount += 1
    for j in range(len(LUT)):
      if LUT[j]==keyLUT:
        LUT[j]=indexCount

for i in range(H):
  for j in range(W):
    index[i][j] = LUT[index[i][j]]

LUTcount=[0 for i in range(len(LUT))]
for i in range(H):
  for j in range(W):
    LUTcount[index[i][j]]+=1
print(max(LUTcount[1:])-1)

Submission status: Wrong Answer

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


def dfs(start_h,start_w):
    q=deque()
    maze_copy=copy.deepcopy(maze)
    walk_count=[[0 for _ in range(w)] for _ in range(h)]
    q.append([start_h,start_w])
    max_count=0
    while(len(q)!=0):
        position=q.popleft()
        now_h=position[0]
        now_w=position[1]
        if(now_h>0 and maze_copy[now_h-1][now_w]=='.'):
            walk_count[now_h-1][now_w]=walk_count[now_h][now_w]+1
            maze_copy[now_h-1][now_w]="#"
            q.append([now_h-1,now_w])
            if(max_count<walk_count[now_h-1][now_w]):
                max_count=walk_count[now_h-1][now_w]

        if(now_h<h-1 and maze_copy[now_h+1][now_w]=='.'):
            walk_count[now_h+1][now_w]=walk_count[now_h][now_w]+1
            maze_copy[now_h+1][now_w]="#"
            q.append([now_h+1,now_w])
            if(max_count<walk_count[now_h+1][now_w]):
                max_count=walk_count[now_h+1][now_w]

        if(now_w>0 and maze_copy[now_h][now_w-1]=='.'):
            walk_count[now_h][now_w-1]=walk_count[now_h][now_w]+1
            maze_copy[now_h][now_w-1]="#"
            q.append([now_h,now_w-1])
            if(max_count<walk_count[now_h][now_w-1]):
                max_count=walk_count[now_h][now_w-1]

        if(now_w<w-1 and maze_copy[now_h][now_w+1]=='.'):
            walk_count[now_h][now_w+1]=walk_count[now_h][now_w]+1
            maze_copy[now_h][now_w+1]="#"
            q.append([now_h,now_w+1])
            if(max_count<walk_count[now_h][now_w+1]):
                max_count=walk_count[now_h][now_w+1]
    return max_count

ans=0
for i in range(h):
    for j in range(w):
        if(maze[i][j]=='.'):
            ij_count=dfs(i,j)
            if(ans<ij_count):
                ans=ij_count
print(ans)

Submission status: Wrong Answer

Solution code:
h,w = map(int,input().split())
s = [list(input()) for i in range(h)]
p = [(0,1),(1,0),(-1,0),(0,-1)]
mx = 0
for i in range(h):
    for j in range(w):
        corse = {}
        start = (i,j)
        n1 ={start}
        cnt = 1
        while n1 != set():
            n2 = set()
            for k in n1:
                for l in p:
                    nk = (k[0]+l[0],k[1]+l[1])
                    if 0<=nk[0]<h and 0<=nk[1]<w:
                     if s[nk[0]][nk[1]] != '#' and corse.get(nk) == None:
                        corse[nk] = 1
                        mx = max(cnt,mx)
                        n2.add(nk)
            cnt += 1
            n1 = n2
print(mx)

Submission status: Wrong Answer

Solution code:
H,W=map(int,input().split())
grid=[None]*H
# 仮のスタート
sx,sy=-1,-1
found=False
for i in range(H):
  grid[i]=input()
  if not found:
    for j in range(len(grid[i])):
      if grid[i][j]==".":
        sy=i
        sx=j
        found=True
        
from collections import deque
stack=deque()
# y,x,そこまでの距離
stack.append([sy,sx,0])
INF=10**18
seen=[[INF for j in range(W)] for i in range(H)]

longest=0
longestY=-1
longestX=-1
while stack:
  node=stack.popleft()
  y=node[0]
  x=node[1]
  dist=node[2]
  if grid[y][x]=="#":
    continue
  if seen[y][x]<=dist:
    continue
  if dist>longest:
    longest=dist
    longestY=y
    longestX=x
  seen[y][x]=dist
  if y-1>=0:
    stack.append([y-1,x,dist+1])
  if y+1<H:
    stack.append([y+1,x,dist+1])
  if x-1>=0:
    stack.append([y,x-1,dist+1])
  if x+1<W:
    stack.append([y,x+1,dist+1])
    
# ここから後半
q=deque()
q.append([longestY,longestX,0])
visited=[[INF for j in range(W)] for i in range(H)]
ans=0
while q:
  node=q.popleft()
  y=node[0]
  x=node[1]
  dist=node[2]
  if grid[y][x]=="#":
    continue
  if visited[y][x]<=dist:
    continue
  if dist>ans:
    ans=dist
  visited[y][x]=dist
  if y-1>=0:
    q.append([y-1,x,dist+1])
  if y+1<H:
    q.append([y+1,x,dist+1])
  if x-1>=0:
    q.append([y,x-1,dist+1])
  if x+1<W:
    q.append([y,x+1,dist+1])

print(ans)

Submission status: Wrong Answer

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

d={}
n=0

def f(i,j):
  if "{}{}".format(i,j) in d:
    if d["{}{}".format(i,j)]==".":
      return 1
    else:
      return 0
  else:
    return 0

for i in range(h):
  s=input()
  for j in range(w):
    d["{}{}".format(i,j)]=s[j]

t=0
for i in range(h):
  for j in range(w):
    if d["{}{}".format(i,j)]==".":
      p=0
      p=p+f(i+1,j)
      p=p+f(i,j+1)
      p=p+f(i-1,j)
      p=p+f(i,j-1)
      if p>0:
        t=t+1
    
print(t-1)

Submission status: Wrong Answer

Solution code:
from array import array

h, w = map(int, input().split())
maze = []
for i in range(h):
    maze.append(input())

g = []

for i in range(w * h):
    g.append(array('l', [10000000] * (w * h)))

for i in range(w * h):
    g[i][i] = 0

for i in range(h):
    for j in range(w):
        if maze[i][j] == '#':
            continue
        if maze[i][j] == '.':
            if i > 0 and maze[i-1][j] != '#':
                g[i * w + j][(i-1) * w + j] = 1
                g[(i-1) * w + j][i * w + j] = 1
            if i < h-1 and maze[i+1][j] != '#':
                g[i * w + j][(i+1) * w + j] = 1
                g[(i+1) * w + j][i * w + j] = 1
            if j > 0 and maze[i][j-1] != '#':
                g[i * w + j][i * w + j-1] = 1
                g[i * w + j-1][i * w + j] = 1
            if j < w-1 and maze[i][j+1] != '#':
                g[i * w + j][i * w + j+1] = 1
                g[i * w + j+1][i * w + j] = 1

def floyed(g, h, w):
    result = 0
    for k in range(h*w):
        for i in range(h*w):
            if i == k:
                continue
            if g[i][k] == 10000000:
                continue
            for j in range(h*w):
                if g[k][j] == 10000000:
                    continue
                if i == j or i == k or k == j:
                    continue
                if g[i][j] > g[i][k] + g[k][j]:
                    g[i][j] = g[i][k] + g[k][j]
                    if g[i][j] > result:
                        result = g[i][j]
    return result

print(floyed(g,h,w))

Submission status: Wrong Answer

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

H, W = map(int, input().split())

maze = np.zeros((H, W))

for i in range(H):
    s = input()
    for j in range(W):
        if s[j] == '.':
            maze[i, j] = 1

max_run = 0

tan = [[1, 0], [-1, 0], [0, 1], [0, -1]]
g = (100, 100)

def max_route(h, w):
    # q = queue.Queue()
    q = deque()
    checker = maze.copy()
    q.append((h, w))
    q.append(g)

    run = 0

    while len(q):
        target = q.popleft()
        if target == g:
            if len(q):
                run += 1
                q.append(g)
        else:
            for t in tan:
                check = [target[0]+t[0], target[1]+t[1]]
                if 0 <= check[0] < H and 0 <= check[1] < W:
                    if checker[check[0], check[1]] == 1:
                        q.append((check[0], check[1]))
                        checker[check[0], check[1]] = 2

    return run


for h in range(H):
    for w in range(W):
        if maze[h, w] == 0:
            continue
        cur = max_route(h, w)
        if cur > max_run:
            max_run = cur

print(max_run)