import std.stdio;
import std.conv, std.array, std.algorithm, std.string;
import std.math, std.random, std.range, std.datetime;
import std.bigint;

int n;
void main(){
	n = readln.chomp.to!int;
	bool[][] xss;
	foreach(j; 0 .. n){
		xss ~= readln.chomp.map!(x => (x == '#')).array;
		}
	
	bool[][][] q, nextq;
	q ~= xss;
	bool[string] doneset;
	for(int d = 0; d < 10; d ++){
		foreach(m; q){
			if(m.isBlack){
				d.writeln;
				return;
				}
			else{
				string k = m.dump;
				if(k in doneset) continue;
				else doneset[k] = 1;
				
				foreach(j0; 0 .. n) foreach(i0; 0 .. n){
					nextq ~= move(m, i0, j0);
					debug foreach(r; m) r.map!(x => (x? '#': '.')).writeln;
					debug [i0, j0].writeln;
					debug foreach(r; nextq[$ - 1]) r.map!(x => (x? '#': '.')).writeln;
					debug nextq[$ - 1].dump.writeln;
					debug "----------".writeln;
					}
				}
			}
		q = nextq;
		nextq = [];
		}
	
	"-1".writeln;
	}

bool[][] move(bool[][] org, int i0, int j0){
	bool[][] res;
	foreach(i; 0 .. n){
		bool[] r;
		foreach(j; 0 .. n){
			if(j == j0) r ~= org[i0][i];
			else r ~= org[i][j];
			}
		res ~= r;
		}
	return res; 
	}

bool isBlack(bool[][] m){
	foreach(r; m) foreach(c; r) if(!c) return 0;
	
	return 1;
	}

string dump(bool[][] m){
	string res;
	foreach(r; m) res ~= r.map!(x => (x? '#': '.')).array;
	return res;
	}