Under construction

Still being built. Things may change

SimRoad 3

Time limit1sMemory limit1024 MB

Summary
Mow the fewest grass cells in a grid so all settlements become connected by four-directional moves, then print the resulting grid.
Level

Hard8 of 10

Topics
Graph, Shortest path, Minimum spanning tree, Greedy
Solved
No attempts yet

Problem

JOI sells a simulation game called SimRoad. In this game, the player becomes the ruler of the country where the game takes place and performs various operations to make the country prosper.

The country is in a primitive age. Settlements are scattered across the country, but much of the country is covered with tall grass, so residents cannot easily move between settlements in that state. Your first mission as the player is to mow the grass so that residents can move easily between settlements.

The state of the country is represented by cells divided into WW cells in the east-west direction and HH cells in the north-south direction, each in one of three states. 'w' means the cell is covered with grass, '.' means the cell is clear ground (not covered with grass), and '@' means there is a settlement in the cell.

Residents can move freely to adjacent cells in the four directions, north, south, east, and west, if the cell is a settlement or clear ground, but they cannot move into a cell covered with grass. Therefore, to let residents move easily between settlements, you must mow the grass on grass-covered cells to turn them into clear ground so that every settlement can reach every other settlement.

Mowing the grass on a grass-covered cell turns it into clear ground, but mowing requires putting residents to work, and mowing one cell costs 1 dollar. Considering future prosperity, spending less money is better.

Naturally, there can be many ways to mow the grass. From the given state of the country, mow in a way that mows as few cells as possible, and produce the resulting state of the country.

Basic algorithm (BASIC ALGORITHM): Let y′y' be H/2H/2 if HH is even, and (H+1)/2(H + 1)/2 if HH is odd. First, mow all grass-covered cells in the y′y'-th row from the north. Next, for every settlement, if there are grass-covered cells between that settlement and the y′y'-th cell from the north in the north-south direction, mow all of them.

Input

The input file is given in the following format.

  • The first line contains integers WW and HH separated by a space.
  • The following HH lines each contain a string of WW characters and represent the state of the country. In the yy-th of these lines, the xx-th character is 'w', '.', or '@', and it represents the state of the cell at the yy-th position from the north and the xx-th position from the west.

Output

Submit the output data as a file.

  • Write the state of the country after mowing. The output consists of HH lines, each a string of WW characters. In the yy-th line, the xx-th character must be 'w', '.', or '@', representing the state of the cell at the yy-th position from the north and the xx-th position from the west.

Constraints

  • 1≤W≤1001 \le W \le 100: the number of cells in the east-west direction
  • 1≤H≤1001 \le H \le 100: the number of cells in the north-south direction

Hint

In this output example, 7 grass-covered cells were mowed and turned into clear ground, so it costs 7 dollars. If this output example is submitted, you get 20 points if no one submits an output of a country state that can be made for less money than yours; but if Em is 6 dollars, you get 12.9 points.

The output of the basic algorithm for the sample input is as follows. It mows 10 grass-covered cells and turns them into clear ground, so it costs 10 dollars.

w@ww@w@
w.ww.w.
....@..
@w.w.ww
ww@.@w.

Examples1

  1. Example 1

    Input
    7 5
    w@ww@w@
    w.wwwww
    wwww@ww
    @wwwwww
    ww@.@w.
    
    Expected output
    w@..@.@
    w.ww.ww
    wwww@ww
    @www.ww
    ..@.@w.