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 C++14 (GCC 5.4.1) (judge verdict: Wrong Answer)

#define FOR(i,a,b) for (int i=(a);i<(b);i++)
#define RFOR(i,a,b) for (int i=(b)-1;i>=(a);i--)
#define REP(i,n) for (int i=0;i<(n);i++)
#define RREP(i,n) for (int i=(n)-1;i>=0;i--)
#define ALL(x) x.begin(), x.end()
#define OUT(x) cout<<x<<endl;

#define I(x) int x; cin>>x;
#define VI(v,n) vector<int> v; v.resize(n); REP(i,n) cin>>v[i];
#define VINT(x) vector<int> x;
#define LI(x) long long x; cin>>x;
#define VLI(v,n) vector<lint> v; v.resize(n); REP(i,n) cin>>v[i];
#define VLINT(x) vector<long long> x;
#define D(x) double x; cin>>x;
#define VD(v,n) vector<double> v; v.resize(n); REP(i,n) cin>>v[i];
#define VDOUBLE(x) vector<double> x;
#define STR(x) string x; cin>>x;
#define VSTR(v,n) vector<string> v; v.resize(n); REP(i,n) cin>>v[i];
#define VSTRING(x) vector<string> x;

#include <iostream>
#include <algorithm>
#include <cmath>
#include <string>
#include <vector>

using namespace std;

int main()
{
	I(N);
	VSTR(A, N);

	int max_r = 0;
	int count_max_r = 0;
	REP(i, N)
	{
		int r = 0;
		REP(j, N)
		{
			if (A[i][j] == '#')
			{
				r++;
			}
		}
		if (r == N) count_max_r++;
		max_r = max(max_r, r);
	}

	int max_c = 0;
	int count_max_c = 0;
	REP(i, N)
	{
		int c = 0;
		REP(j, N)
		{
			if (A[j][i] == '#')
			{
				c++;
			}
		}
		if (c == N) count_max_c++;
		max_c = max(max_c, c);
	}

	if (max_c == 0 && max_r == 0)
	{
		OUT(-1);
		return 0;
	}

	// both have max of N
	if (max_c == N && max_r == N)
	{
		OUT(N - count_max_c);
	}
	else if (max_r == N)
	{
		OUT(N);
	}
	else if (max_c == N)
	{
		int d = N - count_max_c;
		if (d == 1)
		{
			OUT(2);
		}
		else if(d == 2)
		{
			OUT(4);
		}
	}
	else
	{
		OUT(2*N - max_r);
	}
	
	return 0;
}