#include <vector>
#include <list>
#include <map>
#include <set>
#include <deque>
#include <stack>
#include <bitset>
#include <algorithm>
#include <functional>
#include <numeric>
#include <utility>
#include <sstream>
#include <queue>
#include <iostream>
#include <iomanip>
#include <cstdio>
#include <complex>
#include <cmath>
#include <cstdlib>
#include <cctype>
#include <string>
#include <cstring>
#include <ctime>
#include <fstream>
#include <stdio.h>

using namespace std;

//conversion
//------------------------------------------
inline int toInt(string s) {int v; istringstream sin(s);sin>>v;return v;}
template<class T> inline string toString(T x) {ostringstream sout;sout<<x;return sout.str();}

//typedef
//------------------------------------------
typedef vector<int> VI;
typedef vector<VI> VVI;
typedef vector<double> VEC;
typedef vector<VEC> MAT;
typedef vector<string> VS;
typedef pair<int, int> PII;
typedef pair<int, PII> TIII;
typedef long long LL;
typedef vector<LL> VLL;
typedef vector<VLL> VVLL;
//container util

//------------------------------------------
#define ALL(a)  (a).begin(),(a).end()
#define RALL(a) (a).rbegin(), (a).rend()
#define PB push_back
#define MP make_pair
#define SZ(a) int((a).size())
#define EACH(i,c) for(typeof((c).begin()) i=(c).begin(); i!=(c).end(); ++i)
#define EXIST(s,e) ((s).find(e)!=(s).end())
#define SORT(c) sort((c).begin(),(c).end())
#define MT(a,b,c) MP(a, MP(b, c))
#define T1 first
#define T2 second.first
#define T3 second.second

//repetition
//------------------------------------------
#define FOR(i,a,b) for(int i=(a);i<(b);++i)
#define REP(i,n)  FOR(i,0,n)

//AOJ 1304
int dx[]={-1,-1,-1,0,0,1,1,1};
int dy[]={-1,0,1,-1,1,-1,0,1};

map<LL, int> vsi;
int n;

//0: . 1: #

inline int getx(LL a){return (a>>25 & 7);}
inline int gety(LL a){return (a>>28 & 7);}
inline int getidx(int x, int y){return y*n+x;}
inline int getstate(LL a, int x, int y){return ((a>>getidx(x,y))&1);}
inline LL setstate(LL a, int x, int y, int state){
	if(state){
		return a|(1LL<<getidx(x,y));
	}else{
		LL p=1LL<<getidx(x,y);
		p = ~p;
		return a&p;
	}
}
inline LL setpos(LL a, int x, int y){
	LL ret=a;
	ret &= ((1LL<<25)-1);
	ret |= ((LL)x)<<25;
	ret |= ((LL)y)<<28;
	return ret;
}

inline int count(LL b){
	int ret = 0;
	REP(i,n)REP(j,n)if(getstate(b,i,j))ret++;
	return ret-1;
}

int step(LL &b){
	LL tb = b;
	int ret = 0;
	int x=getx(b);
	int y=gety(b);
	REP(i,n){
		REP(j,n){
			if(y==i&&x==j)continue;
			int cnt = 0;
			REP(k,8){
				int ny=i+dy[k];
				int nx=j+dx[k];
				if(nx<0 || ny<0 || nx>=n || ny>=n)continue;
				if(getstate(tb,nx,ny))cnt++;
			}
			if(getstate(tb,j,i)){
				if(cnt!=2&&cnt!=3)b=setstate(b,j,i,0);
			}else if(!getstate(tb,j,i)){
				if(cnt==3)b=setstate(b,j,i,1);
			}
		}
	}
	return ret;
}

int main(){
	while(cin>>n, n){
		vsi.clear();
		string b;
		REP(i,n){
			string tmp;
			cin>>tmp;
			b+=tmp;
		}
		LL is=0;
		REP(i,n){
			REP(j,n){
				switch(b[i*n+j]){
				case '@':
					is = setpos(is, j, i);
				case '#':
					is |= (1LL<<i*n+j);
					break;
				}
			}
		}
		priority_queue<pair<int, LL>, vector<pair<int, LL> >, greater<pair<int, LL> > > q;
		q.push(MP(0,is));
		int ans=-1;
		while(!q.empty()){
			int x,y;
			pair<int, LL> tmp=q.top();
			q.pop();
			if(EXIST(vsi,tmp.second))continue;
			vsi[tmp.second] = tmp.first;
			if(count(tmp.second)==0){
				ans=tmp.first;
				break;
			}
			x = getx(tmp.second);
			y = gety(tmp.second);
			REP(i,8){
				int nx=x+dx[i];
				int ny=y+dy[i];
				if(nx<0 || ny<0 || nx>=n || ny>=n || getstate(tmp.second, nx, ny)==1)continue;
				LL tb = tmp.second;
				tb=setstate(tb,x,y,0);
				tb=setstate(tb,nx,ny,1);
				tb=setpos(tb,nx,ny);
				step(tb);
				if(EXIST(vsi,tb))continue;
				q.push(MP(tmp.first+1, tb));
			}
		}
		cout<<ans<<endl;
	}
	return 0;
}

Measured memory_bytes: 1708