Parse the Syntax Tree

아직 제출이 없습니다시간 제한2초메모리 제한1024 MB

문제

To evaluate a program efficiently, a language processor often transforms it into a syntax tree. In this problem you are given a syntax tree of a mathematical expression using ASCII characters. Please evaluate the expression

The syntax tree we consider in this problem is a rooted binary tree where each node has either zero or two children. If a node has zero children, it is an integer node that corresponds to a single integer between 0 and 9, inclusive. On the other hand, if a node has two children, the node is a binary operation node that corresponds to a binary operation of either addition, subtraction or multiplication. In this case the left and right children correspond to the left and right operands of the binary operation, respectively. For example, Figure B.1 represents the syntax tree of expression (94)\*((7\*2)+5)(9-4) \* ((7\*2)+5).

Figure B.1: Example of a syntax tree

To represent such a syntax tree using ASCII characters, you are given HH strings of WW characters. Each character is either ‘+', ‘-', ‘*', a digit between ‘0' and ‘9', or a period that represents a blank. For example, here is the representation of the syntax tree of Figure B.1.

...*.....
.-.....+.
9.4..*..5
....7.2..

Figure B.2 shows the rules (similar to Backus-Naur Form) of such representation of a syntax tree.

Figure B.2: Rules of the representation of a syntax tree

More precisely, the rules are defined as follows.

  • A “cell" is a rectangular region of characters that corresponds to a single node (i.e., either an integer node or a binary operation node) of a syntax tree.
  • A cell corresponding to an integer node contains only a single digit that is the same integer of the node. The height and width of such a cell are 1.
  • A cell cc corresponding to a binary operation node vv contains a single operator and two other cells as children. More precisely, let v_1v\_1 and v_2v\_2 be the left and right children of the binary operation node, respectively. And let c_1c\_1 and c_2c\_2 be the cells that correspond to v_1v\_1 and v_2v\_2, respectively. The height of cc is max(h_1,h_2)+1\max{(h\_1, h\_2)} + 1 where h_1h\_1 and h_2h\_2 are the heights of c_1c\_1 and c_2c\_2, respectively. On the other hand, the width of cc is w_1+w_2+1w\_1 + w\_2 + 1 where w_1w\_1 and w_2w\_2 are the widths of c_1c\_1 and c_2c\_2, respectively. The topmost row of cc consists of w_1w\_1 periods followed by an operator followed by w_2w\_2 periods where the operator is either ‘+', ‘-' or ‘*'. c_1c\_1 is located from the second to the (h_1+1)(h\_1+1)-st rows (from the top) and the first to the w_1w\_1-st columns (from the left) of cc. Similarly, c_2c\_2 is located from the second to the (h_2+1)(h\_2+1)-st rows (from the top) and the (w_1+2)(w\_1+2)-nd to the (w_1+w_2+1)(w\_1+w\_2+1)-st columns (from the left) of cc. Note that although c_1c\_1 and c_2c\_2 may have different heights, their top borders are always aligned.
  • It is guaranteed by the above rules that no two cells partially overlap each other. In other words, when two cells overlap, then one of them completely contains the other.
  • Any other characters that are not restricted by the above rules are filled by periods.
  • The entire region of characters is the “root" cell. In other words, the cell corresponding to the root node of the syntax tree has height HH and width WW.

Your task is to calculate the mathematical expression that corresponds to the given syntax tree formatted by the above rules.

입력

The input consists of a single test case of the following format.

HH WW

s_1s\_1

\vdots

s_Hs\_H

The first line contains two integers HH and WW (1H,W371 \le H, W \le 37), which represent the height and width of the representation of the given syntax tree. The following HH lines consist of strings of length WW where each character is either ‘+', ‘-', ‘*', a digit between ‘0' and ‘9', or a period. It is guaranteed that these strings represent a syntax tree of a mathematical expression in a valid form.

출력

Print the calculation result of the mathematical expression that corresponds to the given input.