// https://atcoder.jp/contests/agc039/tasks/agc039_b
//
#![allow(unused_imports)]
use std::io::*;
use std::fmt::*;
use std::str::*;
use std::cmp::*;
use std::collections::*;

macro_rules! input {
    (source = $s:expr, $($r:tt)*) => {
        let mut iter = $s.split_whitespace();
        input_inner!{iter, $($r)*}
    };
    ($($r:tt)*) => {
        let s = {
            use std::io::Read;
            let mut s = String::new();
            std::io::stdin().read_to_string(&mut s).unwrap();
            s
        };
        let mut iter = s.split_whitespace();
        input_inner!{iter, $($r)*}
    };
}

macro_rules! input_inner {
    ($iter:expr) => {};
    ($iter:expr, ) => {};

    ($iter:expr, $var:ident : $t:tt $($r:tt)*) => {
        let $var = read_value!($iter, $t);
        input_inner!{$iter $($r)*}
    };
}

macro_rules! read_value {
    ($iter:expr, ( $($t:tt),* )) => {
        ( $(read_value!($iter, $t)),* )
    };

    ($iter:expr, [ $t:tt ; $len:expr ]) => {
        (0..$len).map(|_| read_value!($iter, $t)).collect::<Vec<_>>()
    };

    ($iter:expr, [ next / $t:tt ]) => {
        {
            let len = read_value!($iter, usize);
            (0..len).map(|_| read_value!($iter, $t)).collect::<Vec<_>>()
        }
    };

    ($iter:expr, chars) => {
        read_value!($iter, String).chars().collect::<Vec<char>>()
    };

    ($iter:expr, usize1) => {
        read_value!($iter, usize) - 1
    };

    ($iter:expr, $t:ty) => {
        $iter.next().unwrap().parse::<$t>().expect("Parse error")
    };
}

#[allow(unused_macros)]
macro_rules! dvec {
    ($t:expr ; $len:expr) => {
        vec![$t; $len]
    };

    ($t:expr ; $len:expr, $($rest:expr),*) => {
        vec![dvec!($t; $($rest),*); $len]
    };
}

#[allow(unused_macros)]
macro_rules! ifv {
    ($t:expr, $a:expr, $b: expr) => {
        if $t { $a } else { $b }
    }
}

#[allow(unused_macros)]
macro_rules! fill {
    ($t:expr, $v:expr) => {
        for i in 0..$t.len() {
            $t[i] = $v;
        }
    };
}

#[allow(unused_macros)]
macro_rules! join {
    ($t:expr, $glue:expr) => {
        $t.into_iter().map(|w| w.to_string()).collect::<Vec<_>>().join($glue)
    };
}

#[allow(unused_macros)]
macro_rules! debug {
    ($($a:expr),*) => {
        println!(concat!($(stringify!($a), " = {:?}, "),*), $($a),*);
    }
}

fn dfs(g: &Vec<Vec<usize>>, color: &mut Vec<i32>, c: i32, now: usize) -> bool {
    if color[now] != 0 {
        return color[now] == c;
    }
    color[now] = c;
    let mut ok = true;
    for &to in &g[now] {
        ok &= dfs(g, color, -c, to);
    }
    ok
}

fn solve(g: &Vec<Vec<usize>>, root: usize) -> i32 {
    let n = g.len();
    let mut dp = vec![-1; n];
    dp[root] = 1;
    let mut vecd = VecDeque::new();
    vecd.push_back(root);
    while let Some(v) = vecd.pop_front() {
        let head = dp[v];
        for &to in &g[v] {
            if dp[to] == -1 {
                dp[to] = head+1;
                vecd.push_back(to);
            }
        }
    }
    let mut ans = 0;
    for i in 0..n {
        ans = max(ans, dp[i]);
    }
    ans
}

fn main() {
    input! {
        n: usize,
        table: [chars; n]
    };

    let mut graph = vec![vec![]; n];
    for i in 0..n {
        for j in 0..n {
            if table[i][j] == '1' {
                graph[i].push(j);
            }
        }
    }
    let mut color = vec![0; n];
    let biparate = dfs(&graph, &mut color, 1, 0);

    if biparate {
        let mut best = 0;
        for i in 0..n {
            best = max(best, solve(&graph, i));
        }
        println!("{}", best);
    } else {
        println!("{}", -1);
    }
}


Measured memory_bytes: 4352