Picking up coins

In a n×mn \times m board there are golden coins and some traps. There are also some pieces: bishops and knights, which move according to chess rules. The pieces can move as many times as you wish, and can cross any square that does not have a trap, even if occupied by another piece. Coins dissapear when some piece picks them up.

Write a program that prints the total number of coins that can be picked up.

Input

Input includes several cases. Each case consists of a line with nn and mm, followed by nn lines with mm characteres each one. A ‘B’ indicates a bishop. A ‘K’ indicates a knight. A ‘T’ indicates a trap. A dot indicates an empty square. A digit indicates a number of golden coins. Both nn and mm are between 1 and 200.

Output

For each case, print a line with the number of golden coins that can be picked up.

Problem information

Author: Unknown
Translator: Carlos Molina

Generation: 2026-01-25T11:40:37.869Z

© Jutge.org, 2006–2026.
https://jutge.org