import java.util.*

fun main(omkar: Array<String>) {
    val n = readLine()!!.toInt()
    val adj = arrayOf(mutableListOf<Int>()) + Array(n) {
        val line = "0" + readLine()!!
        val res = mutableListOf<Int>()
        for (j in 1..n) {
            if (line[j] == '1') {
                res.add(j)
            }
        }
        res
    }
    val q = LinkedList<Int>()
    q.add(1)
    val dist = IntArray(n + 1) { -1 }
    dist[1] = 0
    while (q.isNotEmpty()) {
        val a = q.removeFirst()
        for (b in adj[a]) {
            if (dist[b] == -1) {
                dist[b] = dist[a] + 1
                q.add(b)
            } else if ((dist[a] + 1 + dist[b]) % 2 != 0) {
                println(-1)
                return
            }
        }
    }
    var answer = 0
    for (r in 1..n) {
        //println("adj[$r] = ${adj[r]}")
        dist.fill(-1)
        dist[r] = 1
        q.add(r)
        while (q.isNotEmpty()) {
            val a = q.removeFirst()
            for (b in adj[a]) {
                if (dist[b] == -1) {
                    dist[b] = dist[a] + 1
                    q.add(b)
                }
            }
        }
        //println("r = $r, dist = ${dist.contentToString()}")
        answer = Math.max(answer, dist.max()!!)
    }
    println(answer)
}