Under construction

Still being built. Things may change

Sirtet

Interview

Time limit2sMemory limit512 MB

Summary
Connected groups of filled cells fall straight down as rigid objects until each lands on the bottom row or on another object. Print the final grid.
Level

Medium5 of 10

Topics
Simulation, BFS, Sorting
Solved
No attempts yet

Problem

In the zero-player video game Sirtet, the game takes place on a rectangular grid with NN rows and MM columns. Before the game begins, some cells are blank (.) and others are filled (#). Filled cells that are adjacent horizontally or vertically belong to the same rigid object. For example, this initial grid:

..#.
##.#
.##.
#...
#...

has four objects, shown below:

##     #    #     #
 ##    #           

When the game begins, the objects fall straight down the grid, all at the same speed. Each object keeps falling until it either touches the bottom row or has some part land directly on top of another object, at which point it stops. What will be the final state of the grid?

Input

The first line contains two space-separated positive integers NN and MM (N⋅M≤106N \cdot M \le 10^6).

The following NN lines contain MM characters each, describing the initial state of the grid. If the jj-th column of the ii-th row contains a block, the corresponding character is #. Otherwise it is ..

Output

Output NN lines of MM characters each, describing the final state of the grid. If the jj-th column of the ii-th row contains a block, the corresponding character is #. Otherwise it is ..

Examples1

  1. Example 1

    Input
    5 4
    ..#.
    ##.#
    .##.
    #...
    #...
    
    Expected output
    ....
    ....
    ###.
    ###.
    #..#