#include <bits/stdc++.h>
#include <unordered_set>

using namespace std;

#define LOG(...) fprintf(stderr, __VA_ARGS__)
//#define LOG(...)
#define FOR(i, a, b) for (int i = (int)(a); i < (int)(b); ++i)
#define RFOR(i, a, b) for (int i = (int)(b - 1); i >= (int)(a); --i)
#define REP(i, n) for (int i = 0; i < (int)(n); ++i)
#define RREP(i, n) for (int i = (int)(n - 1); i >= 0; --i)
#define ALL(a) (a).begin(), (a).end()
#define RALL(a) (a).rbegin(), (a).rend()
#define EXIST(s, e) ((s).find(e) != (s).end())
#define SORT(c) sort(ALL(c))
#define RSORT(c) sort(RALL(c))
#define SZ(a) ((int)(a).size())
#define BIT(x, i) (((x) >> (i)) & 1)
#define SQ(x) ((x) * (x))

typedef long long ll;
typedef unsigned long long ull;
typedef vector<bool> vb;
typedef vector<int> vi;
typedef vector<ll> vll;
typedef vector<vb> vvb;
typedef vector<vi> vvi;
typedef vector<vll> vvll;
typedef vector<char> vc;
typedef vector<vc> vvc;
typedef vector<string> vs;
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;

int N;

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

struct P {
    int x, y;
    int cnt;
    vvb field;
};

void infect(vvb& field) {
    vvb newfield(N, vb(N));
    vvi adjacent(N, vi(N));
    REP(y, N) REP(x, N) {
        REP(i, 8) {
            int sx = x + dx[i], sy = y + dy[i];
            if (0 <= sx && sx < N && 0 <= sy && sy < N && field[sy][sx]) {
                adjacent[y][x]++;
            }
        }
    }
    REP(y, N) REP(x, N) {
        if (field[y][x] && (adjacent[y][x] == 2 || adjacent[y][x] == 3)) {
            newfield[y][x] = true;
        } else if (!field[y][x] && adjacent[y][x] == 3) {
            newfield[y][x] = true;
        } else {
            newfield[y][x] = false;
        }
    }
    field = newfield;
}

int bitfield(const vvb& field) {
    int bit = 0;
    REP(y, N) REP(x, N) {
        bit <<= 1;
        bit |= field[y][x];
    }
    return bit;
}

int count_infected(const vvb& field, int ix, int iy) {
    int cnt = 0;
    REP(y, N) REP(x, N) {
        if (!(x == ix && y == iy) && field[y][x]) cnt++;
    }
    return cnt;
}

int main() {
    while (cin >> N, N) {
        vvb field(N, vb(N));
        int ix, iy;
        REP(y, N) REP(x, N) {
            char c;
            cin >> c;
            // @???infected?????????
            field[y][x] = c != '.';
            if (c == '@') {
                ix = x;
                iy = y;
            }
        }

        vector<vector<unordered_set<int>>> G(N, vector<unordered_set<int>>(N));
        queue<P> que;

        if (count_infected(field, ix, iy) == 0) {
            cout << 0 << endl;
            goto END;
        }
        G[iy][ix].insert(bitfield(field));
        que.push({ix, iy, 0, field});
        while (!que.empty()) {
            P p = que.front(); que.pop();
            REP(i, 8) {
                int sx = p.x + dx[i], sy = p.y + dy[i];
                if (0 <= sx && sx < N && 0 <= sy && sy < N && !p.field[sy][sx]) {
                    vvb field = p.field;
                    swap(field[p.y][p.x], field[sy][sx]);
                    infect(field);
                    int bit = bitfield(field);
                    field[sy][sx] = false;
                    if (!EXIST(G[sy][sx], bit)) {
                        G[sy][sx].insert(bit);
                        if (count_infected(field, sx, sy) == 0) {
                            cout << p.cnt+1 << endl;
                            goto END;
                        }
                        field[sy][sx] = true;
                        que.push({sx, sy, p.cnt+1, field});
                    }
                }
            }
        }
        cout << -1 << endl;
END: {}
    }
}

Measured memory_bytes: 4624