#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <cmath>
#include <algorithm>
using namespace std;
#define rep(i,n) for (int i = 0; i < (n); i++)
const int dx[8] = {1,1,1,-1,-1,-1,0,0};
const int dy[8] = {1,-1,0,1,-1,0,1,-1};
struct data
{
	int mask,x,y;
};
int n;
data next_state(int mask, int x, int y)
{
	int T = 0;
	mask |= (1 << (x*n+y));
	rep(i,n)
		rep(j,n)
		{
			int cnt = 0;
			rep(k,8)
			{
				int xx = i + dx[k];
				int yy = j + dy[k];
				if (xx < 0 || xx >= n || yy < 0 || yy >= n)
					continue;
				if (mask & (1 << (xx*n+yy)))
					cnt++;
			}
			if (mask & (1 << (i * n + j)))
			{
				if (cnt == 3 || cnt == 2)
					T |= 1 << (i * n + j);
			}
			else
			{
				if (cnt == 3)
					T |= 1 << (i*n+j);
			}
		}
	T &= ~(1 << (x * n + y));
	return (data){T, x, y};
}
bool dfs(data s, int dep)
{
	if (s.mask == 0)
		return true;
	if (dep == 0)
		return false;
	rep(i,8)
	{
		int xx = s.x + dx[i];
		int yy = s.y + dy[i];
		if (xx < 0 || xx >= n || yy < 0 || yy >= n) continue;
		if (s.mask & (1 << (xx*n+yy)))
			continue;
		if (dfs(next_state(s.mask,xx,yy),dep-1))
			return true;
	}
	return false;
}
char c[10];
int main()
{
	for (; scanf("%d", &n), n;)
	{
		data s = {};
		rep(i,n)
		{
			scanf("%s", c);
			rep(j,n)
			{
				switch(c[j])
				{
					case '@': s.x = i, s.y = j; break;
					case '#': s.mask |= 1 << (i * n + j);break;
				}
			}
		}
		int ans = -1;
		rep(t,11)
			if (dfs(s, t))
			{
				ans = t;
				break;
			}
		printf("%d\n", ans);
	}
	return 0;
}