Cubes

Time limit1sMemory limit512 MB

Summary
Draw a 3D ASCII picture of an m by n grid of cube towers, painting cubes back to front and bottom to top so nearer cubes cover farther ones, then trim to the smallest bounding rectangle.
Level

Medium5 of 10

Topics
Simulation, Implementation, Matrix
Solved
No attempts yet

Problem

You want to draw a 3D picture of a pile of stacked cubes using ASCII characters. The cubes stand neatly in mm rows and nn columns, and some cubes carry one or more further cubes on top of them, forming towers. The rows are numbered 1 to mm: row 1 is the farthest row in the picture and row mm is the nearest. The columns are numbered 1 to nn from left to right. Some cubes hide other cubes, which are then partly or completely invisible.

A single cube is drawn with the characters + (plus), - (minus), | (vertical bar), / (slash) and (space) in 6 lines and 7 columns, like this:

  +---+
 /   /|
+---+ |
|   | +
|   |/
+---+

Within this 6 by 7 block, the two leftmost cells of the first line, the leftmost cell of the second line, the rightmost cell of the fifth line and the two rightmost cells of the sixth line do not belong to the cube. Every other cell belongs to the cube, including the spaces inside its faces.

The position of each cube follows these rules:

  • In the same row, moving one column to the right shifts the drawing 4 columns to the right.
  • Moving one row farther away (the row number decreases by 1) shifts the drawing 2 lines up and 2 columns to the right.
  • A cube one level higher is drawn 3 lines above the cube directly below it.

Hiding works as if you draw the cubes in this order: rows 1 to mm, within a row columns 1 to nn, and at one position from the bottom level to the top, where each cube drawn later covers what was drawn before.

Write a program that draws the whole configuration. The picture must use as few lines and columns as possible, so print the smallest rectangle that contains every cube. Mark empty cells with . (period).

Input

The first line contains the positive integers mm and nn (1≤m,n≤501 \le m, n \le 50).

Each of the next mm lines contains nn positive integers. Each of them is at most 50 and is the number of cubes stacked at that position, that is, the height of the tower. The jj-th number on the ii-th line is the height at row ii, column jj.

Output

Print the picture of the cubes as described in the statement. All lines have the same length.

Examples3

  1. Example 1

    Input
    1 2
    1 2
    
    Expected output
    ......+---+
    ...../   /|
    ....+---+ |
    ..+-|   | +
    ./  |   |/|
    +---+---+ |
    |   |   | +
    |   |   |/.
    +---+---+..
    
  2. Example 2

    Input
    3 1
    2
    1
    3
    
    Expected output
    ......+---+
    ..+---+  /|
    ./   /|-+ |
    +---+ | | +
    |   | + |/|
    |   |/|-+ |
    +---+ |/| +
    |   | + |/.
    |   |/| +..
    +---+ |/...
    |   | +....
    |   |/.....
    +---+......
    
  3. Example 3

    Input
    2 3
    2 3 2
    1 2 1
    
    Expected output
    ........+---+....
    ......./   /|....
    ......+---+ |....
    ....+-|   | +---+
    .../  |   |/   /|
    ..+---+---+---+ |
    ..|  /   /|   | +
    ..| +---+ |   |/|
    ..+-|   | +---+ |
    ./  |   |/   /| +
    +---+---+---+ |/.
    |   |   |   | +..
    |   |   |   |/...
    +---+---+---+....