#include <vector>
#include <algorithm>
#include <map>
#include <string>
#include <queue>
#include <iostream>
using namespace std;

const int dx[] = {-1, -1, -1, 0, 0, 1, 1, 1};
const int dy[] = {-1, 0, 1, -1, 1, -1, 0, 1};

const int INF = 1 << 29;

vector<string> get_next_board(const vector<string> &board) {
    int N = board.size();
    vector<string> res(N, string(N, '.'));

    // vector<string> sample = {".....", "##.@.", "#....", "...#.", "##.##"};
    
    for(int i=0; i<N; i++) {
        for(int j=0; j<N; j++) {
            int cnt = 0;            
            for(int k=0; k<8; k++) {
                int ni = i + dx[k], nj = j + dy[k];
                if(ni < 0 or ni >= N or nj < 0 or nj >= N) continue;
                cnt += (board[ni][nj] == '#' or board[ni][nj] == '@');
            }

            /*
            if(sample == board) {
                fprintf(stderr, "i = %d, j = %d, cnt = %d\n", i, j, cnt);
            }
            */

            if(board[i][j] == '#') {
                if(cnt == 2 or cnt == 3) res[i][j] = '#';
                else res[i][j] = '.';
            }
            else if(board[i][j] == '@') {
                res[i][j] = '@';
            }
            else {
                if(cnt == 3) res[i][j] = '#';
                else res[i][j] = '.';
            }
        }
    }

    /*
    if(sample == board) {
        for(int i=0; i<N; i++) {
            cerr << "# " << res[i] << endl;
        }
        for(int i=0; i<N; i++) {
            cerr << "? " << board[i] << endl;
        }
    }
    */
    return res;
}

int bfs(const vector<string> &board, int ax, int ay) {
    map< vector<string>, int > state;
    int N = board.size();
    state[board] = 0;

    queue< tuple< vector<string>, int, int> > que;
    que.emplace(board, ax, ay);

    int res = INF;
    while(que.size()) {
        vector<string> B; int x, y;
        tie(B, x, y) = que.front(); que.pop();
        int cost = state[B];
        
        int cnt = 0;
        for(int i=0; i<N; i++) {
            for(int j=0; j<N; j++) {
                if(B[i][j] == '#') {
                    cnt = 1;
                    break;
                }
            }
        }
        if(cnt == 0) {
            res = cost;
            break;
        }
        
        for(int k=0; k<8; k++) {
            int nx = x + dx[k], ny = y + dy[k];
            if(nx < 0 or nx >= N or ny < 0 or ny >= N) continue;
            if(B[nx][ny] == '#') continue;
            swap(B[x][y], B[nx][ny]);
            vector<string> NB = get_next_board(B);
            if(!state.count(NB)) {
                state[NB] = cost + 1;
                que.emplace(NB, nx, ny);
            }
            swap(B[x][y], B[nx][ny]);
        }
    }
    return res;
}

int solve_testcase() {
    int N; cin >> N;
    if(N == 0) return 1;

    vector<string> board(N);
    int ax = -1, ay = -1;
    for(int i=0; i<N; i++) {
        cin >> board[i];
        for(int j=0; j<N; j++) {
            if(board[i][j] == '@') {
                ax = i, ay = j;
            }
        }
    }

    int ans = bfs(board, ax, ay);
    cout << (ans == INF ? -1 : ans) << endl;
    return 0;
}

int main() {
    while(!solve_testcase());
    return 0;
}



Measured memory_bytes: 13640