Getting Gold

No attempts yetTime limit1sMemory limit128 MB

Problem

We are building an old-school, back-to-basics computer game: a simple text-based adventure where you walk around a grid, collecting treasure (gold) while avoiding traps.

The game is played on a rectangular grid, and the player is given only very limited information about her surroundings. She may move up, down, left, or right (never diagonally) and may keep moving for as long as she likes — until she falls into a trap.

  • She picks up gold whenever she steps onto a square that contains gold.
  • If she is standing immediately next to (up, down, left, or right of) one or more traps, she "senses a draft," but she cannot tell from which direction it comes or how many traps are near.
  • If she tries to step into a wall, she notices the wall in that direction and stays where she is.

For scoring, we want to know how much gold the player could collect while always being certain that every square she steps into is safe. The map is randomly generated and unknown to her in advance, so she has no prior knowledge of its layout.

Input

The first line contains two integers W and H ($3 \le W, H \le 50$): the width and height of the map. The next H lines each contain W characters describing the map, using these symbols:

  • P — the player's starting position
  • G — a piece of gold
  • T — a trap
  • # — a wall
  • . — ordinary floor

There is exactly one P, and the border of the map is always made of walls.

Output

Print the number of pieces of gold the player can collect without ever risking falling into a trap.