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: Runtime Error)

#include<iostream>
#include<string>
#include<vector>
#include<set>
#include<algorithm>
#include<queue>
#include<functional>
#include<numeric>
#include<sstream>
#include<math.h>
#include <iomanip>
#define ll long long
#define PLL pair<ll,ll>
#define VS vector<string>
#define VL vector<ll>
#define VB vector<bool>
#define VPLL vector<pair<ll,ll> >
#define VVL vector<vector<ll> >
#define VVVL vector<vector<vector<ll> > >
#define VVB vector<vector<bool> >
#define rep(i,a) for (ll i=0;i<a;i++)
#define nrep(i,n,a) for (ll i=n;i<a;i++)
#define mrep(i,a) for(ll i=a;i>=0;i--)
#define INF 1000000000000000000
#define PI 3.141592653589793238
#define vmin(vec) *std::min_element(vec.begin(),vec.end())
#define vmax(vec) *std::max_element(vec.begin(),vec.end())
#define vsum(vec) std::accumulate(vec.begin(),vec.end(),0LL)
#define ksort(vec) sort(vec.begin(), vec.end(), greater<ll>())
#define ssort(vec) (vec.begin(),vec.end())
#define VPLLsort(vec) sort(vec.begin(), vec.end(),[](PLL &a, PLL &b){ return a.first < b.first; });
#define LTS(n) to_string(n)
#define STL(str) stoll(str)
using namespace std;

ll n, ans = INF;

bool check(VVB &map){
	rep(i, n) rep(j, n) if (map[i][j]) return false;
	return true;
}

bool end(VVB &map){
	rep(i, n) rep(j, n) if (!map[i][j]) return false;
	return true;
}

ll fun(VVB &map){
	ll count = 0;
	ll ans = 0;
	rep(i, n) rep(j, n) if(map[i][j]){
		ans += pow(2,count++);
	}return ans;
}

ll solv(VL &dp,VB &table,ll ii, ll jj, VVB now){
	//cout <<"ii="<< ii << " jj=" << jj << endl;
	//rep(i, n){
		//rep(j, n) cout << now[i][j];
		//cout << endl;
	//}cout << "mapend" << endl;

	if (end(now)) {
		//cout << "end" << endl;
		return 0;
	}
	if (check(now)){
		//cout << "check" << endl;
		return INF;
	}
	VB next(n);
	rep(i, n) next[i] = now[ii][i];
	rep(i, n) now[i][jj] = next[i];
	ll nn = fun(now);
	if (dp[nn] != -1){
		//cout << "dp" << endl;
		return dp[nn];
	}
	if (table[nn]){
		//cout << "table" << endl;
		return INF;
	}
	table[nn] = true;
	ll ret = INF;
	rep(i, n) rep(j, n) if(!(i==ii && j==jj)) ret = min(ret, solv(dp, table, i, j, now));
	return dp[nn] = ret+1;
}

int main() {
	cin >> n;
	VL dp(1 << n*n, -1);
	VB table(1 << n*n, 0);
	string s;
	VVB map(n, VB(n));
	rep(i, n){
		cin >> s;
		rep(j, n) map[i][j] = s[j] == '#';
	}
	ans = INF;
	table[fun(map)] = true;
	rep(i, n) rep(j, n) ans = min(ans, solv(dp, table, i, j, map));
	cout << (ans == INF ? -1 : ans) << endl;
	return 0;
}