atcoder#MUJINPC2017B. Row to Column

Row to Column

Score : 13001300 points

Problem Statement

There is a square-shaped grid with NN vertical rows and NN horizontal columns. We will denote the square at the ii-th row from the top and the jj-th column from the left as (i, j)(i,\ j).

Initially, each square is either white or black. The initial color of the grid is given to you as characters aija_{ij}, arranged in a square shape. If the square (i, j)(i,\ j) is white, aija_{ij} is .. If it is black, aija_{ij} is #.

You are developing a robot that repaints the grid. It can repeatedly perform the following operation:

  • Select two integers ii, jj (1i, jN1 \leq i,\ j \leq N). Memorize the colors of the squares (i, 1)(i,\ 1), (i, 2)(i,\ 2), ......, (i, N)(i,\ N) as c1c_1, c2c_2, ......, cNc_N, respectively. Then, repaint the squares (1, j)(1,\ j), (2, j)(2,\ j), ......, (N, j)(N,\ j) with the colors c1c_1, c2c_2, ......, cNc_N, respectively.

Your objective is to turn all the squares black. Determine whether it is possible, and find the minimum necessary number of operations to achieve it if the answer is positive.

Constraints

  • 2N5002 \leq N \leq 500
  • aija_{ij} is either . or #.

Partial Score

  • In a test set worth 300300 points, N3N \leq 3.

Input

The input is given from Standard Input in the following format:

NN

a11a_{11}......a1Na_{1N}

::

aN1a_{N1}......aNNa_{NN}

Output

If it is possible to turn all the squares black, print the minimum necessary number of operations to achieve the objective. If it is impossible, print -1 instead.

2
#.
.#
3

For example, perform the operation as follows:

  • Select i=1i = 1, j=2j = 2.
  • Select i=1i = 1, j=1j = 1.
  • Select i=1i = 1, j=2j = 2.

The transition of the colors of the squares is shown in the figure below:

6a0314bb2b1073694a7ef5a062e77b13.png

2
..
..
-1
2
##
##
0
3
.#.
###
.#.
2
3
...
.#.
...
5