Under construction

Still being built. Things may change

E-Circuit Is Now on Sale!

Interview

Time limit2sMemory limit2048 MB

Summary
Given a grid of units that form a formula tree, compute the integer value that the printer unit displays.
Level

Easy3 of 10

Topics
Tree, DFS, Implementation
Solved
No attempts yet

Problem

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 fits exactly into one grid cell. Units have input and/or output terminals that transfer integer values. When several units are appropriately placed in the grid, they form a tree that represents a mathematical formula.

Each unit has one of the following functions, shown by a single character.

  • Digit (0 to 9): Has one output terminal. Sends the integer indicated by the digit to its output.
  • Connector (#): Has one input terminal and one output terminal. Passes the value from its input to its output without changes.
  • Operator (+, -, *, /): Has two input terminals and one output terminal. Computes on the two input values and sends the result to its output.
    • + computes the sum of the two inputs.
    • - computes the difference of the two inputs, the larger one minus the smaller one.
    • * computes the product of the two inputs.
    • / computes the quotient, the larger input divided by the smaller one, with the fraction truncated.
  • Printer (P): Has one input terminal and displays the input value. There must be exactly one printer unit on the grid.

Two cells are adjacent if they share an edge. When two units sit on adjacent cells, they connect by using an input terminal of one and an output terminal of the other.

The placement you receive is appropriate: the units form a single tree that represents a formula. The exact format is given in the Input section. Find the value the printer displays.

Input

The input is a single test case in the following format.

nn mm

x1,1x_{1,1} ⋯\cdots x1,mx_{1,m}

⋮\vdots

xn,1x_{n,1} ⋯\cdots xn,mx_{n,m}

The first two integers nn and mm (1≤n≤501 \le n \le 50, 1≤m≤501 \le m \le 50) give the grid size, nn rows by mm columns. The next nn lines describe the placement. The character xi,jx_{i,j} (1≤i≤n1 \le i \le n, 1≤j≤m1 \le j \le m) is the unit in the cell in row ii from the top and column jj from the left. Each character is either a unit symbol from the problem statement or ., which means the cell is empty.

The placement is guaranteed to be appropriate:

  • each unit has as many adjacent units as it has input and output terminals,
  • the total number of input terminals equals the total number of output terminals across all units, and
  • every unit belongs to the printer tree. A unit belongs to the printer tree if and only if it is the printer or is adjacent to another unit in the printer tree.

Also, the input values of every / operator are not zero, and no unit outputs a value larger than 101810^{18}.

Output

Output a line containing the value the printer displays.

Examples3

  1. Example 1

    Input
    6 8
    3.......
    #....P..
    #....#.2
    #.###*#+
    ##-....#
    ..1...4#
    
    Expected output
    12
    
  2. Example 2

    Input
    4 3
    .4.
    ./P
    9*.
    .#7
    
    Expected output
    15
    
  3. Example 3

    Input
    5 11
    8...8.....8
    #.###...###
    #.#...P.#..
    #**##-*+/##
    .4...3.0..1
    
    Expected output
    2024