
n = gets.to_i

s = (1..n).map{|a|gets.chomp.chars.map(&:to_i)}
#######

result = -1



nodes = [nil] + Array.new(n){
  {
    sides: [],
    limit: nil, # nilはallOK, []はallNG
    kotei: nil, # 固定したら
    visit: false,
  }
}
n.times do |i|
  n.times do |ii|
    nodes[i+1][:sides] << ii+1 if s[i][ii] == 1
  end
end



# n.times do |ifirst| #############
ifirst = nil
n.times do |iii|
  if nodes[iii+1][:sides].size == 1
    ifirst = iii
    break
  end
end
ifirst ||= 1
# ifirst

n.times do |i|
  nodes[i+1][:limit] = nil
  nodes[i+1][:kotei] = nil
  nodes[i+1][:visit] = false
end



first = ifirst+1
nodes[first][:limit] = [1]
queue = [first]

# p queue
# pp nodes[1..-1].map.with_index(1){|a,i|[i,a]}



while !queue.empty?
  ind = queue.shift # とるときshift, 入れるとき<<(push)
  # ind = queue.pop

  next if nodes[ind][:visit]

  kotei = nodes[ind][:limit].max # できるだけ大きい値で固定
  if kotei.nil?
    break
  end
  nodes[ind][:kotei] = kotei

  nodes[ind][:sides].each do |side| # sideは周りのnodesのindex
    if nodes[side][:limit].nil?
      nodes[side][:limit] = [nodes[ind][:kotei]-1, nodes[ind][:kotei]+1]
    else
      nodes[side][:limit] = nodes[side][:limit] & [nodes[ind][:kotei]-1, nodes[ind][:kotei]+1]
    end
    queue << side if nodes[side][:visit] == false
  end
  nodes[ind][:visit] = true
  # queue.uniq!

  # p queue
  # pp nodes[1..-1].map.with_index(1){|a,i|[i,a]}
end

if nodes[1..-1].map{|a|a[:kotei]}.none?{|a|a.nil?}
  tmpmax = nodes[1..-1].map{|a|a[:kotei]}.max
  if result < tmpmax
    result = tmpmax
  end
end


# end ##################

# 1 2  3
# 2 -1 4
p result
