Problem: The earth is under an attack of a deadly virus. Luckily, prompt actions of the Ministry of Health against this emergency successfully confined the spread of the infection within a square grid of areas. Recently, public health specialists found an interesting pattern with regard to the transition of infected areas. At each step in time, every area in the grid changes its infection state according to infection states of its directly (horizontally, vertically, and diagonally) adjacent areas.

* An infected area continues to be infected if it has two or three adjacent infected areas.
* An uninfected area becomes infected if it has exactly three adjacent infected areas.
* An area becomes free of the virus, otherwise.



Your mission is to fight against the virus and disinfect all the areas. The Ministry of Health lets an anti-virus vehicle prototype under your command. The functionality of the vehicle is summarized as follows.

* At the beginning of each time step, you move the vehicle to one of the eight adjacent areas. The vehicle is not allowed to move to an infected area (to protect its operators from the virus). It is not allowed to stay in the same area.
* Following vehicle motion, all the areas, except for the area where the vehicle is in, change their infection states according to the transition rules described above.
Special functionality of the vehicle protects its area from virus infection even if the area is adjacent to exactly three infected areas. Unfortunately, this virus-protection capability of the vehicle does not last. Once the vehicle leaves the area, depending on the infection states of the adjacent areas, the area can be infected.
The area where the vehicle is in, which is uninfected, has the same effect to its adjacent areas as an infected area as far as the transition rules are concerned.



The following series of figures illustrate a sample scenario that successfully achieves the goal.

Initially, your vehicle denoted by @ is found at (1, 5) in a 5 × 5-grid of areas, and you see some infected areas which are denoted by #'s.

<image>

Firstly, at the beginning of time step 1, you move your vehicle diagonally to the southwest, that is, to the area (2, 4). Note that this vehicle motion was possible because this area was not infected at the start of time step 1.

Following this vehicle motion, infection state of each area changes according to the transition rules. The column "1-end" of the figure illustrates the result of such changes at the end of time step 1. Note that the area (3, 3) becomes infected because there were two adjacent infected areas and the vehicle was also in an adjacent area, three areas in total.

In time step 2, you move your vehicle to the west and position it at (2, 3).

Then infection states of other areas change. Note that even if your vehicle had exactly three infected adjacent areas (west, southwest, and south), the area that is being visited by the vehicle is not infected. The result of such changes at the end of time step 2 is as depicted in "2-end".

Finally, in time step 3, you move your vehicle to the east. After the change of the infection states, you see that all the areas have become virus free! This completely disinfected situation is the goal. In the scenario we have seen, you have successfully disinfected all the areas in three time steps by commanding the vehicle to move (1) southwest, (2) west, and (3) east.

Your mission is to find the length of the shortest sequence(s) of vehicle motion commands that can successfully disinfect all the areas.



Input

The input is a sequence of datasets. The end of the input is indicated by a line containing a single zero. Each dataset is formatted as follows.

n
a11 a12 ... a1n
a21 a22 ... a2n
...
an1 an2 ... ann


Here, n is the size of the grid. That means that the grid is comprised of n × n areas. You may assume 1 ≤ n ≤ 5. The rest of the dataset consists of n lines of n letters. Each letter aij specifies the state of the area at the beginning: '#' for infection, '.' for free of virus, and '@' for the initial location of the vehicle. The only character that can appear in a line is '#', '.', or '@'. Among n × n areas, there exists exactly one area which has '@'.

Output

For each dataset, output the minimum number of time steps that is required to disinfect all the areas. If there exists no motion command sequence that leads to complete disinfection, output -1. The output should not contain any other extra character.

Examples

Input

3
...
.@.
...
3
.##
.#.
@##
3
##.
#..
@..
5
....@
##...
#....
...#.
##.##
5
#...#
...#.
#....
...##
..@..
5
#....
.....
.....
.....
..@..
5
#..#.
#.#.#
.#.#.
....#
.#@##
5
..##.
..#..
#....
#....
.#@..
0


Output

0
10
-1
3
2
1
6
4


Input

3
...
.@.
...
3
.##
.#.
@##
3
.
..
@..
5
....@
...
....
...#.
.##
5
...#
...#.
....
...##
..@..
5
....
.....
.....
.....
..@..
5
..#.
.#.#
.#.#.
....#
.#@##
5
..##.
..#..
....
....
.#@..
0


Output

0
10
-1
3
2
1
6
4

Solution (in CPP):

Solution:
#include <iostream>
#include <sstream>
#include <string>
#include <algorithm>
#include <vector>
#include <stack>
#include <queue>
#include <set>
#include <map>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <cmath>
#include <cassert>

using namespace std;

#define FOR(i,k,n) for(int i=(k); i<(int)(n); ++i)
#define REP(i,n) FOR(i,0,n)
#define FORIT(i,c) for(__typeof((c).begin())i=(c).begin();i!=(c).end();++i)

template<class T> void debug(T begin, T end){ for(T i = begin; i != end; ++i) cerr<<*i<<" "; cerr<<endl; }
inline bool valid(int x, int y, int W, int H){ return (x >= 0 && y >= 0 && x < W && y < H); }

typedef long long ll;
const int INF = 100000000;
const double EPS = 1e-8;
const int MOD = 1000000007;
int dx[8] = {1, 0, -1, 0, 1, -1, -1, 1};
int dy[8] = {0, 1, 0, -1, 1, 1, -1, -1};
int N;
inline bool infected(int S, int x, int y){
  return S >> (y * N + x) & 1;
}
int next(int S, int bx, int by){
  int NS = 0;
  for(int y = 0; y < N; y++){
    for(int x = 0; x < N; x++){
      if(bx == x && by == y) continue;
      int cnt = 0;
      for(int r = 0; r < 8; r++){
        if(valid(x + dx[r], y + dy[r], N, N) && infected(S, x + dx[r], y + dy[r]))cnt++;
        if(bx == x + dx[r] && by == y + dy[r]) cnt++;
        if(cnt >= 4) break;
      }
      if(infected(S, x, y) && (cnt == 2 || cnt == 3)) NS |= 1 << (y * N + x);
      if(!infected(S, x, y) && cnt == 3) NS |= 1 << (y * N + x);
    }
  }
  return NS;
}
void output(int bx, int by, int s, int t){
  printf("time = %d:\n", t);
  REP(y, 5){
    REP(x, 5){
      if(x == bx && y == by){
        assert(!infected(s, x, y));
        putchar('@');
      }else if(infected(s, x, y)){
        putchar('#');
      }else{
        putchar('.');
      }
    }
    putchar('\n');
  }
  putchar('\n');
}

int main(){
  while(cin >> N && N){
    vector<string> grid(N);
    REP(i, N) cin >> grid[i];
    int ss = 0, sx = 0, sy = 0;
    REP(y, N) REP(x, N) {
      if(grid[y][x] == '@'){
        sx = x, sy = y;
      }
      if(grid[y][x] == '#'){
        ss |= 1 << (y * N + x);
      }
    }
    queue<int> qs, qx, qy;
    qx.push(sx); qy.push(sy); qs.push(ss);
    int t = 0;
    qx.push(-1); qy.push(-1); qs.push(-1);
    set<int> visit[5][5];
    int ans = -1;
    while(qx.size() > 1){
      int x = qx.front(), y = qy.front(), s = qs.front();
      qx.pop(); qy.pop(); qs.pop();
      if(s == 0){
        ans = t;
        break;
      }
      if(s == -1){
        t++;
        qx.push(-1); qy.push(-1); qs.push(-1);
        continue;
      }
      REP(r, 8){
        int nx = x + dx[r];
        int ny = y + dy[r];
        if(valid(nx, ny, N, N) && !infected(s, nx, ny)){
          int ns = next(s, nx, ny);
          if(visit[ny][nx].insert(ns).second){
            qx.push(nx); qy.push(ny); qs.push(ns);
          }
        }
      }
    }
    cout << ans << endl;
  }
  return 0;
}