Problem:
Score : 500 points

Problem Statement

Given is a connected undirected graph with N vertices and M edges. The vertices are numbered 1 to N, and the edges are described by a grid of characters S.
If S_{i,j} is 1, there is an edge connecting Vertex i and j; otherwise, there is no such edge.

Determine whether it is possible to divide the vertices into non-empty sets V_1, \dots, V_k such that the following condition is satisfied. If the answer is yes, find the maximum possible number of sets, k, in such a division.

Every edge connects two vertices belonging to two "adjacent" sets. More formally, for every edge (i,j), there exists 1\leq t\leq k-1 such that i\in V_t,j\in V_{t+1} or i\in V_{t+1},j\in V_t holds.

Constraints

2 \leq N \leq 200

S_{i,j} is 0 or 1.

S_{i,i} is 0.

S_{i,j}=S_{j,i}

The given graph is connected.

N is an integer.

Input

Input is given from Standard Input in the following format:

N
S_{1,1}...S_{1,N}
:
S_{N,1}...S_{N,N}

Output

If it is impossible to divide the vertices into sets so that the condition is satisfied, print -1.
Otherwise, print the maximum possible number of sets, k, in a division that satisfies the condition.

Sample Input 1

2
01
10

Sample Output 1

2

We can put Vertex 1 in V_1 and Vertex 2 in V_2.

Sample Input 2

3
011
101
110

Sample Output 2

-1

Sample Input 3

6
010110
101001
010100
101000
100000
010000

Sample Output 3

4

Solution in Haskell (GHC 7.10.3) (judge verdict: Accepted)

-- https://github.com/minoki/my-atcoder-solutions
{-# LANGUAGE BangPatterns #-}
import Data.Maybe
import Data.Monoid
import Data.Foldable
import Control.Monad
import Control.Monad.ST
import Control.Monad.Trans.Maybe
import qualified Data.Vector as V
import qualified Data.Vector.Mutable as VM
import qualified Data.Vector.Unboxed as U
import qualified Data.Vector.Unboxed.Mutable as UM
import qualified Data.ByteString.Char8 as BS
import qualified Data.IntSet as IntSet

farthest :: V.Vector IntSet.IntSet -> Int -> Int
farthest graph !origin = U.maximum d
  where
    d = U.create $ do
      let n = V.length graph
      vec <- UM.replicate n (-1 :: Int)
      let bfs !depth !seen !set
            | IntSet.null set = return ()
            | otherwise = do
                forM_ (IntSet.toList set) $ \i -> do
                  UM.write vec i depth
                let seen' = set `IntSet.union` seen
                let next = foldMap_IntSet (graph V.!) set `IntSet.difference` seen'
                bfs (depth + 1) seen' next
      bfs 0 IntSet.empty (IntSet.singleton origin)
      return vec

solve :: V.Vector IntSet.IntSet -> Bool
solve graph = fromMaybe False $ runST $ runMaybeT $ do
  let n = V.length graph
  result <- UM.replicate n (0 :: Int)
  let dfs !p !i !c = do
        c' <- UM.read result i
        case c' of
          0 -> do
            UM.write result i c
            forM_ (IntSet.toList $ graph V.! i) $ \j -> do
              when (j /= p) $ do
                dfs i j (-c)
          _ | c' == c -> return ()
            | otherwise -> mzero
  dfs (-1) 0 1
  return True

main = do
  n <- readLn
  s <- V.replicateM n BS.getLine
  -- s V.! i `BS.index` j
  let graph :: V.Vector IntSet.IntSet
      graph = V.create $ do
        g <- VM.replicate n IntSet.empty
        V.forM_ (V.indexed s) $ \(i,r) -> do
          forM_ [0..n-1] $ \j -> do
            let c = r `BS.index` j
            when (c == '1') $ do
              VM.modify g (IntSet.insert j) i
        return g
  if solve graph then do
    let y = maximum $ map (farthest graph) [0..n-1]
    print (y + 1)
  else
    putStrLn "-1"

foldMap_IntSet :: (Monoid n) => (Int -> n) -> IntSet.IntSet -> n
foldMap_IntSet f set = go set
  where
    go set = case IntSet.splitRoot set of
               [] -> mempty
               [x] -> foldMap f (IntSet.toList x)
               xs -> foldMap go xs