problems / 289

Suki's Muddy Paw Tiles

easy matrix arrays simulation 1000 ms · 256 MB

Suki's Muddy Paw Tiles

Suki, a black curly-coated dog with a grey beard and white paws, came in from a long dig in the garden and ran straight across the kitchen. Her owner took a photo of the floor right away: it is a grid of R rows and C columns of tiles, and each tile is either clean (.) or holds one of Suki's muddy pawprints (M). But Suki skids on the smooth tiles, and by the time she settles on her blanket, every row that has a pawprint in the photo is muddy from one end to the other, and so is every column that has a pawprint in the photo.

Only the photo decides which rows and columns get muddy: a tile that was muddied by skidding does not make its own row or column muddy. Show the kitchen floor after the skidding and count how many tiles are muddy.

Input

The first line contains two integers R and C (1 ≤ R, C ≤ 1000).

Each of the next R lines contains a string of exactly C characters, each . or M: the photo of the floor, row by row.

Output

Print R lines of C characters each: the floor after the skidding, where a tile is M if its row or its column has an M in the photo, and . otherwise. Then print one more line with the number of M tiles on the final floor.

Notes

In the first example the photo has pawprints in rows 1 and 3 and in columns 2 and 4. Rows 1 and 3 become fully muddy, and in row 2 only the tiles of columns 2 and 4 do, so 10 of the 12 tiles are muddy. The tile in row 2, column 2 was muddied by skidding, so it doesn't spread mud along row 2.

In the second example there are no pawprints at all: the floor stays clean and the count is 0.

Example 1

Input

3 4
.M..
....
...M

Output

MMMM
.M.M
MMMM
10

Example 2

Input

2 3
...
...

Output

...
...
0

Hints · reveal one at a time if you're stuck

Hint 1

If you paint a row muddy as soon as you find a pawprint in it, what happens when you later look at a tile you just painted? How do you tell a real pawprint from a skid mark?

Hint 2

Don't change the grid while you read it. First record, for every row and every column, whether it has a pawprint in the photo. Then a tile ends up muddy exactly when its row or its column was recorded.

report an issue Prefer your own editor? sooks prep 289 (CLI)
Sign in to run code

Run your code on the examples, or submit it against every test.