Good Bye 2017
H
New Year and Boolean Bridges
Codeforces
3100
原题
← G→
翻译中…

Your friend has a hidden directed graph with $*n*$ nodes.

翻译中…

Let $*f*(*u*, *v*)$ be true if there is a directed path from node $*u*$ to node $*v*$, and false otherwise. For each pair of distinct nodes, $*u*, *v*$, you know at least one of the three statements is true:

翻译中…
  1. image
  2. image
  3. image
翻译中…

Here AND, OR and XOR mean AND, OR and exclusive OR operations, respectively.

翻译中…

You are given an $*n*$ by $*n*$ matrix saying which one of the three statements holds for each pair of vertices. The entry in the $*u*$-th row and $*v*$-th column has a single character.

翻译中…
  1. If the first statement holds, this is represented by the character 'A'.
  2. If the second holds, this is represented by the character 'O'.
  3. If the third holds, this is represented by the character 'X'.
  4. The diagonal of this matrix will only contain the character '-'.
翻译中…

Note that it is possible that a pair of nodes may satisfy multiple statements, in which case, the character given will represent one of the true statements for that pair. This matrix is also guaranteed to be symmetric.

翻译中…

You would like to know if there is a directed graph that is consistent with this matrix. If it is impossible, print the integer -1. Otherwise, print the minimum number of edges that could be consistent with this information.

翻译中…

Input

翻译中…

The first line will contain an integer $*n*$ ($1 ≤ *n* ≤ 47$), the number of nodes.

翻译中…

The next $*n*$ lines will contain $*n*$ characters each: the matrix of what you know about the graph connectivity in the format described in the statement.

翻译中…

Output

翻译中…

Print the minimum number of edges that is consistent with the given information, or -1 if it is impossible.

翻译中…

Note

翻译中…

Sample 1: The hidden graph is a strongly connected graph. We can put all four nodes in a cycle.

翻译中…

Sample 2: One valid graph is $3 → 1 → 2$. For each distinct pair, exactly one of $*f*(*u*, *v*), *f*(*v*, *u*)$ holds.

翻译中…

样例

Input #1
4
-AAA
A-AA
AA-A
AAA-
Output #1
4
InputCopy OutputCopy
Input #2
3
-XX
X-X
XX-
Output #2
2
InputCopy OutputCopy
题目信息
题号
908H
所属比赛
时间限制
5 s
内存限制
512 MB

题面与样例版权归 Codeforces 及出题人所有;中文翻译由 AI 生成(CI 管线),如有出入请以原文为准。