from subprocess import*

call(('python3','-c',"""

from numpy import*

from scipy.sparse import*

_,*s=open(0)

s=array([list(map(int,t[:-1]))for t in s])

g=csr_matrix(s)

m=csgraph.floyd_warshall(g)

d=m.max()

i=m[0]%2<1

j=i^True

print(all(s[i][:,i]==0)*all(s[j][:,j]==0)*int(d+1)or-1)

"""))
from numpy import*

from scipy.sparse import*

_,*s=open(0)

s=array([list(map(int,t[:-1]))for t in s])

g=csr_matrix(s)

m=csgraph.floyd_warshall(g)

i=m[0]%2<1

j=i^True

print((all(s[i][:,i]==0)*all(s[j][:,j]==0)*int(m.max()+1)or-1))
from subprocess import *

call(
    (
        "python3",
        "-c",
        """
from numpy import*
from scipy.sparse import*
_,*s=open(0)
s=array([list(map(int,t[:-1]))for t in s])
g=csr_matrix(s)
m=csgraph.floyd_warshall(g)
d=m.max()
i=m[0]%2<1
j=i^True
print(all(s[i][:,i]==0)*all(s[j][:,j]==0)*int(d+1)or-1)
""",
    )
)

from numpy import *
from scipy.sparse import *

_, *s = open(0)
s = array([list(map(int, t[:-1])) for t in s])
g = csr_matrix(s)
m = csgraph.floyd_warshall(g)
i = m[0] % 2 < 1
j = i ^ True
print((all(s[i][:, i] == 0) * all(s[j][:, j] == 0) * int(m.max() + 1) or -1))

-from subprocess import * +from numpy import * +from scipy.sparse import * -call( -    ( -        "python3", -        "-c", -        """ -from numpy import* -from scipy.sparse import* -_,*s=open(0) -s=array([list(map(int,t[:-1]))for t in s]) -g=csr_matrix(s) -m=csgraph.floyd_warshall(g) -d=m.max() -i=m[0]%2<1 -j=i^True -print(all(s[i][:,i]==0)*all(s[j][:,j]==0)*int(d+1)or-1) -""", -    ) -) +_, *s = open(0) +s = array([list(map(int, t[:-1])) for t in s]) +g = csr_matrix(s) +m = csgraph.floyd_warshall(g) +i = m[0] % 2 < 1 +j = i ^ True +print((all(s[i][:, i] == 0) * all(s[j][:, j] == 0) * int(m.max() + 1) or -1))