Problem:
Score : 1300 points

Problem Statement

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

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

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

Select two integers i, j (1 ≤ i,\ j ≤ N). Memorize the colors of the squares (i,\ 1), (i,\ 2), ..., (i,\ N) as c_1, c_2, ..., c_N, respectively. Then, repaint the squares (1,\ j), (2,\ j), ..., (N,\ j) with the colors c_1, c_2, ..., c_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

2 ≤ N ≤ 500

a_{ij} is either . or #.

Partial Score

In a test set worth 300 points, N ≤ 3.

Input

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

N
a_{11}...a_{1N}
:
a_{N1}...a_{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.

Sample Input 1

2
#.
.#

Sample Output 1

3

For example, perform the operation as follows:

Select i = 1, j = 2.

Select i = 1, j = 1.

Select i = 1, j = 2.

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

Sample Input 2

2
..
..

Sample Output 2

-1

Sample Input 3

2
##
##

Sample Output 3

0

Sample Input 4

3
.#.
###
.#.

Sample Output 4

2

Sample Input 5

3
...
.#.
...

Sample Output 5

5

Solution in Ruby (2.3.3) (judge verdict: Wrong Answer)

k = gets.to_i
count = 0
s = [[]]
while(k > count)
    a = ""
    a = gets.chomp.split("").map(&:to_s)
    s[count] = a
    count = count + 1
end

black_flg = 0
memo_row = []
#全部黒の列を調べる
for r in 0..k-1
    tmp_row = []
    for c in 0..k-1
        if(s[c][r] == "#")
            black_flg = 1
            tmp_row.push(s[c][r])
        end
    end
    if(tmp_row.length == k)
        memo_row.push(r)
    end 
end

if(black_flg == 0)
    print(-1)
    exit
end
#全部黒の行を調べる
memo_col = []
for c in 0..k-1
    tmp_col = []
    for r in 0..k-1
        if(s[c][r] == "#")
            tmp_col.push(s[c][r])
        end
    end
    if(tmp_col.length == k)
        memo_col.push(c)
    end 
end

#黒があるマス目を探す
tmp_col = -1
tmp_row = -1
for r in 0..k-1
    for c in 0..k-1
        if(s[c][r] == "#")
            tmp_col = c
            tmp_row = r
            break
        end
    end
    if(tmp_col != -1)
        break
    end
end

black_row = 0
for i in 0..k-1
    if(i == tmp_row)
        next
    end
    if(s[tmp_col][i] == "#")
        black_row += 1
    end
end

if(memo_col.length > 0) 
    print(k - memo_row.length)
    exit
end

#いったん塗る
for r in 0..k-1
    for c in 0..k-1
        if(r != tmp_col)
            s[c][r] = s[tmp_col][r]
        end
    end
end

memo_row2 = []
#全部黒の列を調べる
for r in 0..k-1
    tmp_row = []
    for c in 0..k-1
        if(s[c][r] == "#")
            tmp_row.push(s[c][r])
        end
    end
    if(tmp_row.length == k)
        memo_row2.push(r)
    end 
end
if(memo_row2.length > 0)
    result = k - memo_row2.length + 1
    print(result)
    exit
end
#全部黒の列分はひく。
result = k + k - 1 - black_row - memo_row.length
print(result)