def bipartite_graph?(n, graph)
  color = Array.new(n, 0)

  dfs = ->(v, c) {
    color[v] = c
    graph[v].each_with_index do |path_exist, nv|
      next if path_exist == 0
      case color[nv]
      when c
        return false
      when 0
        return false unless dfs.call(nv, -1 * c)
      end
    end
  }

  n.times do |i|
    if color[i] == 0
      return false if !dfs.call(i, 1)
    end
  end
  true
end

def warshall_floyd(n, graph)
  d = Array.new(n) { Array.new(n) }
  n.times do |i|
    n.times do |j|
      if i == j
        d[i][j] = 0
      elsif graph[i][j] == 1
        d[i][j] = 1
      else
        d[i][j] = 10 ** 9 # INF
      end
    end
  end

  n.times do |k|
    n.times do |i|
      n.times do |j|
        d[i][j] = [d[i][j], d[i][k] + d[k][j]].min
      end
    end
  end

  d
end

n = gets.to_i
graph = []
n.times do |i|
  graph << s.gets.chomp.split(//).map(&:to_i)
end

unless bipartite_graph?(n, graph)
  puts -1
  exit
end


d = warshall_floyd(n, graph)
max = 0
d.each do |costs|
  max = [max, costs.max].max
end

puts max + 1