8993: Ama no Jaku

内存限制:128 MB 时间限制:1 S
题面:传统 评测方式:文本比较 上传者:
提交:9 通过:9

题目描述

Let me tell you the things that I have been thinking for a long time.
You are given an n×nn\times nn×n matrix AAA with each number in AAA either 000 or 111.
You can do the following operation on AAA any number of times (possibly zero):
  • Choose a row or a column from AAA, and flip it (i.e. all the 000s in that row or column become 111s, and all the 111s become 000s).
Define rir_iri as (Ai1Ai2…Ain‾)2(\overline{A_{i1}A_{i2}\dots A_{in}})_2(Ai1Ai2Ain)2, which means writing all the number in the iii-th row from left to right, forming a binary number rir_iri.
Define cjc_jcj as (A1jA2j…Anj‾)2(\overline{A_{1j}A_{2j}\dots A_{nj}})_2(A1jA2jAnj)2, which means writing all the number in the jjj-th column from top to bottom, forming a binary number cjc_jcj.
Note that rir_iri and cjc_jcj may change after operations.
Determine whether you can achieve min⁡(ri)≥max⁡(cj)\min(r_i)\geq \max(c_j)min(ri)max(cj) by operations. If so, find out the minimal number of operations needed.

输入格式

The first line contains a positive integer nnn (1≤n≤20001\leq n\leq 20001n2000), denoting the size of matrix AAA.
Each of the following nnn lines contains a string consisting of nnn 0and1s, denoting each row of matrix AAA.

输出格式

If it is possible to achieve min⁡(ri)≥max⁡(cj)\min(r_i)\geq \max(c_j)min(ri)max(cj), print the minimal number of operations in a single line. Otherwise, print −1-11.

输入样例 复制

3
100
010
001

输出样例 复制

-1