import java.util.Scanner

import scala.collection.{immutable, 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(start: Int, N: Int, S: Array[Seq[Int]]): Int = {
    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)) {
        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
  }

  def solve(sc: Scanner): String = {
    val N = sc.nextInt
    val S: Array[Seq[Int]] = Array.fill(N)(sc.next.zipWithIndex.filter(_._1 == '1').map(_._2))

    val answers = for (i <- 0 until N if (S(i).length == 1)) yield solve(i, N, S)

    if (answers.isEmpty) {
      "-1"
    } else {
      answers.max.toString
    }
  }
}
