Killbots

No attempts yetTime limit1sMemory limit128 MB

Problem

Killbots is a turn based game about staying alive in a room full of killer robots. Nobody knows who built the robots or why they were told to destroy everything in front of them. The one certain thing is that their builder went for quantity over quality, so the robots are many and dim.

The board is a rectangular grid, and every cell holds one of the following.

  • Hero (@): the unlucky soul locked in a room with a pack of robots. In the original game the player walks the hero around the board, but the hero in this problem never moves. Instead the hero destroys every robot and fastbot that steps onto the hero's cell.
  • Robot (+): a mechanical thug that wants to flatten the hero. It has never heard of the Three Laws of Robotics and would crush anyone who tried to explain them. Every turn a robot takes one step toward the hero. A robot in the hero's row or the hero's column steps straight at the hero, otherwise it takes the diagonal step that brings it closer. Written out, if the hero stands on row rhr_h and column chc_h, a robot on row rr and column cc moves to row r+s(rhr)r + s(r_h - r) and column c+s(chc)c + s(c_h - c), where s(x)s(x) is 11 for x>0x > 0, 1-1 for x<0x < 0, and 00 for x=0x = 0.
  • Fastbot (#): a quicker build of the basic robot. It follows the same movement rule but takes two steps for every one step a robot takes. The extra speed came with no extra brains, so a fastbot usually manages only to destroy itself twice as fast.
  • Junkheap (*): the debris left behind when two or more robots or fastbots land on the same cell. All of them are destroyed and a junkheap appears there. Robots and fastbots ignore any junkheap in their path, charge into it, and are destroyed. A junkheap never moves and never goes away.

A turn runs in two phases.

  1. Every robot and every fastbot takes one step, all at the same time.
  2. Every surviving fastbot takes one more step, all at the same time, while the robots stay where they are.

Cells are resolved once all of a phase's steps are taken.

  • Whatever stepped onto the hero's cell is destroyed by the hero.
  • Whatever stepped onto a junkheap is destroyed, and the junkheap stays.
  • If two or more of them stepped onto the same cell, all of them are destroyed and a junkheap appears there. In the second phase a robot already standing on that cell is destroyed with them.

Everything moves at the same time, so entering a cell that another unit leaves in the same phase costs nothing.

Turns repeat until no robot and no fastbot is left. Count how many robots and fastbots the hero destroys.

The figure below is the first turn of one game. The hero stands on E5, and junkheaps sit on D1, F3, G5 and B7.

   A B C D E F G H
1  . . . * . . . .
2  . . . . . . . #
3  . . . . . * . .
4  . . . . + . . .
5  . + . . @ . * .
6  . . . . . . . +
7  . * . . . + . .
8  . + . . . . # .

In the first phase the robot on B5 shares the hero's row and steps right to C5, and the robot on E4 shares the hero's column and steps down onto E5, where the hero destroys it. The robot on B8 steps up and right to C7. The robot on H6 steps up and left to G5, hits the junkheap and is destroyed. The robot on F7 steps to E6, and in the same phase the fastbot on G8 steps to F7, so the two never share a cell. In the second phase the robots stand still and the fastbot on F7 steps to E6, where it crashes into the waiting robot. Both are destroyed and a junkheap is left on E6.

Input

The input holds several test cases, and one test case is one grid. A grid is given as several lines of the same length. One line is one row of the grid, and one character of a line is one cell. An empty cell is ., and the other cells use the characters described above. A line holding a single $ character follows each grid. Exactly one cell of a grid holds @. Each side of a grid is at least 1 and at most 1000 cells.

Output

For each test case print on one line the number of robots and fastbots the hero destroyed by the time the game ends, that is, once every robot and fastbot has been destroyed.