import java.util.Scanner

import scala.collection.mutable
import scala.collection.mutable.ArrayBuffer

object Main {
  def main(args: Array[String]) {
    val result = solve(new Scanner(System.in))
    if (result.length > 0) println(result)
  }

  def solve(in: String): String = solve(new Scanner(in))

  def solve(sc: Scanner): String = {
    val N = sc.nextInt
    val S = Array.fill(N)(sc.next.map(_ == '1'))

    var start = -1
    for (i <- 0 until N) {
      if (S(i).count(_ == true) == 1) {
        start = i
      }
    }

    if (start == -1) {
      "-1"
    } else {
      val Q = mutable.Queue[(Int, Int)]()
      val T = ArrayBuffer.fill(N)(0)

      T(start) = 1
      Q += Tuple2(start, 1)

      while (Q.nonEmpty) {
        val (i, t) = Q.dequeue()
        for (next <- S(i).zipWithIndex.filter(_._1).map(_._2)) {
          if (T(next) == 0) {
            T(next) = t + 1
            Q += Tuple2(next, t + 1)
          } else if (T(next) != t - 1 && T(next) != t + 1) {
            return "-1"
          }
        }
      }

      T.max.toString
    }
  }
}
