Number of walks

Given a directed graph as an adjacency matrix, find the smallest K such that the number of walks of length L grows as O(L^K), or -1 if none exists.

Hard9GraphDynamic programmingCombinatoricsMatrixNo attempts yetTime limit2sMemory limit512 MB

Problem

A walk in a graph is a route that may pass through the same vertex and the same edge more than once. The length of a walk is the number of edges it contains.

Given a graph, write a program that finds the smallest nonnegative integer KK such that the number of walks of length LL is O(LK)O(L^K).

The number of walks being O(LK)O(L^K) means there is a constant CC such that the number of walks of length LL is at most C×LKC \times L^K for every positive integer LL.

Input

The first line contains the number of vertices NN (2N50)(2 \le N \le 50).

Each of the next NN lines gives the edges of the graph as an adjacency matrix. If the jj-th character of the ii-th line is Y, there is an edge from vertex ii to vertex jj; if it is N, there is no such edge. No edge joins a vertex to itself.

Output

Print the smallest nonnegative integer KK that satisfies the condition. If no KK satisfies it, print -1.

Hint

If three vertices are joined to each other in both directions, the number of walks of length LL is 3×2L3 \times 2^L. That value cannot be written as O(LK)O(L^K) for any KK.

In a graph with no cycle, the number of walks of length LL is 0 once LL reaches NN.

If the graph consists only of cycles that share no vertex, the number of walks of length LL equals NN for every LL.