334. Tiny Puzzle

Time limit per test: 0.25 second(s)
Memory limit: 65536 kilobytes
input: standard
output: standard

You've decided to make a puzzle as a part of the "re-branding" campaign run by your company. In the box this puzzle will look as the new logo of your company, that is, it will be a set of 9 (nine) connected unit squares on a rectangular grid. However, these 9 squares will not necessarily all be glued together, i.e., they may form several connected parts (however, you're not allowed to split squares in the middle). The objective of the puzzle will be to rearrange those parts so that they form a 3x3 square. When rearranging, one is only allowed to shift parts, and NOT to rotate or to flip them.

To make the puzzle more challenging, you need to have as few parts as possible. Given the logo, compute how many parts do you need and how to rearrange them into a 3x3 square.

Every word "connected" in the problem statement means connectedness in the side-by-side sense.

The first line of input contains two integers H and W. The next H lines will contain W characters each, describing your logo. Each of those characters will be either '
' (dot), denoting an empty square, or '
' (capital English letter X), denoting a square occupied by the logo. All the '
's will form a connected figure, there will be exactly 9 '
's in input. The first line, the last line, the first column and the last column will contain at least one '
', in other words, there will not be unnecessary empty rows at any side of the grid.

On the first line, output the required number of parts K. Afterwards, output one of the possible rearrangements, more precisely: on the next H lines output the logo with all the '
's replaced by the first K capital letters of English alphabet, each letter denoting one part. Then output an empty line, and then 3 lines containing 3 characters each, containing the same K parts but shifted around so that they form a 3x3 square.

All the parts must be connected, and K must be as little as possible. If there're several possible solutions, output any.

sample input
sample output
4 5

Online Contester Team © 2002 - 2010. All rights reserved.