Harryxun/GPT-100-sft-dataset huggingface dataset:
Problem p03601 (python)

Fixed submission:
import fractions as frac
a, b, c, d, e, f = map(int, input().split())
memo=[[(-1, -1)]*1000 for i in range(10000)]
func = lambda t: frac.Fraction(t[0], t[1]) if t[1]>0 else -10
def dp(s, w):
    if w*e<s or w*100+s>f:
        return (-1, -1)
    m=memo[s][w]
    if m[0]>0:
        return m
    p0=(s,w) 
    p1=dp(s, w+a)
    p2=dp(s, w+b)
    p3=dp(s+c, w)
    p4=dp(s+d, w)
    memo[s][w]=max(p0, p1, p2, p3, p4, key = func) if w>0 else max(p1, p2, key = func)
    return memo[s][w]
    
ans=dp(0,0)
print(ans[0]+ans[1]*100, ans[0])

Buggy submission (verdict: Runtime Error):
import fractions as frac
a, b, c, d, e, f = map(int, input().split())
memo=[[(-1, -1)]*10000 for i in range(1000)]
func = lambda t: frac.Fraction(t[0], t[1]) if t[1]>0 else -10
def dp(s, w):
    if w*e<s or w*100+s>f:
        return (-1, -1)
    m=memo[s][w]
    if m[0]>0:
        return memo[s][w]
    p0=(s,w) 
    p1=dp(s, w+a)
    p2=dp(s, w+b)
    p3=dp(s+c, w)
    p4=dp(s+d, w)
    memo[s][w]=max(p0, p1, p2, p3, p4, key = func) if w>0 else max(p1, p2, key = func)
    return memo[s][w]
    
ans=dp(0,0)
print(ans[0]+ans[1]*100, ans[0])

Buggy submission (verdict: Runtime Error):
import fractions as frac
a, b, c, d, e, f = map(int, input().split())
memo=[[(-1, -1)]*10000 for i in range(1000)]
func = lambda t: frac.Fraction(t[0], t[1]  ) 
def dp(s, w):
    if w*e<s or w*100+s>f:
        return (-1, -1)
    m=memo[s][w]
    if m[0]>0:
        return memo[s][w]
    p0=(s,w) 
    p1=dp(s, w+a)
    p2=dp(s, w+b)
    p3=dp(s+c, w)
    p4=dp(s+d, w)
    memo[s][w]=max(p0, p1, p2, p3, p4, key = func) if w>0 else max(p1, p2, key = func)
    return memo[s][w]
    
ans=dp(0,0)
print(ans[0]+ans[1]*100, ans[0])
Problem p03660 (python)

Fixed submission:
from collections import deque
import sys
sys.setrecursionlimit(1000000000)
N = int(input())
edges = [[] for _ in range(N)]
for _ in range(N-1):
    a, b = map(int, input().split())
    edges[a-1].append(b-1)
    edges[b-1].append(a-1)
que = deque([0])
parent = [-1]*N
parent[0] = 0
while que:
    cur = que.pop()
    for nxt in edges[cur]:
        if parent[nxt] < 0:
            parent[nxt] = cur
            que.append(nxt)
cur = N-1
route = [N-1]
while cur:
    cur = parent[cur]
    route.append(cur)
i = len(route)//2
a, b = route[i-1:i+1]
edges[a].remove(b)
edges[b].remove(a)
que = deque([0])
flag = [True]*N
score = 0
flag[0] = False
while que:
    v0 = que.pop()
    score+=1
    for v in edges[v0]:
        if flag[v]:
            que.append(v)
            flag[v] = False
if score>N//2:
    print('Fennec')
else:
    print('Snuke')

Buggy submission (verdict: Runtime Error):
from collections import deque
N = int(input())
edges = [[] for _ in range(N)]
for _ in range(N-1):
    a, b = map(int, input().split())
    edges[a-1].append(b-1)
    edges[b-1].append(a-1)
def dfs(route):
    v0 = route[-1]
    if v0 == 0:
        for v in edges[0]:
            if v == N-1:
                return route+[N-1]
            res = dfs(route+[v])
            if res:
                return res
    if len(edges[v0]) == 1:
        return False
    for v in edges[v0]:
        if v == N-1:
            return route+[N-1]
        if v != route[-2]:
            res = dfs(route+[v])
            if res:
                return res
    return False
route = dfs([0])
i = (len(route)+1)//2 
a, b = route[i-1:i+1]
edges[a].remove(b)
edges[b].remove(a)
que = deque([0])
flag = [True]*N
score = 0
while que:
    v0 = que.pop()
    score+=1
    for v in edges[v0]:
        if flag[v]:
            que.append(v)
            flag[v] = False
if score>N//2:
    print('Fennec')
else:
    print('Snuke')
Problem p03701 (python)

Fixed submission:
n=int(input())
s=[int(input()) for i in range(n)]
memo=[[-1]*20000 for i in range(200)]
def dp(score, i):
    if i==n:
        if score%10==0:
            memo[i][score]=0
            return 0
        else:
            memo[i][score]=score
            return score
    if memo[i][score]>=0:
        return memo[i][score]
    memo[i][score]=max(dp(score+s[i-1], i+1), dp(score, i+1))
    return memo[i][score]
print(dp(0,0))

Buggy submission (verdict: Runtime Error):
n=int(input())
s=[int(input()) for i in range(n)]
memo=[[0]*10000 for i in range(100)]
def dp(score, i):
    if i==n:
        if score%10==0:
            memo[i][score]=0
            return 0
        else:
            memo[i][score]=score
            return score
    if memo[i][score]>0:
        return memo[i][score]
    memo[i][score]=max(dp(score+s[i-1], i+1), dp(score, i+1))
    return memo[i][score]
print(dp(0,0))
Problem p03716 (python)

Fixed submission:
import heapq
n= int(input())
a=[int(i) for i in input().split()]
heap1=[]
heap2=[]
a_max=[]
b_max=[]
s=0
for i in range(n):
    heapq.heappush(heap1, a[i])
    s+=a[i]
a_max.append(s)
for i in range(n):
    heapq.heappush(heap1,a[n+i])
    mini=heapq.heappop(heap1)
    s=s+a[n+i]-mini
    a_max.append(s)
s=0
for i in range(n):
    heapq.heappush(heap2,-a[2*n+i])
    s=s-a[2*n+i]
b_max.append(s)    
for i in range(n):
    heapq.heappush(heap2,-a[2*n-1-i])
    mini=heapq.heappop(heap2)
    s=s-a[2*n-1-i]-mini
    b_max.append(s)
ans=a_max[0]+b_max[len(b_max)-1]
for i in range(n+1):
    res=a_max[i]+b_max[-(i+1)]
    ans=max(ans, res)
print(ans)

Buggy submission (verdict: Runtime Error):
import heapq
n= int(input())
a=[int(i) for i in input().split()]
heap1=[]
heap2=[]
a_max=[]
b_max=[]
s=0
for i in range(n):
    heapq.heappush(heap1, a[i])
    s+=a[i]
a_max.append(s)
for i in range(n):
    heapq.heappush(heap1,a[n+i])
    mini=heapq.heappop(heap1)
    s=s+a[n+i]-mini
    a_max.append(s)
s=0
for i in range(n):
    heapq.heappush(heap2,-a[2*n+i])
    s=s-a[2*n+i]
b_max.append(s)    
for i in range(n):
    heapq.heappush(heap2,-a[2*n-1-i])
    mini=heapq.heappop(heap2)
    s=s-a[2*n-1-i]-mini
    b_max.append(s)
ans=a_max[2]+b_max[-3]
for i in range(n):
    res=a_max[i]+b_max[-(i+1)]
    ans=max(ans, res)
print(ans)
Problem p03722 (python)

Fixed submission:
inf=float('inf')
n, m=map(int, input().split())
abc=[list(map(int, input().split())) for i in range(m)]
abc=[[ai[0]-1, ai[1]-1, -ai[2]] for ai in abc]
d=[inf]*n
d[0]=0
for i in range(n):
    flag=False
    for abci in abc:
        if d[abci[1]]>d[abci[0]]+abci[2]:
            flag=True
            d[abci[1]]=d[abci[0]]+abci[2]
            if i==n-1 and abci[1]==n-1:
                print('inf')
                exit()

print(-d[n-1])

Buggy submission (verdict: Runtime Error):
inf=float('inf')
n, m=map(int, input().split())
abc=[list(map(int, input().split())) for i in range(m)]
abc=[[ai[0]-1, ai[1]-1, -ai[2]] for ai in abc]
d=[inf]*n
d[0]=0
for i in range(n):
    flag=False
    for abci in abc:
        if d[abci[1]]>d[abci[0]]+abci[2]:
            flag=True
            d[abci[1]]=d[abci[0]]+abci[2]
            if i==n-1 and abci[1]=n-1:
                print('inf')
                exit()

print(-d[n-1])
Problem p03791 (python)

Fixed submission:
mod = 10**9+7
N = int(input())
x = list(map(int, input().split()))
ans = 1
cnt = 1
now = 0
while now<N-1 and x[now]>=cnt*2-1:
    cnt+=1
    now+=1
ans*=cnt
ans%=mod
for i in range(1, N):
    cnt-=1
    while now<N-1 and x[now]>=cnt*2-1:
        cnt+=1
        now+=1
    ans*=cnt
    ans%=mod
print(ans)

Buggy submission (verdict: Runtime Error):
mod = 10**9+7
N = int(input())
x = list(map(int, input().split()))
ans = 1
cnt = 1
now = 0
while x[now]>=cnt*2-1:
    cnt+=1
    now+=1
ans*=cnt
ans%=mod
for i in range(1, N):
    cnt-=1
    while now<N-1 and x[now]>=cnt*2-1:
        cnt+=1
        now+=1
    ans*=cnt
    ans%=mod
print(ans)
Problem p03808 (python)

Fixed submission:
def solve():
    N = int(input())
    A = list(map(int, input().split()))
    Sum = sum(A)
    NN = N*(N+1)//2
    if Sum%NN != 0:
        return False
    k = Sum//NN
    cnt = 0
    for i in range(N):
        a = A[i]-A[i-1]
        if (k-a)%N != 0:
            return False
        cnt += (k-a)//N 
        if cnt > k:
            return False
    if cnt != k:
        return False
    for i in range(N):
        if A[i]<k:
            return False
    return True
if solve():
    print('YES')
else:
    print('NO')

Buggy submission (verdict: Runtime Error):

import math
n=int(input())
a=list(map(int, input().split()))
tmp=0
for i in range(n-1):
    if a[i]>(a[i+1]+tmp*n):
        tmp=math.ceil((a[i]-a[i+1])/n)
    a[i+1]+=tmp*n
tmp = a[1]-a[0]
if all(a[i+1]-a[i]==tmp for i in range(1, n-1)):
    print('YES')
else:
    print('NO')
Problem p03866 (python)

Fixed submission:
from scipy.sparse.csgraph import csgraph_from_dense,dijkstra

xs, ys, xt, yt = map(int, input().split())
n = int(input())

p = [(xs,ys,0),(xt,yt,0)]
for i in range(2,n+2):
    p.append(tuple(map(int,input().split())))
edge = [[-1]*(n+2)for i in range(n+2)]
 
for i in range(n+2):
    for j in range(i+1,n+2):
        edge[i][j] = edge[j][i] =  max(((p[i][0]-p[j][0])**2+(p[i][1]-p[j][1])**2)**0.5 - p[i][2] - p[j][2],0.0)

#ここから
G = csgraph_from_dense(edge,null_value=-1)
print(dijkstra(G,indices=0)[1])

Buggy submission (verdict: Runtime Error):
from scipy.sparse.csgraph import csgraph_from_dense,dijkstra
from numpy.linalg import norm
from numpy import array
xs, ys, xt, yt = map(int, input().split())
N = int(input())
p=[(xs, ys, 0), (xt, yt, 0)]
edge = [[-1]*(n+2) for _ in range(n+2)]
edge[0][1] = edge[1][0] = norm(array([xs-xt, ys-yt]))
for i in range(N):
    x, y, r = map(int, input().split())
    for j in range(len(p)):
        x1, y1, r1 = p[j]
        edge[i+2][j] = edge[j][i+2] = max(norm(array([x-x1, y-y1]))-r-r1, 0.0)
    p.append((x, y, r))
G = csgraph_from_dense(edge,null_value=-1)  
print(dijkstra(G,indices=0)[1])
Problem p04000 (python)

Fixed submission:
H, W, N = map(int, input().split())
d = 10**9+1
L = []
S = set()
for _ in range(N):
    a, b = map(int, input().split())
    S.add((a, b))
Map =[[0]*5 for _ in range(5)]
ans = [0]*10
for i in S:
    a, b = i
    for i in range(-2, 3):
        for j in range(-2, 3):
            a1, b1 = a+i, b+j

            if (1<=a1<=H) and (1<=b1<=W):
                if (a1, b1) in S:
                    Map[i+2][j+2] = 1
                else:
                    Map[i+2][j+2] = 0
            else:
                Map[i+2][j+2] = -100
    for i in range(-1, 2):
        for j in range(-1, 2):
            res = 0
            for k in range(3):
                for l in range(3):
                    res+=Map[i+1+k][j+1+l]
            if res>=0:
                ans[res]+=1
for i in range(1, 10)[::-1]:
    ans[i] //=i
ans[0] = (W-2)*(H-2)-sum(ans)
for i in ans:
    print(i)

Buggy submission (verdict: Runtime Error):
import numpy as np
H, W, N = map(int, input().split())
d = 10**9+1
L = []
S = set()
for _ in range(N):
    a, b = map(int, input().split())
    S.add((a, b))
Map = np.zeros([5, 5], dtype = 'int')
ans = [0]*10
for i in S:
    a, b = i
    for i in range(-2, 3):
        for j in range(-2, 3):
            a1, b1 = a+i, b+j"""
            if (1<=a1<=H) and (1<=b1<=W):
                if (a1, b1) in S:
                    Map[i+2, j+2] = 1
                else:
                    Map[i+2, j+2] = 0
            else:
                Map[i+2, j+2] = -100
    for i in range(-1, 2):
        for j in range(-1, 2):
            k = 1#Map[i+1:i+4, j+1:j+4].sum()
            if k>=0:
                ans[k]+=1
for i in range(1, 10)[::-1]:
    ans[i] //=i
ans[0] = (W-2)*(H-2)-sum(ans)
for i in ans:
    print(i)"""
Problem p04015 (python)

Fixed submission:
n, a = map(int, input().split())
x=list(map(lambda x: int(x)-a, input().split()))
k=[[0]*3000 for i in range(60)]
for i in range(3000):
    k[0][i]=0
k[0][x[0]+1500]+=1
k[0][1500]+=1
for i in range(1, n):
    for j in range(3000):
        if 3000>j-x[i]>=0:
            k[i][j]=k[i-1][j-x[i]]+k[i-1][j]
        else:
            k[i][j]=k[i-1][j]
print(k[n-1][1500]-1)

Buggy submission (verdict: Runtime Error):
import math
n = int(input())
s = int(input())
def solve(n, s):
    if n == s:
        return n+1
    for b in range(2, math.floor(n**0.5)+1):
        tmp = n
        res = 0
        while tmp>=b:
            res+=tmp%b
            tmp //=b
        res += tmp
        if res == s:
            return b
    for p in range(int(n**0.5), 0, -1):
        r = s-p
        if r<0:
            continue
        if (n-r)%p == 0:
            b = (n-r)//p
            if r<b and b*b>n:
                return b
    return -1
print(solve(n, s))

Buggy submission (verdict: Runtime Error):
def dp(k, value):
    if k==n:
        if value==0:
            return 1
        else:
            return 0
    if memo[k][value]>=0:
        return memo[k][value]
    memo[k][value]=dp(k+1, value+x[k])+dp(k+1, value)
    return memo[k][value]
print(dp(0, 0)-1)

Buggy submission (verdict: Runtime Error):
n, a =map(int, input().split())
x=list(map(int, input().split()))
x=[i-a for i in x]
memo=[[-1]*110 for i in range(60)]
def dp(k, value):
    if k==n:
        if value==0:
            return 1
        else:
            return 0
    if memo[k][value]>0:
        return memo[k][value]
    memo[k][value]=dp(k+1, value+x[k])+dp(k+1, value)
    return memo[k][value]
print(dp(0, 0)-1)

Buggy submission (verdict: Runtime Error):
n, a =map(int, input().split())
x=list(map(int, input().split()))
x=[i-a for i in x]
memo=[[-1]*110 for i in range(60)]
def dp(k, value):
    if k==n:
        if value==0:
            return 1
        else:
            return 0
    if memo[k][value]>0:
        return memo[k][value]
    memo[k][value]=dp(k+1, value+x[k])+dp(k+1, value)
    print(memo[k][value], k, value)
    return memo[k][value]
print(dp(0, 0)-1)
Problem p02599 (python)

Fixed submission:
from operator import itemgetter
N,Q = map(int, input().split())
C = [int(c) for c in input().split()]
L = [[int(l) for l in input().split()] for _ in range(Q)]

for i in range(Q):
    L[i].append(i)
M = pow(2, len(bin(N))-2)
BIT = [0]*(M+1)
L.sort(key=itemgetter(1))
memo = [0]*(N+1)

ans = [0]*Q
i = 1
j = 0
while i <= N:
    c = C[i-1]
    idx = i
    if i <= L[j][1]:
        if memo[c] > 0:
            k = memo[c]
            while k <= M:
                BIT[k] -= 1
                k += k&(-k)
        memo[c] = i
        while idx <= M:
            BIT[idx] += 1
            idx += idx&(-idx)
    if i == L[j][1]:
        cnt = 0
        n = L[j][1]
        while n > 0:
            cnt += BIT[n]
            n -= n&(-n)
        n = L[j][0]-1
        while n > 0:
            cnt -= BIT[n]
            n -= n&(-n)
        ans[L[j][2]] = cnt
        j += 1
        if j == Q:
            break
        elif L[j][1] == i:
            i -= 1
    i += 1

for a in ans:
    print(a)

Buggy submission (verdict: Runtime Error):
N,Q = map(int, input().split())
C = [int(c) for c in input().split()]

#区間の最大値を求めるセグメント木
seg = [0]*(2*N-1)
    
def update(k, x):
    k += N-1
    seg[k] = 1<<(x-1)
    while k:
        k = (k-1)//2
        seg[k] = (seg[2*k+1] & seg[2*k+2])
        
def query(p, q):
    p += N-1
    q += N-2
    res = 0
    while q-p > 1:
        if p&1 == 0:
            res = (res & seg[p])
        if q&1 == 1:
            res = (res & seg[q])
            q -= 1
        p //= 2
        q = (q-1)//2
    if p == q:
        res = (res & seg[q])
    else:
        res = (res & seg[p] & seg[q])
    return res


for i in range(N):
    update(i, C[i])

for _ in range(Q):
    l, r = map(int, input().split())
    print(query(l, r))
Problem p02618 (python)

Fixed submission:
D = int(input())
C = [int(c) for c in input().split()]
S = [[int(s) for s in input().split()] for _ in range(D)]

T = [0]*D
for i in range(D):
    temp = -1
    for j in range(26):
        if temp < S[i][j]:
            temp = S[i][j]
            idx = j+1
    T[i] = idx

for t in T:
    print(t)

Buggy submission (verdict: Runtime Error):
D = int(input())
C = [int(c) for c in input().split()]
S = [[int(s) for s in input().split()] for _ in range(D)]

T = [0]*26
for i in range(D):
    temp = -1
    for j in range(26):
        if temp < S[i][j]:
            temp = S[i][j]
            idx = j+1
    T[i] = idx

for t in T:
    print(t)
Problem p02642 (python)

Fixed submission:
N = int(input())
A = [int(a) for a in input().split()]

M = 10**6+1
L = [0]*M

for a in A:
    if L[a] >= 1:
        L[a] = 2
        continue
    L[a] = 1
    i = 2
    while a*i < M:
        L[a*i] = 2
        i += 1

ans = 0
for a in A:
    if L[a] == 1:
        ans += 1
print(ans)

Buggy submission (verdict: Runtime Error):
N = int(input())
A = [int(a) for a in input().split()]

M = 10**6+1
L = [0]*M

for a in A:
    if L[a] >= 1:
        L[a] = 2
        continue
    L[a] = 1
    i = 2
    while a*i <= M:
        L[a*i] = 2
        i += 1

ans = 0
for a in A:
    if L[a] == 1:
        ans += 1
print(ans)
Problem p02660 (python)

Fixed submission:
N = int(input())

p = [0]*10**6
for i in range(2, 10**6):
    if p[i] == 0:
        p[i] = 1
        j = 2
        while i*j < 10**6:
            p[i*j] = -1
            j += 1

ans = 0
for i in range(10**6):
    if p[i] < 1:
        continue
    j = 1
    while N%pow(i, j) == 0:
        N //= pow(i, j)
        j += 1
        ans += 1

for i in range(10**6):
    if p[i] < 1:
        continue
    while N%i == 0:
        N //= i
    
if N > 10**6:
    i = 2
    f = True
    while i <= N**0.5:
        if N%i == 0:
            f = False
            break
        i += 1
    if f:
        ans += 1

print(ans)

Buggy submission (verdict: Runtime Error):
N = int(input())

p = [0]*10**6
for i in range(2, 10**6):
    if p[i] == 0:
        p[i] = 1
        j = 2
        while i*j < 10**6:
            p[i*j] = -1
            j += 1

L = []
ans = 0
for i in range(10**6):
    if p[i] < 1:
        continue
    j = 1
    while N%pow(i, j) == 0:
        L.append(pow(i, j))
        N //= pow(i, j)
        j += 1
        ans += 1

L2 = []
i = 2
while i <= N**0.5:
    if N%i == 0:
        L2.append(N, N//i)
    i += 1

if len(L2) == 0 and N > 1 and N not in L:
    ans += 1

print(ans)
Problem p02697 (python)

Fixed submission:
N,M = map(int, input().split())

a = 1
b = N
for i in range(M, 0, -1):
    if i%2 == M%2:
        print(a, a+i)
        a += 1
    else:
        print(b, b-i)
        b -= 1

Buggy submission (verdict: Runtime Error):
N,M = map(int, input().split())

L = [0]*N
j = 1
for i in range(1, M+1):
    while L[j-1] == 1:
        j += 1
    print(j, i+j)
    L[j-1] = 1
    L[i+j-1] = 1
Problem p02744 (python)

Fixed submission:
N = int(input())

L = ["a", "b", "c", "d", "e", "f", "g", "h", "i", "j"]

ans = []
def f(List, n):
    if n == N:
        s = ""
        for l in List:
            s += L[l]
        ans.append(s)
        return None
    for i in range(max(List)+2):
        f(List+[i], n+1)

f([0], 1)        
ans.sort()
for a in ans:
    print(a)

Buggy submission (verdict: Runtime Error):
import copy
N = int(input())

L = ["a", "b", "c", "d", "e", "f", "g", "h", "i", "j"]

ans = []

def f(List):
    if sum(List) == N:
        List.sort(reverse=True)
        s = ""
        for i in range(10):
            s += L[i]*List[i]
        ans.append(s)
    for i in range(9):
        if List[i+1] == 0:
            n = List[i]
            idx = i+1
            break
    for i in range(1, min(n, N-sum(List))+1):
        List2 = copy.copy(List)
        List2[idx] = i
        f(List2)

for i in range(1, N+1):
    List = [0]*10
    List[0] = i
    f(List)

ans.sort()
for a in ans:
    print(a)
Problem p02803 (python)

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

ans = 0
inf = 10**12
for i in range(H):
    for j in range(W):
        if S[i][j] == "#":
            continue
        q = deque()
        q.append((i, j, 0))
        used = [[inf]*W for _ in range(H)]
        used[i][j] = 0
        while q:
            temp = q.popleft()
            k = temp[0]
            l = temp[1]
            d = temp[2]
            if k > 0 and S[k-1][l] == "." and used[k-1][l] > d+1:
                used[k-1][l] = d+1
                q.append((k-1, l, d+1))
            if k < H-1 and S[k+1][l] == "." and used[k+1][l] > d+1:
                used[k+1][l] = d+1
                q.append((k+1, l, d+1))
            if l > 0 and S[k][l-1] == "." and used[k][l-1] > d+1:
                used[k][l-1] = d+1
                q.append((k, l-1, d+1))
            if l < W-1 and S[k][l+1] == "." and used[k][l+1] > d+1:
                used[k][l+1] = d+1
                q.append((k, l+1, d+1))

        for s in range(H):
            for t in range(W):
                if used[s][t] < inf:
                    ans = max(ans, used[s][t])

print(ans)

Buggy submission (verdict: Runtime Error):
H,W = map(int, input().split())
S = [input() for _ in range(H)]

dp = [[10**6]*(H*W) for _ in range(H*W)]
for i in range(H):
    for j in range(W):
        if S[i][j] == "#":
            continue
        p = H*i+j
        if i > 0 and S[i-1][j] == ".":
            dp[p][p-H] = 1
            dp[p-H][p] = 1
        if i < H-1 and S[i+1][j] == ".":
            dp[p][p+H] = 1
            dp[p+H][p] = 1
        if j > 0 and S[i][j-1] == ".":
            dp[p][p-1] = 1
            dp[p-1][p] = 1
        if j < W-1 and S[i][j+1] == ".":
            dp[p][p+1] = 1
            dp[p+1][p] = 1

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

ans = 0
for i in range(H*W):
    for j in range(H*W):
        if dp[i][j] == 10**6:
            continue
        ans = max(ans, dp[i][j])
            
print(ans)

Buggy submission (verdict: Runtime Error):
H,W = map(int, input().split())
S = [input() for _ in range(H)]

dp = [[10**6]*(H*W) for _ in range(H*W)]
for i in range(H):
    for j in range(W):
        if S[i][j] == "#":
            continue
        p = H*i+j
        if i > 0 and S[i-1][j] == ".":
            dp[p][p-H] = 1
            dp[p-H][p] = 1
        if i < H-1 and S[i+1][j] == ".":
            dp[p][p+H] = 1
            dp[p+H][p] = 1
        if j > 0 and S[i][j-1] == ".":
            dp[p][p-1] = 1
            dp[p-1][p] = 1
        if j < W-1 and S[i][j+1] == ".":
            dp[p][p+1] = 1
            dp[p+1][p] = 1

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

ans = 0
for i in range(H*W):
    for j in range(H*W):
        if dp[i][j] == 10**6:
            continue
        ans = max(ans, dp[i][j])
            
print(ans)