Under construction

Still being built. Things may change

Gravity

Interview

Time limit1sMemory limit512 MB

Summary
Apples fall straight down until they rest on obstacles or the floor; print the final grid.
Level

Medium4 of 10

Topics
Simulation, Implementation
Solved
No attempts yet

Problem

Simulate gravity on a two-dimensional grid of nn rows and mm columns.

Each cell holds an obstacle, holds a single apple, or is empty.

Apply the following rules until no further change is possible.

  • Obstacles never move.
  • If the cell immediately below an apple is empty, the apple moves into that cell.

Find the state of the grid after every apple has settled.

Input

The first line contains two integers nn and mm separated by a space. (1≤n,m≤501 \le n, m \le 50)

Each of the next nn lines contains mm characters describing one row of the grid, from top to bottom. An obstacle is #, an apple is o, and an empty cell is ..

Output

Print the state of the grid on nn lines after all of the rules above have been applied.

Examples2

  1. Example 1

    Input
    3 3
    ooo
    #..
    ..#
    
    Expected output
    o..
    #.o
    .o#
    
  2. Example 2

    Input
    4 2
    oo
    oo
    o.
    ..
    
    Expected output
    ..
    o.
    oo
    oo