Patisserie ACM
Amber Claes Maes, a patissier, opened her own shop last month. She decided to submit her work to the International Chocolate Patissier Competition to promote her shop, and she was pursuing a recipe of sweet chocolate bars. After thousands of trials, she finally reached the recipe. However, the recipe required high skill levels to form chocolate to an orderly rectangular shape. Sadly, she has just made another strange-shaped chocolate bar as shown in Figure 1.
Figure 1: A strange-shaped chocolate bar
Each chocolate bar consists of many small rectangular segments of chocolate. Adjacent segments are separated with a groove in between them for ease of snapping. She planned to cut the strange-shaped chocolate bars into several rectangular pieces and sell them in her shop. She wants to cut each chocolate bar as follows.
The bar must be cut along grooves.
The bar must be cut into rectangular pieces.
The bar must be cut into as few pieces as possible.
Following the rules, Figure 2 can be an instance of cutting of the chocolate bar shown in Figure 1. Figures 3 and 4 do not meet the rules; Figure 3 has a non-rectangular piece, and Figure 4 has more pieces than Figure 2.
Figure 2: An instance of cutting that follows the rules
Figure 3: An instance of cutting that leaves a non-rectangular piece
Figure 4: An instance of cutting that yields more pieces than Figure G-2
Your job is to write a program that computes the number of pieces of chocolate after cutting according to the rules.
Input
The input is a sequence of datasets. The end of the input is indicated by a line containing two zeros separated by a space. Each dataset is formatted as follows.
h wr_{(1, 1)} ... r_{(1, w)}r_{(2, 1)} ... r_{(2, w)}...r_{(h, 1)} ... r_{(h, w)}
The integers h and w are the lengths of the two orthogonal dimensions of the chocolate, in number of segments. You may assume that 2 ≤ h ≤ 100 and 2 ≤ w ≤ 100. Each of the following hlines consists of w characters, each is either a "." or a "#". The character r_{(i, j)} represents whether the chocolate segment exists at the position (i, j) as follows.
".": There is no chocolate.
"#": There is a segment of chocolate.
You can assume that there is no dataset that represents either multiple disconnected bars as depicted in Figure 5 or a bar in a shape with hole(s) as depicted in Figure 6 and 7. You can also assume that there is at least one "#" character in each dataset.
Figure 5: Disconnected chocolate bars
Figure 6: A chocolate bar with a hole
Figure 7: Another instance of a chocolate bar with a hole
Output
For each dataset, output a line containing the integer representing the number of chocolate pieces obtained by cutting according to the rules. No other characters are allowed in the output.