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 Ruby (2.3.3) (judge verdict: Time Limit Exceeded)

def get_int
  gets.chop.to_i
end

def get_ints
  gets.chop.split.map(&:to_i)
end

def get_double
  gets.chop.to_f
end

def get_str
  gets.chop
end

def get_strs
  gets.chop.split
end

def get_max(x1, x2)
  x1 > x2 ? x1 : x2
end

def get_min(x1, x2)
  x1 < x2 ? x1 : x2
end

def main
  n = get_int
  edges_by_node = []
  n.times do |i|
    edges_by_node << get_str.chars.map(&:to_i)
  end
  if not is_bi_graph(n, edges_by_node)
    puts -1
    exit 0
  end

  d = calc_max_distant(n, edges_by_node)
  puts (d + 1)
end

def is_bi_graph(n, edges_by_node)
  colors = [-1] * n

  colors[0] = 0
  stack = [0]
  while stack.size >= 1
    node = stack.pop
    color = colors[node]
    next_color = color == 0 ? 1 : 0
    edges_by_node[node].each_with_index do |cnt, next_node|
      next if cnt == 0
      return false if colors[next_node] == color
      if colors[next_node] == -1
        colors[next_node] = next_color
        stack << next_node
      end
    end
  end
  true
end

INF = 10 ** 5

def calc_max_distant(n, edges_by_node)
  dp = Array.new(n) { [0] * n }
  edges_by_node.each_with_index do |edges, i|
    edges.each_with_index do |e, j|
      if i == j
        dp[i][j] = 0
      else
        dp[i][j] = e == 0 ? INF : 1
      end
    end
  end
  max_d = 0
  (0...n).each do |k|
    (0...n).each do |i|
      (0...n).each do |j|
        d1 = dp[i][j]
        d2 = dp[i][k]
        d3 = dp[k][j]
        d = get_min(d1, d2 + d3)
        dp[i][j] = d
        if d < INF and d > max_d and i != j
          max_d = d
        end
      end
    end
  end
  max_d
end

if __FILE__ == $0
  main
end