fun main(args: Array<String>) {
    val N = readLine()!!.toInt()
    val S = Array(N) { "" }

    for (i in S.indices) {
        S[i] = readLine()!!
    }
    val INF = 1e6.toInt()
    val mat = Array(N) { i -> Array(N) { j -> if (i == j) 0 else INF } }

    for (i in S.indices) for (j in i + 1 until S.size) {
        if (S[i][j] == '1') {
            mat[i][j] = 1
            mat[j][i] = 1
        }
    }

    for (k in mat.indices) for (i in mat.indices) for (j in mat.indices) {
        mat[i][j] = Math.min(mat[i][j], mat[i][k] + mat[k][j])
    }

    var res = -1
    for (k in mat.indices) {
        var r = 0
        var f = true
        for (i in mat.indices) {
            r = Math.max(r, mat[k][i])

            for (j in i + 1 until mat.size) {
                f = f && (mat[k][i] != mat[k][j] || S[i][j] == '0')
            }
        }

        if (f) {
            res = Math.max(res, r + 1)
        }
    }

    println(res)
}
