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.