Problem:
Problem J: Infected Land

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.

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.

Sample Input

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

Output for the Sample Input

0
10
-1
3
2
1
6
4

Solution in Ruby (judge verdict: Runtime Error)

### constant

INF = 1 << 30

DXYS = [
  [ 1, 0], [1, -1], [0, -1], [-1, -1],
  [-1, 0], [-1, 1], [0,  1], [ 1,  1]
]

### subroutines

def tostr(n, bits, pos)
  str = ''
  for k  in (0...(n * n))
    if k == pos
      str += '@'
    else
      str += (bits & (1 << k)) == 0 ? '.' : '#'
    end
    str += "\n" if (k + 1) % n == 0
  end
  str
end

def infection(nn, bits, adjs)
  return $icache[bits] if $icache[bits]

  ibits = bits

  for pos in (0...nn)
    c = 0
    adjs[pos].each{|apos| c += ((bits >> apos) & 1)}

    b = 1 << pos
    if (bits & b) == 0
      ibits |= b if c == 3
    else
      ibits &= ~b if c != 2 && c != 3
    end
  end

  $icache[bits] = ibits
end

### main

loop do
  n = gets.to_i
  break if n == 0

  nn = n * n

  sbits = 0
  spos = 0
  for y in (0...n)
    line = gets.strip
    for x in (0...n)
      pos = y * n + x
      case line[x]
      when '#'
        sbits |= (1 << pos)
      when '@'
        spos = pos
      end
    end
  end
  #p sbits.to_s(2)
  #p spos

  adjs = []
  for y0 in (0...n)
    for x0 in (0...n)
      adj = []
      DXYS.each do |dx, dy|
        x = x0 + dx
        y = y0 + dy
        if x >= 0 && x < n && y >= 0 && y < n
          adj << (y * n + x)
        end
      end
      adjs << adj
    end
  end
  #p adjs

  $icache = {}
  
  dists = nn.times.map{Hash.new{INF}}
  dists[spos][sbits] = 0
  q = [[spos, sbits]]
  min_d = INF
  
  while ! q.empty?
    upos, ubits = q.shift
    ud = dists[upos][ubits]
    #puts tostr(n, ubits, upos); puts
    
    if ubits == 0
      min_d = ud
      break
    end

    nvd = ud + 1

    for vpos in adjs[upos]
      #p [upos, vpos]
      vb = (1 << vpos)
      if (ubits & vb) == 0
        vbits = (infection(nn, (ubits | vb), adjs) & ~vb)
        #p [ubits.to_s(2), vbits.to_s(2)]
        if dists[vpos][vbits] >= nvd
          dists[vpos][vbits] = nvd
          q << [vpos, vbits]
        end
      end
    end
  end

  #p $icache
  #p dists
  puts (min_d >= INF ? -1 : min_d)
end