Buggy python submission (verdict: Time Limit Exceeded):
n=int(input())
edge=[[]for i in range(n+1)]
for i in range(1,n+1):
    s=list(input())
    for j in range(i,n+1):
        if s[j-1]=="1":
            edge[i].append(j)
            edge[j].append(i)



ans=0
for i in range(1,n+1):
    bubun=[[] for iii in range(n+1)]
    bubun[0].append(i)
    check=0
    for j in range(n):
        for k in bubun[j]:
            for y in edge[k]:
                if y in bubun[j-1]:
                    continue
                elif y in bubun[j]:
                    break
                else:
                    bubun[j+1].append(y)
            else:
                continue
            break
        else:
            continue
        break
    else:
        for ii in bubun:
            if ii:
                check+=1
        ans=max(ans,check)
if ans==0:
    print(-1)
else:
    print(ans)
        

Fixed submission:
n=int(input())
edge=[[]for i in range(n+1)]
for i in range(1,n+1):
    s=list(input())
    for j in range(i,n+1):
        if s[j-1]=="1":
            edge[i].append(j)
            edge[j].append(i)



ans=0
for i in range(1,n+1):
    bubun=[[] for iii in range(n+1)]
    bubun[0].append(i)
    check=0
    for j in range(n):
        for k in bubun[j]:
            for y in edge[k]:
                if y in bubun[j-1]:
                    continue
                elif y in bubun[j]:
                    break
                elif y in bubun[j+1]:
                    continue
                else:
                    bubun[j+1].append(y)
            else:
                continue
            break
        else:
            continue
        break
    else:
        for ii in bubun:
            if ii:
                check+=1
        ans=max(ans,check)
if ans==0:
    print(-1)
else:
    print(ans)

Buggy python submission (verdict: Time Limit Exceeded):
n=int(input())
edge=[[]for i in range(n+1)]
for i in range(1,n+1):
    s=list(input())
    for j in range(i,n+1):
        if s[j-1]=="1":
            edge[i].append(j)
            edge[j].append(i)



ans=0
for i in range(1,n+1):
    bubun=[[] for iii in range(n+1)]
    bubun[0].append(i)
    check=0
    for j in range(n):
        for k in bubun[j]:
            for y in edge[k]:
                if y in bubun[j-1]:
                    continue
                elif y in bubun[j]:
                    break
                else:
                    bubun[j+1].append(y)
            else:
                continue
            break
        else:
            continue
        break
    else:
        for ii in bubun:
            if ii:
                check+=1
        ans=max(ans,check)
if ans==0:
    print(-1)
else:
    print(ans)
        

Fixed submission:
n=int(input())
edge=[[]for i in range(n+1)]
for i in range(1,n+1):
    s=list(input())
    for j in range(i,n+1):
        if s[j-1]=="1":
            edge[i].append(j)
            edge[j].append(i)



ans=0
for i in range(1,n+1):
    bubun=[[] for iii in range(n+1)]
    bubun[0].append(i)
    check=0
    for j in range(n):
        for k in bubun[j]:
            for y in edge[k]:
                if y in bubun[j-1]:
                    continue
                elif y in bubun[j]:
                    break
                elif y in bubun[j+1]:
                    continue
                else:
                    bubun[j+1].append(y)
            else:
                continue
            break
        else:
            continue
        break
    else:
        for ii in bubun:
            if ii:
                check+=1
        ans=max(ans,check)
if ans==0:
    print(-1)
else:
    print(ans)