Harryxun/testdata huggingface dataset:
Buggy python submission (verdict: Time Limit Exceeded):
N=int(input())
S=[input() for i in range(N)]
G=[[] for i in range(N)]
for i in range(N):
    for j in range(N):
        if S[i][j]=="1":
            G[i].append(j)
q=[0]
dist=[-1 for i in range(N)]
dist[0]=0
while(q):
    r=q.pop()
    for p in G[r]:
        if dist[p]==-1:
            dist[p]=1-dist[r]
            q.append(p)
for i in range(N):
    for j in G[i]:
        if dist[i]==dist[j]:
            print(-1)
            exit()
#possible
#print(G)
d=[[10**9 for i in range(N)] for i in range(N)]
for i in range(N):
    for j in G[i]:
        d[i][j]=1
    d[i][i]=0

for k in range(N):
    for i in range(N):
        for j in range(N):
            if d[i][j]>d[i][k]+d[k][j]:
                d[i][j]=d[i][k]+d[k][j]
ans=2
for i in range(N):
    for j in range(N):
        ans=max(ans,d[i][j]+1)
print(ans)


Fixed submission:
N=int(input())
S=[input() for i in range(N)]
G=[[] for i in range(N)]
for i in range(N):
    for j in range(N):
        if S[i][j]=="1":
            G[i].append(j)
q=[0]
dist=[-1 for i in range(N)]
dist[0]=0
while(q):
    r=q.pop()
    for p in G[r]:
        if dist[p]==-1:
            dist[p]=1-dist[r]
            q.append(p)
for i in range(N):
    for j in G[i]:
        if dist[i]==dist[j]:
            print(-1)
            exit()
#possible
#print(G)
d=[[10**9 for i in range(N)] for i in range(N)]
for i in range(N):
    for j in G[i]:
        d[i][j]=1
    d[i][i]=0

for k in range(N):
    for i in range(N):
        for j in range(N):
            if d[i][j]>d[i][k]+d[k][j]:
                d[i][j]=d[i][k]+d[k][j]
ans=2
for i in range(N):
    for j in range(N):
        ans=max(ans,d[i][j]+1)
print(ans)
Buggy python submission (verdict: Time Limit Exceeded):
import math
N,L=map(int,input().split())
T=[int(input()) for i in range(N)]
X=0
Y=0
for i in range(N):
    for j in range(i+1,N):
        angle=math.pi*(T[j]+T[i])/L
        #print(math.cos(angle),math.sin(angle))
        X+=math.cos(angle)*(N-2-(j-i-1)*2)
        Y+=math.sin(angle)*(N-2-(j-i-1)*2)
A=(N*(N-1)*(N-2))//6
print(X/A,Y/A)


Fixed submission:
import math
N,L=map(int,input().split())
T=[int(input()) for i in range(N)]
X=0
Y=0
for i in range(N):
    for j in range(i+1,N):
        angle=math.pi*(T[j]+T[i])/L
        #print(math.cos(angle),math.sin(angle))
        X+=math.cos(angle)*(N-2-(j-i-1)*2)
        Y+=math.sin(angle)*(N-2-(j-i-1)*2)
A=(N*(N-1)*(N-2))//6
print(X/A,Y/A)
Buggy python submission (verdict: Time Limit Exceeded):
N=int(input())
A=[int(i) for i in input().split()]
B=[int(i) for i in input().split()]
ans=0
while(True):
    flag=0
    for i in range(N):
        tmp=B[(i-1)%N]+B[(i+1)%N]
        if B[i]>tmp and B[i]>A[i]:
            flag=1
            ans+=1
            B[i]-=tmp
    if flag==0:
        break
flag2=1
for i in range(N):
    if A[i]!=B[i]:
        flag2=0
        break
if flag2:
    print(ans)
else:
    print(-1)


Fixed submission:
N=int(input())
A=[int(i) for i in input().split()]
B=[int(i) for i in input().split()]
ans=0
while(True):
    flag=0
    #print(B)
    for i in range(N):
        tmp=B[(i-1)%N]+B[(i+1)%N]
        if B[i]>tmp>0 and B[i]>A[i]:
            beforeans=ans
            if B[i]%tmp>=A[i]:
                ans+=B[i]//tmp
                B[i]%=tmp
            else:
                k=((A[i]-1-(B[i]%tmp))//tmp)+1
                ans+=B[i]//tmp
                B[i]%=tmp
                #print(i,1,B)
                ans-=k
                B[i]+=k*tmp
                #print(i,2,B)
            if ans!=beforeans:
                flag=1
    if flag==0:
        break
flag2=1
for i in range(N):
    if A[i]!=B[i]:
        flag2=0
        break
if flag2:
    print(ans)
else:
    print(-1)
Buggy python submission (verdict: Time Limit Exceeded):
p=int(input())
a=[int(i) for i in input().split()]
b=[0 for i in range(p)]
#f(i)=ai
#(i,ai) 0<=i<p
#P(x)=sum range(p+1) ai fi(x)/fi(i)
#fi(i)=i!*(N-i-1)!
c=[0 for i in range(p+1)]
c[1]=-1
c[-1]=1
def inv(x):
    return pow(x,p-2,p)
def divide(k):
    #cを(x-k)で割る
    tmp=[c[i] for i in range(p+1)]
    res=[0 for i in range(p)]
    for i in range(p-1,-1,-1):
        res[i]=tmp[i+1]
        tmp[i]+=k*tmp[i+1]
        tmp[i]%=p
        #print(k,tmp,res)
    return res
#devide(i):fi(x)
Fact=[0 for i in range(p)]
Finv=[0 for i in range(p)]
Fact[0]=1
for i in range(p-1):
    Fact[i+1]=((i+1)*Fact[i])%p
Finv[p-1]=inv(Fact[p-1])
for i in range(p-1)[::-1]:
    Finv[i]=((i+1)*Finv[i+1])%p
ans=[0 for i in range(p)]
for i in range(p):
    fix=divide(i)
    fixiinv=(Finv[i]*Finv[p-i-1])%p
    fixiinv*=(-1)**(p-i-1)
    fixiinv%=p
    #print(fix,a[i],fixiinv)
    for j in range(p):
        ans[j]+=(((a[i]*fix[j])%p)*fixiinv)%p
        ans[j]%=p
print(*ans)


Fixed submission:
p=int(input())
a=[int(i) for i in input().split()]
b=[0 for i in range(p)]
#f(i)=ai
#(i,ai) 0<=i<p
#P(x)=sum range(p+1) ai fi(x)/fi(i)
#fi(i)=i!*(N-i-1)!
c=[0 for i in range(p+1)]
c[1]=-1
c[-1]=1
def inv(x):
    return pow(x,p-2,p)
def divide(k):
    #cを(x-k)で割る
    tmp=[c[i] for i in range(p+1)]
    res=[0 for i in range(p)]
    for i in range(p-1,-1,-1):
        res[i]=tmp[i+1]
        tmp[i]+=k*tmp[i+1]
        tmp[i]%=p
        #print(k,tmp,res)
    return res
#devide(i):fi(x)
Fact=[0 for i in range(p)]
Finv=[0 for i in range(p)]
Fact[0]=1
for i in range(p-1):
    Fact[i+1]=((i+1)*Fact[i])%p
Finv[p-1]=inv(Fact[p-1])
for i in range(p-1)[::-1]:
    Finv[i]=((i+1)*Finv[i+1])%p
ans=[0 for i in range(p)]
for i in range(p):
    fix=divide(i)
    fixiinv=(Finv[i]*Finv[p-i-1])%p
    fixiinv*=(-1)**(p-i-1)
    fixiinv%=p
    #print(fix,a[i],fixiinv)
    for j in range(p):
        ans[j]+=(((a[i]*fix[j])%p)*fixiinv)%p
        ans[j]%=p
print(*ans)
Buggy python submission (verdict: Time Limit Exceeded):
class BITree:
    def __init__(self, n):
        self.size = n
        self.tree = [0] * (n + 1)
 
    def total(self, i):
        s = 0
        while i > 0:
            s += self.tree[i]
            i -= i & -i
        return s
 
    def add(self, i, x):
        while i <= self.size:
            self.tree[i] += x
            i += i & -i
N=int(input())
A=[int(input())*(10**5)-i for i in range(N)]
#追加削除
#二分検索
B=[0]+sorted(list(set([i for i in A])))
M=len(B)
D=dict()
for i in range(M):
    D[B[i]]=i
#print(B)
S=BITree(M)
def search(X):
    Y=D[X]
    tmp=S.total(Y)
    if tmp!=S.total(Y-1):
        return Y
    left=0
    right=Y-1
    mid=(left+right)//2
    while(left<=right):
        mid=(left+right)//2
        if S.total(mid)<tmp:
            left=mid+1
        else:
            right=mid-1
    return left
for i in range(N):
    #print([S.total(i+1)-S.total(i) for i in range(M)])
    #print([S.total(i) for i in range(M+1)])
    tmp2=search(A[i])
    if tmp2==0:
        S.add(D[A[i]],1)
    else:
        S.add(D[A[i]],1)
        S.add(tmp2,-1)
print(S.total(M))


Fixed submission:
class BITree:
    def __init__(self, n):
        self.size = n
        self.tree = [0] * (n + 1)
 
    def total(self, i):
        s = 0
        while i > 0:
            s += self.tree[i]
            i -= i & -i
        return s
 
    def add(self, i, x):
        while i <= self.size:
            self.tree[i] += x
            i += i & -i
N=int(input())
A=[int(input())*(10**5)-i for i in range(N)]
#追加削除
#二分検索
B=[0]+sorted(list(set([i for i in A])))
M=len(B)
D=dict()
for i in range(M):
    D[B[i]]=i
#print(B)
S=BITree(M)
def search(X):
    Y=D[X]
    tmp=S.total(Y)
    if tmp!=S.total(Y-1):
        return Y
    left=0
    right=Y-1
    mid=(left+right)//2
    while(left<=right):
        mid=(left+right)//2
        if S.total(mid)<tmp:
            left=mid+1
        else:
            right=mid-1
    return left
for i in range(N):
    #print([S.total(i+1)-S.total(i) for i in range(M)])
    #print([S.total(i) for i in range(M+1)])
    tmp2=search(A[i])
    if tmp2==0:
        S.add(D[A[i]],1)
    else:
        S.add(D[A[i]],1)
        S.add(tmp2,-1)
print(S.total(M))
Buggy python submission (verdict: Time Limit Exceeded):
N,K=map(int,input().split())
mod=10**9+7
Layer=[]
a=1
b=1
while(b<=N):
    a=b
    b=N//(N//a)+1
    Layer.append((a,b))
#print(Layer)
num=[x[1]-x[0] for x in Layer]
M=len(Layer)
#print(num)
dp=[[0 for j in range(M+1)] for i in range(K+1)]
dp[0][0]=1
sdp=[0 for i in range(M+2)]
for k in range(K):
    for j in range(M+1):
        sdp[j+1]=(dp[k][j]+sdp[j])%mod
    for j in range(M):
        dp[k+1][j]=(sdp[M-j]*num[j])%mod
ans=0
for j in range(M+1):
    ans+=dp[K][j]
    ans%=mod
print(ans)


Fixed submission:
N,K=map(int,input().split())
mod=10**9+7
Layer=[]
a=1
b=1
while(b<=N):
    a=b
    b=N//(N//a)+1
    Layer.append((a,b))
#print(Layer)
num=[x[1]-x[0] for x in Layer]
M=len(Layer)
#print(num)
dp=[[0 for j in range(M+1)] for i in range(K+1)]
dp[0][0]=1
sdp=[0 for i in range(M+2)]
for k in range(K):
    for j in range(M+1):
        sdp[j+1]=(dp[k][j]+sdp[j])%mod
    for j in range(M):
        dp[k+1][j]=(sdp[M-j]*num[j])%mod
ans=0
for j in range(M+1):
    ans+=dp[K][j]
    ans%=mod
print(ans)

Buggy python submission (verdict: Runtime Error):
N,K=map(int,input().split())
mod=10**9+7
Layer=[]
a=1
b=1
while(b<N):
    a=b
    b=N//(N//a)+1
    Layer.append((a,b))
#print(Layer)
L=[N//x[0] for x in Layer]
num=[x[1]-x[0] for x in Layer]
M=len(Layer)
#print(num)
dp=[[0 for j in range(M)] for i in range(K+1)]
dp[0][0]=1
sdp=[0 for i in range(M+1)]
for i in range(K):
    for j in range(M):
        sdp[j+1]=(dp[i][j]+sdp[j])%mod
    for j in range(M):
        dp[i+1][j]=(sdp[M-j]*num[j])%mod
ans=0
for j in range(M):
    ans+=dp[K][j]
    ans%=mod
print(ans)

Fixed submission:
N,K=map(int,input().split())
mod=10**9+7
Layer=[]
a=1
b=1
while(b<=N):
    a=b
    b=N//(N//a)+1
    Layer.append((a,b))
#print(Layer)
num=[x[1]-x[0] for x in Layer]
M=len(Layer)
#print(num)
dp=[[0 for j in range(M+1)] for i in range(K+1)]
dp[0][0]=1
sdp=[0 for i in range(M+2)]
for k in range(K):
    for j in range(M+1):
        sdp[j+1]=(dp[k][j]+sdp[j])%mod
    for j in range(M):
        dp[k+1][j]=(sdp[M-j]*num[j])%mod
ans=0
for j in range(M+1):
    ans+=dp[K][j]
    ans%=mod
print(ans)
Buggy python submission (verdict: Time Limit Exceeded):
mod=10**9+7
N,M=map(int,input().split())
S=[int(i) for i in input().split()]
T=[int(i) for i in input().split()]
dp=[[0 for j in range(M)] for i in range(N)]
ans=[[0 for j in range(M+1)] for i in range(N+1)]
for i in range(N+1):
    ans[i][0]=1
for j in range(M+1):
    ans[0][j]=1
for i in range(N):
    for j in range(M):
        if S[i]==T[j]:
            dp[i][j]=ans[i][j]
        else:
            dp[i][j]=0
        ans[i+1][j+1]=dp[i][j]+ans[i+1][j]+ans[i][j+1]-ans[i][j]
        ans[i+1][j+1]%=mod
print(ans[N][M])


Fixed submission:
mod=10**9+7
N,M=map(int,input().split())
S=[int(i) for i in input().split()]
T=[int(i) for i in input().split()]
dp=[[0 for j in range(M)] for i in range(N)]
ans=[[0 for j in range(M+1)] for i in range(N+1)]
for i in range(N+1):
    ans[i][0]=1
for j in range(M+1):
    ans[0][j]=1
for i in range(N):
    for j in range(M):
        if S[i]==T[j]:
            dp[i][j]=ans[i][j]
        else:
            dp[i][j]=0
        ans[i+1][j+1]=dp[i][j]+ans[i+1][j]+ans[i][j+1]-ans[i][j]
        ans[i+1][j+1]%=mod
print(ans[N][M])
Buggy python submission (verdict: Time Limit Exceeded):
H,W=map(int,input().split())
S=[list(input()) for i in range(H)]
Iplus=[[-1 for j in range(W)] for i in range(H)]
Iminus=[[-1 for j in range(W)] for i in range(H)]
Jplus=[[-1 for j in range(W)] for i in range(H)]
Jminus=[[-1 for j in range(W)] for i in range(H)]
for j in range(W):
    if S[0][j]==".":
        Iplus[0][j]=0
for i in range(1,H):
    for j in range(W):
        if S[i][j]==".":
            Iplus[i][j]=Iplus[i-1][j]+1
        else:
            Iplus[i][j]=-1
for j in range(W):
    if S[-1][j]==".":
        Iminus[-1][j]=0
for i in range(H-1)[::-1]:
    for j in range(W):
        if S[i][j]==".":
            Iminus[i][j]=Iminus[i+1][j]+1
        else:
            Iminus[i][j]=-1
for i in range(H):
    if S[i][0]==".":
        Jplus[i][0]=0
for j in range(1,W):
    for i in range(H):
        if S[i][j]==".":
            Jplus[i][j]=Jplus[i][j-1]+1
        else:
            Jplus[i][j]=-1
for i in range(H):
    if S[i][-1]==".":
        Jminus[i][-1]=0
for j in range(W-1)[::-1]:
    for i in range(H):
        if S[i][j]==".":
            Jminus[i][j]=Jminus[i][j+1]+1
        else:
            Jminus[i][j]=-1
ans=0
for i in range(H):
    for j in range(W):
        tmp=Iplus[i][j]+Iminus[i][j]+Jplus[i][j]+Jminus[i][j]+1
        if ans<tmp:
            ans=tmp
            #print(i,j,tmp)
#print(Iplus[2][5],Iminus[2][5],Jplus[2][5],Jminus[2][5])
print(ans)
#for line in Jminus:
#    print(line)
    


Fixed submission:
H,W=map(int,input().split())
S=[list(input()) for i in range(H)]
Iplus=[[-1 for j in range(W)] for i in range(H)]
Iminus=[[-1 for j in range(W)] for i in range(H)]
Jplus=[[-1 for j in range(W)] for i in range(H)]
Jminus=[[-1 for j in range(W)] for i in range(H)]
for j in range(W):
    if S[0][j]==".":
        Iplus[0][j]=0
for i in range(1,H):
    for j in range(W):
        if S[i][j]==".":
            Iplus[i][j]=Iplus[i-1][j]+1
        else:
            Iplus[i][j]=-1
for j in range(W):
    if S[-1][j]==".":
        Iminus[-1][j]=0
for i in range(H-1)[::-1]:
    for j in range(W):
        if S[i][j]==".":
            Iminus[i][j]=Iminus[i+1][j]+1
        else:
            Iminus[i][j]=-1
for i in range(H):
    if S[i][0]==".":
        Jplus[i][0]=0
for j in range(1,W):
    for i in range(H):
        if S[i][j]==".":
            Jplus[i][j]=Jplus[i][j-1]+1
        else:
            Jplus[i][j]=-1
for i in range(H):
    if S[i][-1]==".":
        Jminus[i][-1]=0
for j in range(W-1)[::-1]:
    for i in range(H):
        if S[i][j]==".":
            Jminus[i][j]=Jminus[i][j+1]+1
        else:
            Jminus[i][j]=-1
ans=0
for i in range(H):
    for j in range(W):
        tmp=Iplus[i][j]+Iminus[i][j]+Jplus[i][j]+Jminus[i][j]+1
        if ans<tmp:
            ans=tmp
            #print(i,j,tmp)
#print(Iplus[2][5],Iminus[2][5],Jplus[2][5],Jminus[2][5])
print(ans)
#for line in Jminus:
#    print(line)
Buggy python submission (verdict: Runtime Error):
L,A,B,M=map(int,input().split())
gap=[-1 for k in range(37)]
for k in range(37):
    tmp=10**k-A
    if tmp%B==0:
        gap[k]=-1+tmp//B
    else:
        gap[k]=tmp//B
    gap[k]=min(L-1,gap[k])
    gap[k]=max(-1,gap[k])
num=[0 for k in range(37)]
for k in range(1,37):
    #k-1,k
    num[k]=-gap[k-1]+gap[k]

def egcd(a, b):
    (x, lastx) = (0, 1)
    (y, lasty) = (1, 0)
    while b != 0:
        q = a // b
        (a, b) = (b, a % b)
        (x, lastx) = (lastx - q * x, x)
        (y, lasty) = (lasty - q * y, y)
    return (lastx, lasty, a)
def inv(x):
    return egcd(x,M)[0]
ans=0
geta=0
for k in range(36,-1,-1):
    n=num[k]
    P=A+B*gap[k-1]
    Q=B
    R=pow(10,num[k],M)
    tmp=R*(P*(R-1)*(pow(R,n,M)-1)+n*Q*pow(R,n+1,M)-(n+1)*Q*pow(R,n,M)+Q)
    tmp%=M
    tmp*=pow(10,geta,M)
    tmp*=inv((R-1)**2)
    tmp%=M
    ans+=tmp
    ans%=M
    geta+=k*gap[k]
print(ans)

Fixed submission:
L,A,B,M=map(int,input().split())
K=19
gap=[-1 for k in range(K)]
for k in range(K):
    tmp=10**k-A
    if tmp%B==0:
        gap[k]=-1+tmp//B
    else:
        gap[k]=tmp//B
    gap[k]=min(L-1,gap[k])
    gap[k]=max(-1,gap[k])
num=[0 for k in range(K)]
for k in range(1,K):
    #k-1,k
    num[k]=-gap[k-1]+gap[k]
def matrix(d):
    return [[pow(10,d,M),1,0],[0,1,B],[0,0,1]]
def mattime(P,Q):
    R=[[0,0,0],[0,0,0],[0,0,0]]
    for i in range(3):
        for j in range(3):
            for k in range(3):
                R[i][j]+=P[i][k]*Q[k][j]
                R[i][j]%=M
    return R
def matexp(P,n):
    if n==0:
        return [[1,0,0],[0,1,0],[0,0,1]]
    elif n%2==0:
        return matexp(mattime(P,P),n//2)
    else:
        return mattime(P,matexp(mattime(P,P),n//2))
Mat=[[1,0,0],[0,1,0],[0,0,1]]
for d in range(1,K):
    Mat=mattime(matexp(matrix(d),num[d]),Mat)
print((A*Mat[0][1]+Mat[0][2])%M)
Buggy python submission (verdict: Runtime Error):
N,M=map(int,input().split())
A=[int(i) for i in input().split()]
B=[0 for i in range(M)]
C=[0 for i in range(M)]
X=[]
for i in range(M):
    B[i],C[i]=map(int,input().split())
    X.append((B[i],C[i]))
A.sort()
X.sort(key=lambda x:x[1],reverse=True)
index=0
flag=1
for i in range(M):
    tmp=0
    for j in range(index,index+X[i][0]):
        if A[j]<X[i][1]:
            A[j]=X[i][1]
            tmp+=1
        else:
            flag=0
            break
    if flag==0:
        break
    index+=tmp
print(sum(A))

Fixed submission:
N,M=map(int,input().split())
A=[int(i) for i in input().split()]
B=[0 for i in range(M)]
C=[0 for i in range(M)]
X=[]
for i in range(M):
    B[i],C[i]=map(int,input().split())
    X.append((B[i],C[i]))
A.sort()
X.sort(key=lambda x:x[1],reverse=True)
index=0
flag=1
for i in range(M):
    tmp=0
    for j in range(index,index+X[i][0]):
        if not(0<=j<N):
            break
        if A[j]<X[i][1]:
            A[j]=X[i][1]
            tmp+=1
        else:
            flag=0
            break
    if flag==0:
        break
    index+=tmp
print(sum(A))
Buggy python submission (verdict: Time Limit Exceeded):
import queue
H,W=map(int,input().split())
A=[list(input()) for i in range(H)]
q=queue.Queue()
dist=[[-1 for j in range(W)] for i in range(H)]
for i in range(H):
    for j in range(W):
        if A[i][j]=="#":
            q.put((i,j))
            dist[i][j]=0
dx=[-1,1,0,0]
dy=[0,0,1,-1]
while(not(q.empty())):
    x,y=q.get()
    for k in range(4):
        nx=x+dx[k]
        ny=y+dy[k]
        if not(0<=nx<H and 0<=ny<W):
            continue
        if dist[nx][ny]==-1:
            dist[nx][ny]=dist[x][y]+1
            q.put((nx,ny))
print(max([max(i) for i in dist]))


Fixed submission:
from collections import deque
H,W=map(int,input().split())
A=[list(input()) for i in range(H)]
q=deque([])
dist=[[-1 for j in range(W)] for i in range(H)]
for i in range(H):
    for j in range(W):
        if A[i][j]=="#":
            q.append((i,j))
            dist[i][j]=0
dx=[-1,1,0,0]
dy=[0,0,1,-1]
while(len(q)>0):
    x,y=q.popleft()
    for k in range(4):
        nx=x+dx[k]
        ny=y+dy[k]
        if not(0<=nx<H and 0<=ny<W):
            continue
        if dist[nx][ny]==-1:
            dist[nx][ny]=dist[x][y]+1
            q.append((nx,ny))
print(max([max(i) for i in dist]))

Buggy python submission (verdict: Time Limit Exceeded):
import queue
H,W=map(int,input().split())
A=[list(input()) for i in range(H)]
q=queue.Queue()
dist=[[-1 for j in range(W)] for i in range(H)]
for i in range(H):
    for j in range(W):
        if A[i][j]=="#":
            q.put((i,j))
            dist[i][j]=0
dx=[-1,1,0,0]
dy=[0,0,1,-1]
while(not(q.empty())):
    x,y=q.get()
    for k in range(4):
        nx=x+dx[k]
        ny=y+dy[k]
        if not(0<=nx<H and 0<=ny<W):
            continue
        if dist[nx][ny]==-1:
            dist[nx][ny]=dist[x][y]+1
            q.put((nx,ny))
print(max([max(i) for i in dist]))


Fixed submission:
from collections import deque
H,W=map(int,input().split())
A=[list(input()) for i in range(H)]
q=deque([])
dist=[[-1 for j in range(W)] for i in range(H)]
for i in range(H):
    for j in range(W):
        if A[i][j]=="#":
            q.append((i,j))
            dist[i][j]=0
dx=[-1,1,0,0]
dy=[0,0,1,-1]
while(len(q)>0):
    x,y=q.popleft()
    for k in range(4):
        nx=x+dx[k]
        ny=y+dy[k]
        if not(0<=nx<H and 0<=ny<W):
            continue
        if dist[nx][ny]==-1:
            dist[nx][ny]=dist[x][y]+1
            q.append((nx,ny))
print(max([max(i) for i in dist]))
Buggy python submission (verdict: Time Limit Exceeded):
mod=10**9+7
def gcd(x,y):
    if x<y:
        x,y=y,x
    #x>y
    if x%y==0:
        return y
    else:
        return gcd(y,x%y)
def egcd(a, b):
    (x,lastx)=(0,1)
    (y,lasty)=(1,0)
    while b != 0:
        q = a // b
        (a, b) = (b, a % b)
        (x, lastx) = (lastx - q * x, x)
        (y, lasty) = (lasty - q * y, y)
    return (lastx, lasty, a)
def inv(x):
    return egcd(x,mod)[0]%mod
N=2*10**5+10
B,W=map(int,input().split())
Blist=[0 for i in range(N+1)]
Wlist=[0 for i in range(N+1)]
Fact=[0 for i in range(N+1)]
Finv=[0 for i in range(N+1)]
Exp2=[0 for i in range(N+1)]
Fact[0]=1;Finv[0]=1
Exp2[0]=1
for i in range(N):
    Exp2[i+1]=(Exp2[i]*2)%mod
for i in range(N):
    Fact[i+1]=(Fact[i]*(i+1))%mod
    Finv[i+1]=inv(Fact[i+1])
def C(n,k):
    if k<0 or n<k:
        return 0
    else:
        return (Finv[k]*(Fact[n]*Finv[n-k])%mod)%mod
Blist[0]=1;Wlist[0]=1
for i in range(N):
    Blist[i+1]=(2*Blist[i]-C(i,B-1))%mod
    Wlist[i+1]=(2*Wlist[i]-C(i,W-1))%mod
#print(Blist[:B+W])
#print(Wlist[:B+W])
ans=[0 for i in range(B+W)]
for i in range(B+W):
    ans[i]=(Blist[i]-Wlist[i]+Exp2[i])%mod
    ans[i]=(ans[i]*inv(Exp2[i+1]))%mod
for line in ans:
    print(line)


Fixed submission:
mod=10**9+7
def gcd(x,y):
    if x<y:
        x,y=y,x
    #x>y
    if x%y==0:
        return y
    else:
        return gcd(y,x%y)
def egcd(a, b):
    (x,lastx)=(0,1)
    (y,lasty)=(1,0)
    while b != 0:
        q = a // b
        (a, b) = (b, a % b)
        (x, lastx) = (lastx - q * x, x)
        (y, lasty) = (lasty - q * y, y)
    return (lastx, lasty, a)
def inv(x):
    return egcd(x,mod)[0]%mod
N=2*10**5+10
B,W=map(int,input().split())
Blist=[0 for i in range(N+1)]
Wlist=[0 for i in range(N+1)]
Fact=[0 for i in range(N+1)]
Finv=[0 for i in range(N+1)]
Exp2=[0 for i in range(N+1)]
Fact[0]=1;Finv[0]=1
Exp2[0]=1
for i in range(N):
    Exp2[i+1]=(Exp2[i]*2)%mod
for i in range(N):
    Fact[i+1]=(Fact[i]*(i+1))%mod
    Finv[i+1]=inv(Fact[i+1])
def C(n,k):
    if k<0 or n<k:
        return 0
    else:
        return (Finv[k]*(Fact[n]*Finv[n-k])%mod)%mod
Blist[0]=1;Wlist[0]=1
for i in range(N):
    Blist[i+1]=(2*Blist[i]-C(i,B-1))%mod
    Wlist[i+1]=(2*Wlist[i]-C(i,W-1))%mod
#print(Blist[:B+W])
#print(Wlist[:B+W])
ans=[0 for i in range(B+W)]
for i in range(B+W):
    ans[i]=(Blist[i]-Wlist[i]+Exp2[i])%mod
    ans[i]=(ans[i]*inv(Exp2[i+1]))%mod
for line in ans:
    print(line)
Buggy python submission (verdict: Runtime Error):
S=input();print(2*min(S.count("0")-S.count("1")))

Fixed submission:
S=input()
N=len(S)
X=0
Y=0
for i in S:
    if i=="0":
        X+=1
    else:
        Y+=1
print(N-abs(X-Y))
Buggy python submission (verdict: Runtime Error):
def gcd(a,b):
    x,y=max(a,b),min(a,b)
    if x%y==0:
        return y
    else:
        return (y,x%y)

N=int(input())
A=[int(i) for i in input().split()]
ans=A[0]
for i in range(1,N):
    ans=gcd(ans,A[i])
print(ans)


Fixed submission:
def gcd(a,b):
    x,y=max(a,b),min(a,b)
    if x%y==0:
        return y
    else:
        return gcd(y,x%y)
N=int(input())
A=[int(i) for i in input().split()]
ans=A[0]
for i in range(1,N):
    ans=gcd(ans,A[i])
print(ans)
Buggy python submission (verdict: Runtime Error):
N,M=map(int,input().split())
A=sorted([int(i) for i in input().split()],reverse=True)
num=[6,2,5,5,4,5,6,3,7,6]
INF=10**15
dp=[-INF for i in range(N+1)];dp[0]=0
for i in range(1,N+1):
    for j in range(M):
        dp[i]=max(dp[i],dp[i-num[A[j]]]+1)
K=N
i=0
ans=""
while(K>0):
    if dp[K-num[A[i]]]==dp[K]-1:
        ans=ans+str(A[i])
        K-=num[A[i]]
    else:
        i+=1
print(ans)


Fixed submission:
N,M=map(int,input().split())
A=sorted([int(i) for i in input().split()],reverse=True)
num=[6,2,5,5,4,5,6,3,7,6]
INF=10**15
dp=[-INF for i in range(N+1)];dp[0]=0
for i in range(1,N+1):
    for j in range(M):
        if N>=i-num[A[j]]>=0:
            dp[i]=max(dp[i],dp[i-num[A[j]]]+1)
K=N
i=0
ans=""
while(K>0):
    if not(N>=K-num[A[i]]>=0):
        i+=1
        continue
    if dp[K-num[A[i]]]==dp[K]-1:
        ans=ans+str(A[i])
        K-=num[A[i]]
    else:
        i+=1
print(ans)