#include <stdio.h>
#include <cmath>
#include <algorithm>
#include <cfloat>
#include <stack>
#include <queue>
#include <vector>
#include <string>
#include <iostream>
#include <set>
#include <map>
typedef long long int ll;
typedef unsigned long long int ull;
#define BIG_NUM 2000000000
#define MOD 1000000007
#define EPS 0.000000001
using namespace std;

struct Info{
	char table[5][6];
	short row,col,step;
};

short H,W;
short diff_row[8] = {-1,-1,-1,0,0,1,1,1},diff_col[8] = {-1,0,1,-1,1,-1,0,1};

map<string,bool> MAP;

bool rangeCheck(short row,short col){
	if(row >= 0 && row <= H-1 && col >= 0 && col <= W-1)return true;
	else{
		return false;
	}
}

string makeString(Info info){
	string ret;
	for(short row = 0; row < H; row++){
		for(short col = 0; col < W; col++){
			ret.append(to_string(info.table[row][col]));
		}
	}
	return ret;
}

bool is_cleard(Info info){

	for(short row = 0; row < H; row++){
		for(short col = 0; col < W; col++){
			if(info.table[row][col] == '#')return false;
		}
	}
	return true;
}

void copyInfo(Info& to,Info from){
	for(short row = 0; row < H; row++){
		for(short col = 0; col < W; col++){
			to.table[row][col] = from.table[row][col];
		}
	}
}

void debug(Info info){
	for(short row = 0;row < H; row++){
		for(short col = 0; col < W; col++){
			printf("%c",info.table[row][col]);
		}
		printf("\n");
	}
	printf("\n\n");
}

void func(){

	MAP.clear();

	Info first;
	for(short row = 0; row < H; row++){
		scanf("%s",first.table[row]);
		for(short col = 0; col < W; col++){
			if(first.table[row][col] == '@'){
				first.row = row;
				first.col = col;
			}
		}
	}

	first.step = 0;
	MAP[makeString(first)] = true;

	queue<Info> Q;
	Q.push(first);

	int adj_row,adj_col,calc_row,calc_col;

	while(!Q.empty()){

		//debug(Q.front());

		if(is_cleard(Q.front())){
			printf("%d\n",Q.front().step);
			return;
		}

		for(short i = 0; i < 8; i++){
			adj_row = Q.front().row + diff_row[i];
			adj_col = Q.front().col + diff_col[i];

			if(rangeCheck(adj_row,adj_col) == false || Q.front().table[adj_row][adj_col] == '#')continue;

			Info next_info;
			copyInfo(next_info,Q.front());

			next_info.table[Q.front().row][Q.front().col] = '.';
			next_info.table[adj_row][adj_col] = '@';

			for(short row = 0; row < H; row++){
				for(short col = 0; col < W; col++){
					if(Q.front().table[row][col] == '#'){
						int count = 0;

						for(short a = 0; a < 8; a++){
							calc_row = row + diff_row[a];
							calc_col = col + diff_col[a];

							if(rangeCheck(calc_row,calc_col) == false)continue;

							if(Q.front().table[calc_row][calc_col] == '#' || next_info.table[calc_row][calc_col] == '@')count++;
						}

						if(count == 2 || count == 3){
							//Do nothing
						}else{
							next_info.table[row][col] = '.';
						}
					}else if(next_info.table[row][col] == '.'){

						int count = 0;

						for(short a = 0; a < 8; a++){
							calc_row = row + diff_row[a];
							calc_col = col + diff_col[a];

							if(rangeCheck(calc_row,calc_col) == false)continue;

							if(Q.front().table[calc_row][calc_col] == '#' || next_info.table[calc_row][calc_col] == '@')count++;
						}

						if(count == 3){
							next_info.table[row][col] = '#';
						}
					}
				}
			}

			string next_state = makeString(next_info);

			auto at = MAP.find(next_state);

			if(at != MAP.end())continue;

			MAP[next_state] = true;

			next_info.step = Q.front().step+1;
			next_info.row = adj_row;
			next_info.col = adj_col;

			Q.push(next_info);
		}
		Q.pop();
	}

	printf("-1\n");
}

int main(){

	while(true){
		scanf("%d",&H);
		if(H == 0)break;

		W = H;
		func();
	}

	return 0;
}

Measured memory_bytes: 6764