qyliang/CodeNanoFix huggingface dataset:
Buggy code:
from collections import deque
from typing import List


def main():
    h, w = map(int, input().split())
    g = []
    for _ in range(h):
        r = input().split()
        g.append(r)
    print(mm(h, w, g))


def mm(h: int, w: int, g: List[List[str]]) -> int:
    ret = 0
    for i in range(h):
        for j in range(w):
            if (g[i][j] == '#'):
                continue
            cnt = 0
            q = deque([(i, j, cnt)])
            v = set()
            while q:
                ii, jj, cnt = q.popleft()
                v.add((ii, jj))
                ret = max(ret, cnt)
                # ↓
                if ii < h - 1 and g[ii + 1][jj] == '.' and (ii + 1, jj) not in v:
                    q.append((ii + 1, jj, cnt + 1))
                # →
                if jj < w - 1 and g[ii][jj + 1] == '.' and (ii, jj + 1) not in v:
                    q.append((ii, jj + 1, cnt + 1))
                # ↑
                if ii > 0 and g[ii - 1][jj] == '.' and (ii - 1, jj) not in v:
                    q.append((ii - 1, jj, cnt + 1))
                # ←
                if jj > 0 and g[ii][jj - 1] == '.' and (ii, jj - 1) not in v:
                    q.append((ii, jj - 1, cnt + 1))
    return ret


if __name__ == '__main__':
    main()


Fixed code:
from collections import deque
from typing import List


def main():
    h, w = map(int, input().split())
    g = []
    for _ in range(h):
        r = list(input())
        g.append(r)
    print(mm(h, w, g))


def mm(h: int, w: int, g: List[List[str]]) -> int:
    ret = 0
    for i in range(h):
        for j in range(w):
            if (g[i][j] == '#'):
                continue
            v = set()
            q = deque([(i, j, 0)])
            v.add((i, j))
            while q:
                ii, jj, cnt = q.popleft()
                ret = max(ret, cnt)
                # ↓
                if ii < h - 1 and g[ii + 1][jj] == '.' and (ii + 1, jj) not in v:
                    q.append((ii + 1, jj, cnt + 1))
                    v.add((ii + 1, jj))
                # →
                if jj < w - 1 and g[ii][jj + 1] == '.' and (ii, jj + 1) not in v:
                    q.append((ii, jj + 1, cnt + 1))
                    v.add((ii, jj + 1))
                # ↑
                if ii > 0 and g[ii - 1][jj] == '.' and (ii - 1, jj) not in v:
                    q.append((ii - 1, jj, cnt + 1))
                    v.add((ii - 1, jj))
                # ←
                if jj > 0 and g[ii][jj - 1] == '.' and (ii, jj - 1) not in v:
                    q.append((ii, jj - 1, cnt + 1))
                    v.add((ii, jj - 1))
    return ret


if __name__ == '__main__':
    main()
Buggy code:
from collections import deque
import copy
import sys
sys.setrecursionlimit(1000)
a,b=map(int,input().split())
maze=[list(input()) for i in range(a)]
ans=0
for i in range(a):
  for j in range(b):
    l=copy.deepcopy(maze)
    if l[i][j]=="#":
      break
    l[i][j]=0
    q=deque([[i,j]])
    while q:
      h,w=q.popleft()
      if 0<=h-1<a and 0<=w<b:
        if l[h-1][w]==".":
          l[h-1][w]=l[h][w]+1
          q.append([h-1,w])
      if 0<=h+1<a and 0<=w<b:
        if l[h+1][w]==".":
          l[h+1][w]=l[h][w]+1
      if 0<=h<a and 0<=w-1<b:
        if l[h][w-1]==".":
          l[h][w-1]=l[h][w]+1
          q.append([h,w-1])
      if 0<=h<a and 0<=w+1<b:
        if l[h][w+1]==".":
          l[h][w+1]=l[h][w]+1
          q.append([h,w+1])
    s=0
    for x in l:
      for y in x:
        if type(y)==int:
          if y>s:
            s=y
    if ans<s:
      ans=s
print(ans)

Fixed code:
from collections import deque
import copy
import sys
sys.setrecursionlimit(1000)
a,b=map(int,input().split())
maze=[list(input()) for i in range(a)]
ans=0
for i in range(a):
  for j in range(b):
    l=copy.deepcopy(maze)
    if l[i][j]!="#":
      l[i][j]=0
      q=deque([[i,j]])
      while q:
        h,w=q.popleft()
        if 0<=h-1<a and 0<=w<b:
          if l[h-1][w]==".":
            l[h-1][w]=l[h][w]+1
            q.append([h-1,w])
        if 0<=h+1<a and 0<=w<b:
          if l[h+1][w]==".":
            l[h+1][w]=l[h][w]+1
            q.append([h+1,w])
        if 0<=h<a and 0<=w-1<b:
          if l[h][w-1]==".":
            l[h][w-1]=l[h][w]+1
            q.append([h,w-1])
        if 0<=h<a and 0<=w+1<b:
          if l[h][w+1]==".":
            l[h][w+1]=l[h][w]+1
            q.append([h,w+1])
      s=0
      for x in l:
        for y in x:
          if type(y)==int:
            if y>s:
              s=y
      if ans<s:
        ans=s
print(ans)
Buggy code:
H, W = map(int, input().split())

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

ans = 0
for ii in range(1,H+1):
    for jj in range(1,W+1):
        if maze[ii][jj]=='#':continue
        temp = deque()
        temp.append([ii,jj])
        mazenum = [[0]*(W+2) for _ in range(H+2)]
        mazenum[ii][jj] = 0
        visited = [[False]*(W+2) for _ in range(H+2)]
        visited[ii][jj] = True

        while temp:
            p = temp.popleft()
            m = mazenum[p[0]][p[1]]
            for i, j in [[p[0]-1,p[1]], [p[0]+1, p[1]], [p[0],p[1]-1],[p[0],p[1]+1]]:
                if maze[i][j]=='.':
                    if not visited[i][j]:
                        temp.append([i, j])
                        mazenum[i][j] = m + 1
                        visited[i][j]=True        
        ans = max(ans, max(max(mazenum)))     

print(ans)


Fixed code:
from collections import deque
H, W = map(int, input().split())

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

for ii in range(1,H+1):
    for jj in range(1,W+1):
        if maze[ii][jj]=='#':continue
        temp = deque()
        temp.append([ii,jj])
        mazenum = [[0]*(W+2) for _ in range(H+2)]
        mazenum[ii][jj] = 0
        visited = [[False]*(W+2) for _ in range(H+2)]
        visited[ii][jj] = True


        while temp:
            p = temp.popleft()
            m = mazenum[p[0]][p[1]]
            for i, j in [[p[0]-1,p[1]], [p[0]+1, p[1]], [p[0],p[1]-1],[p[0],p[1]+1]]:
                if maze[i][j]=='.':
                    if not visited[i][j]:
                        temp.append([i, j])
                        mazenum[i][j] = m + 1
                        visited[i][j]= True        
        ans = max(ans, max(max(x) for x in mazenum))

print(ans)
Buggy code:
h, w = [int(i) for i in input().split()]

s = [input() for _ in range(h)]

def search(s_h, s_w):
    stack = []
    isuse = [[False] * w for _ in range(h)]
    def add(a, b,score):
        nonlocal stack, isuse
        if not (0 <= a < h and 0 <= b < w):
            return 0
        if s[a][b] == "#":
            return 0
        if isuse[a][b]:
            return 0
        isuse[a][b] = True
        stack.append((a,b,score+1))
        
    add(s_h, s_w, 0)
    c=0
    while stack:
        a,b,c = stack.pop(0)
        add(a + 1, b, c)
        add(a , b + 1, c)
        add(a, b - 1, c)
        add(a - 1, b, c)
    return c

search(0,0)

ans = 0
for s_h in range(h):
    for s_w in range(w):
        ans = max(search(s_h, s_w), ans)
        print(s_h,s_w,search(s_h, s_w))
print(ans)


Fixed code:
h, w = [int(i) for i in input().split()]

s = [input() for _ in range(h)]

def search(s_h, s_w):
    stack = []
    isuse = [[False] * w for _ in range(h)]
    def add(a, b,score):
        nonlocal stack, isuse
        if not (0 <= a < h and 0 <= b < w):
            return 0
        if s[a][b] == "#":
            return 0
        if isuse[a][b]:
            return 0
        isuse[a][b] = True
        stack.append((a,b,score+1))
        
    add(s_h, s_w, -1)
    c=0
    while stack:
        a,b,c = stack.pop(0)
        add(a + 1, b, c)
        add(a , b + 1, c)
        add(a, b - 1, c)
        add(a - 1, b, c)
    return c

search(0,0)

ans = 0
for s_h in range(h):
    for s_w in range(w):
        ans = max(search(s_h, s_w), ans)
        #print(s_h,s_w,search(s_h, s_w))
print(ans)
Buggy code:
from collections import deque
h,w=map(int,input().split())
S=[input() for _ range(h)]
inf=float("inf")
ans=0
for i in range(h):
   for j in range(w):
      if S[i][j]=="#":
         continue
      else:
         DP=[[inf for _ in range(w)]for _ in range(h)]
         D[i][j]=0
         que=deque([(i,j)])
         while que:
            si,sj=que.popleft()
            for ni,nj in [(si+1,sj)(si-1,sj)(si,sj+1)(si,sj-1)]:
               if 0<=ni<h and 0<=nj<w and S[ni][nj]!="#":
                  if D[ni][nj]>D[si][sj]+1:
                     D[ni][nj]=D[si][sj]+1
                     que.append((ni,nj))
                     ans=max(ans,D[si][sj]+1)
print(ans)

Fixed code:
from collections import deque

h,w=map(int,input().split())
S=[input() for _ in range(h)]
inf=float("inf")
ans=0

for i in range(h):
    for j in range(w):
        if S[i][j]=="#":
            continue
        else:
            dist=[[inf for _ in range(w)] for _ in range(h)]
            dist[i][j]=0
            que=deque([(i,j)])
            while que:
                si,sj=que.popleft()
                for ni,nj in [(si+1,sj),(si-1,sj),(si,sj+1),(si,sj-1)]:
                    if 0<=ni<=h-1 and 0<=nj<=w-1 and S[ni][nj]!="#":
                        if dist[ni][nj]>dist[si][sj]+1:
                            dist[ni][nj]=dist[si][sj]+1
                            ans=max(ans,dist[si][sj]+1)
                            que.append((ni,nj))

print(ans)
Buggy code:
h,w=map(int,input().split())
s=[list(input())for i in range(h)]
h,w=20,20
s=[["."]*20for j in range(20)]
e=0
for i in range(h):
    for j in range(w):
        if s[i][j]==".":
            p=[i,j]
            q=[p]
            t=[[1]*w for j in range(h)]
            t[p[0]][p[1]]=0
            g=0
            while q:
                c=[]
                for i,j in q:
                    a=[1,-1,0,0]
                    b=[0,0,1,-1]
                    for d in range(4):
                        if 0<=i+a[d]<h and 0<=j+b[d]<w:
                            if s[i+a[d]][j+b[d]]==".":
                                if t[i+a[d]][j+b[d]]:
                                    t[i+a[d]][j+b[d]]=0
                                    c.append([i+a[d],j+b[d]])
                if len(c)==0:
                    break
                else:
                    g+=1
                    q=c[::]
            e=max(e,g)
print(e)

Fixed code:
h,w=map(int,input().split())
s=[list(input())for i in range(h)]
e=0
for i in range(h):
    for j in range(w):
        if s[i][j]==".":
            p=[i,j]
            q=[p]
            t=[[1]*w for j in range(h)]
            t[p[0]][p[1]]=0
            g=0
            while q:
                c=[]
                for i,j in q:
                    a=[1,-1,0,0]
                    b=[0,0,1,-1]
                    for d in range(4):
                        if 0<=i+a[d]<h and 0<=j+b[d]<w:
                            if s[i+a[d]][j+b[d]]==".":
                                if t[i+a[d]][j+b[d]]:
                                    t[i+a[d]][j+b[d]]=0
                                    c.append([i+a[d],j+b[d]])
                if len(c)==0:
                    break
                else:
                    g+=1
                    q=c[::]
            e=max(e,g)
print(e)
Buggy code:
from collections import deque

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

dx = [0, 1, -1, 0]
dy = [1, 0, 0, -1]
           
def bfs(sx, sy):
    q = deque([])
    d = [[-1 for _ in range(W)] for _ in range(H)]
    q.append([sx, sy])
    d[sy][sx] = 0
    
    while(q):
        pop = q.popleft()
        x, y = pop[0], pop[1]
        for i in range(4):
            nx = x + dx[i]
            ny = y + dy[i]
            if 0 <= nx and nx < W and ny >= 0 and ny < H:
                if s[ny][nx] != "#" and d[ny][nx] == -1:
                    q.append([nx, ny])
                    d[ny][nx] = d[y][x] + 1
    return d

def max_dim2(d):
    ret = 0
    for i in range(len(d)):
        for j in d[i]:
            ret = max(ret, j)
    return ret

ans = 0
for i in range(H):
    for j in range(W):
        if d[i][j] == ".":
            ret = bfs(j, i)
            temp = max_dim2(ret)
            ans = max(ans, temp)
print(ans)    

Fixed code:
from collections import deque

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

dx = [0, 1, -1, 0]
dy = [1, 0, 0, -1]
           
def bfs(sx, sy):
    q = deque([])
    d = [[-1 for _ in range(W)] for _ in range(H)]
    q.append([sx, sy])
    d[sx][sy] = 0
    
    while(q):
        pop = q.popleft()
        x, y = pop[0], pop[1]
        for i in range(4):
            nx = x + dx[i]
            ny = y + dy[i]
            if 0 <= nx and nx < H and ny >= 0 and ny < W:
                if s[nx][ny] != "#" and d[nx][ny] == -1:
                    q.append([nx, ny])
                    d[nx][ny] = d[x][y] + 1
    return d

def max_dim2(d):
    ret = 0
    for i in range(len(d)):
        for j in d[i]:
            ret = max(ret, j)
    return ret

ans = 0
for i in range(H):
    for j in range(W):
        if s[i][j] == ".":
            ret = bfs(i, j)
            temp = max_dim2(ret)
            ans = max(ans, temp)
print(ans)
Buggy code:
from collections import deque


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

moves = [[-1,0], [1,0], [0,-1], [0,1]]



min_val = 0
for i in range(H):
    for j in range(W):
        if S[i][j] == "#":
            continue
        todo = deque([[i,j]])
        
        shortest = [[-10e+10 for _ in range(W)] for _ in range(H)]
        shortest[i][j] = 0
        
        while len(todo) > 0:
            print(todo)
            checking = todo.popleft()
            for move in moves:
                if checking[0] + move[0] >= 0 and checking[0] + move[0] < H and checking[1] + move[1] >= 0 and checking[1] + move[1] < W:
                    if shortest[checking[0]+move[0]][checking[1]+move[1]] == -10e+10 and S[checking[0]+move[0]][checking[1]+move[1]] == ".":
                        shortest[checking[0]+move[0]][checking[1]+move[1]] = shortest[checking[0]][checking[1]]+1
                        todo.append([checking[0]+move[0],checking[1]+move[1]])
        if min_val < max([max(row) for row in shortest]):
            min_val = max([max(row) for row in shortest])
print(min_val)

Fixed code:
from collections import deque

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

moves = [[-1,0], [1,0], [0,-1], [0,1]]



min_val = 0
for i in range(H):
    for j in range(W):
        if S[i][j] == "#":
            continue
        todo = deque([[i,j]])
        
        shortest = [[-10e+10 for _ in range(W)] for _ in range(H)]
        shortest[i][j] = 0
        
        while len(todo) > 0:
            checking = todo.popleft()
            for move in moves:
                if checking[0] + move[0] >= 0 and checking[0] + move[0] < H and checking[1] + move[1] >= 0 and checking[1] + move[1] < W:
                    if shortest[checking[0]+move[0]][checking[1]+move[1]] == -10e+10 and S[checking[0]+move[0]][checking[1]+move[1]] == ".":
                        shortest[checking[0]+move[0]][checking[1]+move[1]] = shortest[checking[0]][checking[1]]+1
                        todo.append([checking[0]+move[0],checking[1]+move[1]])
        if min_val < max([max(row) for row in shortest]):
            min_val = max([max(row) for row in shortest])
print(min_val)
Buggy code:
from collections import deque
H, W = map(int, input().split())
graph = [['#'] * (W+2) for _ in range(H+2)]
for i in range(H):
  S = input()
  for j in range(W):
    graph[i+1][j+1] = S[j]

def dfs(sx, sy, gx, gy):
  q = deque([(sx, sy)])
  dist = [[-1] * (W+2) for _ in range(H+2)]
  dist[sx][sy] = 0
  while q:
    x, y = q.popleft()
    dxy = [(1, 0), (-1, 0), (0, 1), (0, -1)]
    for dx, dy in dxy:
      nextx = x+dx
      nexty = y+dy
      if(dist[nextx][nexty] >= 0):
        continue
      if(graph[nextx][nexty] == '#'):
        continue
      dist[nextx][nexty] = dist[x][y] + 1
      q.append((nextx, nexty))
  return dist[gx][gy]

ans = 0  
for sx in range(1, H+1):
  for sy in range(1, W+1):
    for gx in range(sx, H+1):
      for gy in range(sy, W+1):
        if(graph[sx][sy] == '.' and graph[gx][gy] == '.'):
          tmp = dfs(sx, sy, gx, gy)
          ans = max(ans, tmp)
          
print(ans)

Fixed code:
def main():
  from collections import deque
  H, W = map(int, input().split())
  graph = [['#'] * (W+2) for _ in range(H+2)]
  for i in range(H):
    S = input()
    for j in range(W):
      graph[i+1][j+1] = S[j]

  def dfs(sx, sy):
    q = deque([(sx, sy)])
    dist = [[-1] * (W+2) for _ in range(H+2)]
    dist[sx][sy] = 0
    s = 0
    while q:
      x, y = q.popleft()
      dxy = [(1, 0), (-1, 0), (0, 1), (0, -1)]
      for dx, dy in dxy:
        nextx = x+dx
        nexty = y+dy
        if(dist[nextx][nexty] >= 0):
          continue
        if(graph[nextx][nexty] == '#'):
          continue
        dist[nextx][nexty] = dist[x][y] + 1
        q.append((nextx, nexty))
    for i in range(H+2):
      for j in range(W+2):
        if(dist[i][j] >= 0):
          s = max(s, dist[i][j])
    return s

  ans = 0  
  for sx in range(1, H+1):
    for sy in range(1, W+1):
      if(graph[sx][sy] == '.'):
          tmp = dfs(sx, sy)
          ans = max(ans, tmp)
          
  print(ans)
  
main()
Buggy code:
import Queue
dx=[1,0,-1,0]
dy=[0,1,0,-1]
H,W=input().split()
S=[]
for h in range(H):
  S.append(input())

def bfs(s):
  dist = [[-1]*H for _ in range(W)]
  
  h,w = s
  dist[h][w]=0
  que = queue.Queue()
  que.put(s)
  
  while not(que.empty):
    h,w = que.get()
    for d in range(4):
      nh = h + dx[d]
      nw = w + dy[d]
      if nh<0 or H<=nh or nw<0 or W<=nw:
        continue
      if S[nh][nw]=="#":
        continue
      if dist[nh][nw]==-1:
        dist[nh][nw]=dist[h][w]+1
        que.put([nh,nw])
  return dist(h,w)

ans = 0
for h in range(H):
  for w in range(W):
    if S[h][w]==".":
      ans = max(ans,bfs([h,w]))
print(ans)
      

    
      

Fixed code:
import queue
dx=[1,0,-1,0]
dy=[0,1,0,-1]
H,W=map(int,input().split())
S=[]
for h in range(H):
  S.append(input())

def bfs(s):
  dist = [[-1]*W for _ in range(H)]
  
  h,w = s
  dist[h][w]=0
  que = queue.Queue()
  que.put(s)
  
  while not(que.empty()):
    h,w = que.get()
    for d in range(4):
      nh = h + dx[d]
      nw = w + dy[d]
      if nh<0 or H<=nh or nw<0 or W<=nw:
        continue
      if S[nh][nw]=="#":
        continue
      if dist[nh][nw]==-1:
        dist[nh][nw]=dist[h][w]+1
        que.put([nh,nw])
  return dist[h][w]

ans = 0
for h in range(H):
  for w in range(W):
    if S[h][w]==".":
      ans = max(ans,bfs([h,w]))
print(ans)
Buggy code:
from collections import deque

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

maze=[list("#"*(w+2))]+[list("#"+input()+"#") for i in range(h)]+[list("#"*(w+2))]
INF=10800 #>50*50
visited=[[-INF]*(w+2) for i in range(h+2)]

ss=[]

for i in range(1,h+1):
  for j in range(1,w+1):
    if maze[i][j]==".":
      ss.append((i,j))



def bfs(maze,visited,sy,sx):#,gy,gx):
  queue=deque([[sy,sx]])
  visited[sy][sx]=0
  while queue:
    [y,x]=queue.popleft()
    for i,j in ((-1,0),(1,0),(0,1),(0,-1)):
      y_new,x_new=y+i,x+j
      if maze[y_new][x_new]=="." and visited[y_new][x_new]==-INF:
        visited[y_new][x_new]=visited[y][x]+1
        queue.append([y_new,x_new])

        
        
ans=0
for i,j in ss:
  visited=[[-INF]*(w+2) for i in range(h+2)]
  bfs(maze,visited,i,j)
  for i in range(1,h+1):
    for j in range(1,w+1):
      ans=max(ans,visited[i][j])
      print(ans)

    
    
print(ans)



Fixed code:
from collections import deque

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

maze=[list("#"*(w+2))]+[list("#"+input()+"#") for i in range(h)]+[list("#"*(w+2))]
INF=10800 #>50*50
visited=[[-INF]*(w+2) for i in range(h+2)]

ss=[]

for i in range(1,h+1):
  for j in range(1,w+1):
    if maze[i][j]==".":
      ss.append((i,j))



def bfs(maze,visited,sy,sx):#,gy,gx):
  queue=deque([[sy,sx]])
  visited[sy][sx]=0
  while queue:
    [y,x]=queue.popleft()
    for i,j in ((-1,0),(1,0),(0,1),(0,-1)):
      y_new,x_new=y+i,x+j
      if maze[y_new][x_new]=="." and visited[y_new][x_new]==-INF:
        visited[y_new][x_new]=visited[y][x]+1
        queue.append([y_new,x_new])

        
        
ans=0
for i,j in ss:
  visited=[[-INF]*(w+2) for i in range(h+2)]
  bfs(maze,visited,i,j)
  for i in range(1,h+1):
    for j in range(1,w+1):
      ans=max(ans,visited[i][j])

    
    
print(ans)
Buggy code:
from collections import deque

h, w = map(int, input().split())
maze = [0] * h
for i in range(h):
    maze[i] = list(input())
# print(maze)
INF = int(1e10 + 7)
d = [[INF] * w for i in range(h)]
dx = [1, 0, -1, 0]
dy = [0, 1, 0, -1]

# que = deque([('x', 'y')])
# print(que)
# print(que[0])

def bfs(x, y):
    dx = [1, 0, -1, 0]
    dy = [0, 1, 0, -1]
    que = deque([(x, y)])
    d[x][y] = 0

    while len(que) != 0:
        val = que.popleft()

        for i in range(4):
            nx = val[0] + dx[i]
            ny = (val[1] + dy[i])
            if (0 <= nx < h and 0 <= ny < w and maze[nx][ny] != '#' and d[nx][ny] == INF):
                que.append((nx, ny))
                d[nx][ny] = d[val[0]][val[1]] + 1
    maxval = 0
    for i in range(h):
        for j in range(w):
            if d[i][j] > maxval and d[i][j] != INF:
                maxval = d[i][j]
    return maxval

maxans = 0
for i in range(h):
    for j in range(w):
        if maze[i][j] != '#':
            if bfs(i, j) > maxans:
                maxans = bfs(i, j)
print(maxans)

Fixed code:
from collections import deque

h, w = map(int, input().split())
maze = [0] * h
for i in range(h):
    maze[i] = list(input())
# print(maze)
INF = int(1e10 + 7)
d = [[INF] * w for i in range(h)]
dx = [1, 0, -1, 0]
dy = [0, 1, 0, -1]

# que = deque([('x', 'y')])
# print(que)
# print(que[0])

def bfs(x, y):
    d = [[INF] * w for i in range(h)]
    dx = [1, 0, -1, 0]
    dy = [0, 1, 0, -1]
    que = deque([(x, y)])
    d[x][y] = 0

    while len(que) != 0:
        val = que.popleft()

        for i in range(4):
            nx = val[0] + dx[i]
            ny = (val[1] + dy[i])
            if (0 <= nx < h and 0 <= ny < w and maze[nx][ny] != '#' and d[nx][ny] == INF):
                que.append((nx, ny))
                d[nx][ny] = d[val[0]][val[1]] + 1
    maxval = 0
    for i in range(h):
        for j in range(w):
            if d[i][j] > maxval and d[i][j] != INF:
                maxval = d[i][j]
    return maxval

maxans = 0
cnt = []
for i in range(h):
    for j in range(w):
        if maze[i][j] != '#':
            if bfs(i, j) > maxans:
                maxans = bfs(i, j)
print(maxans)