Hex Tile Equations#4636

Hex Tile Equations

1 second
64 megabytes
Very hard

An amusing puzzle consists of a collection of hexagonal tiles packed together, with each tile showing a digit, an equal sign =, or an arithmetic operation: +, -, *, or /. Consider continuous paths passing through each tile exactly once, with each successive tile being an immediate neighbor of the previous tile. The objective is to choose such a path so that the sequence of characters on the tiles forms a valid equation, according to the restrictions listed below. In each figure above, an example sequence is illustrated. In Figure 1, if you follow the gray path from the top, the character sequence is "6/3=9-7". Similarly, in Figure 2, start from the bottom left 3 to get "3*21+10=73".

There are many potential paths through even a moderate-sized hex tile pattern. A puzzle player may get frustrated and want to see the answer. Your task is to automate finding the solution.

The arrangement of hex tiles and choice of characters in each puzzle must satisfy these rules:

1. The hex pattern has an odd number of rows, greater than . All odd-numbered rows contain the same number of tiles. Even-numbered rows have one more tile than the odd-numbered rows, and these longer even rows protrude both to the left and right of the odd rows. 2. There is exactly one = in the hex pattern. 3. There are no more than two * characters in the hex pattern. 4. There are fewer than tiles in the hex pattern. 5. Assuming the restrictions on allowed character sequences described below, each hex pattern has a unique acceptable solution.

For a sequence along a path to be acceptable, the expressions on each side of the equal sign must be in an acceptable form and evaluate to the same numeric value. The following rules define acceptable form and how expressions are evaluated:

6. The operators +, -, *, and / can only be used as binary operators; sequences where + or - would be unary are not acceptable. For example, "-2*3=-6" and "1=5+-4" are not acceptable. 7. Standard operator precedence is not used. Instead, all operations are performed left to right, with equal precedence. For example, "44-4/2=2+3*4" is acceptable, but "14=2+3*4" is not. 8. If division is present, it is only acceptable when the division yields an exact integer result at every step. For example, "10/5=12/6" and "7+3/5=3*4/6" are acceptable, but "5/2*4=10" is not, because intermediate results are not integers. "5/2*4=8" is also not acceptable, because equality would require truncation or rounding during division. 9. At most two digits may appear together as a number. For example, "123+1=124" is not acceptable. 10. A sequence where a digit is immediately followed by another digit is not acceptable. For example, "3*05=15" is not acceptable.

Under these assumptions, intermediate or final arithmetic results will never exceed three million in magnitude.

Input

The input will consist of one to fifteen data sets, followed by a line containing only 0.

The first line of a dataset contains two space-separated integers , where is the number of rows in the hex pattern, and is the number of entries in the odd-numbered rows. The next lines contain the characters on the hex tiles, one row per line. All tile characters within a row are space-separated. Odd-numbered rows also begin with a space, to help simulate the hexagonal arrangement. Properties always apply.

Output

For each dataset, print the unique valid equation according to rules . The output line should not contain spaces.

Examples

Example 1

Loading
Loading