Are you looking for math education tools for your children? Then, why not try this amazing product, E-circuit? This is the best toy to learn two-dimensional geometry, logic, and arithmetic!
The E-circuit consists of a grid space with a bunch of square blocks, called units. Each unit perfectly fits into a grid cell. They have some input and/or output terminals to transfer integer values. When a number of units are appropriately placed in the grid, they form a tree representing a mathematical formula.
The units have different functionalities, each represented by a single character as follows.
Two cells are adjacent if they share an edge. When two units are placed on adjacent cells, they are connected by using an input terminal of one unit and an output terminal of the other.
You are given an appropriate placement of units in which the units form a single tree representing a mathematical formula. A formal description of such a placement will be given in the Input section.
Your task is to calculate the value the printer displays for the given unit placement.
The input consists of a single test case of the following format.
$n$ $m$ $x_{1,1}$ ... $x_{1,m}$ $\vdots$ $x_{n,1}$ ... $x_{n,m}$
The first two integers $n$ and $m$ ($1 \leq n \leq 50, 1 \leq m \leq 50$) mean that the grid has cells arranged in an $n \times m$ matrix. The following $n$ lines describe the placement of units. The character $x_{i,j}$ ($1 \leq i \leq n, 1 \leq j \leq m$) specifies the unit on the cell $i$-th from the top and $j$-th from the left. Each character either represents the unit functionality, as described in the problem statement, or is the character ‘.’, meaning that the cell is empty.
It is guaranteed that the units are placed appropriately, that is,
It is also guaranteed that input values of ‘/’ operators are not zero and no units have an output value larger than $10^{18}$.
Output a line containing the displayed value.
6 8 3....... #....P.. #....#.2 #.###*#+ ##-....# ..1...4#
12
4 3 .4. ./P 9*. .#7
15
5 11 8...8.....8 #.###...### #.#...P.#.. #**##-*+/## .4...3.0..1
2024