Under construction

Still being built. Things may change

Elephant Show

Interview

Time limit1sMemory limit128 MB

Summary
Count the yellow tiles reachable from the elephant start by moving up, down, left, or right.
Level

Easy3 of 10

Topics
DFS, Matrix
Solved
No attempts yet

Problem

Chiang Mai has a lot of elephants, and elephant camps can be seen all around the city. Elephant camps are among the most popular attractions in Chiang Mai, but Khun-Lung-Ken Elephant Farm is very different from the others.

At Khun-Lung-Ken Elephant Farm you can enjoy shows performed by elephants such as dancing, playing football and painting. The most interesting one is the show where an elephant eats apples on the ground at high speed. It is held on a rectangular ground covered with big square tiles. Each tile is colored either red or yellow, and every tile has one apple on it. The elephant starts on a yellow tile. From the tile it stands on, it moves to one of the four tiles next to it, horizontally or vertically, never diagonally. The elephant will not step on a red tile, it walks only on yellow tiles, and it never picks up an apple placed on a red tile.

Write a program that counts how many apples the elephant can eat by repeating this move.

Input

The input consists of multiple data sets. Each data set starts with a line containing two positive integers WW and HH, the number of tiles in the x direction and the number of tiles in the y direction. Both WW and HH are at most 40.

The next HH lines each contain WW characters. One character gives the state of one tile.

  • .: a yellow tile
  • #: a red tile
  • A: an elephant standing on a yellow tile (it appears exactly once in a data set)

The input ends with a line where both WW and HH are 0.

Output

For each data set, print on one line the number of tiles the elephant can reach from its starting tile. The starting tile is counted as well.

Examples1

  1. Example 1

    Input
    11 5
    .....#.....
    ....#.#....
    ...##A##...
    ..#.....#..
    .#.......#.
    8 6
    ..######
    ..#.....
    .A#.....
    ..#.....
    ..######
    ........
    19 7
    .....#.......#.....
    ....###.....#.#....
    ...#...#...#...#...
    ..#.....#.#.....#..
    .#.......#.......#.
    #........#........#
    #.......A##.......#
    0 0
    
    Expected output
    14
    18
    31