Problem:
Square Carpets

Mr. Frugal bought a new house.
He feels deeply in love with his new house
because it has a comfortable living room
in which he can put himself completely at ease.
He thinks his new house is a really good buy.

But, to his disappointment,
the floor of its living room has some scratches on it.

The floor has a rectangle shape, covered with square panels.
He wants to replace all the scratched panels with flawless panels,
but he cannot afford to do so.
Then, he decides to cover all the scratched panels
with carpets.

The features of the carpets he can use are as follows.

Carpets are square-shaped.

Carpets may overlap each other.

Carpets cannot be folded.

Different sizes of carpets are available.
Lengths of sides of carpets are multiples of that of the panels.

The carpets must cover all the scratched panels,
but must not cover any of the flawless ones.

For example,
if the scratched panels are as shown in Figure 1,
at least 6 carpets are needed.

Figure 1: Example Covering

As carpets cost the same irrespective of their sizes,
Mr. Frugal would like to use as few number of carpets as possible.

Your job is to write a program which tells the minimum number
of the carpets to cover all the scratched panels.

Input

The input consists of multiple data sets.

As in the following, the end of the input is indicated by a line
containing two zeros.

DataSet1

DataSet2

...

DataSetn

0 0

Each data set (DataSeti) represents the
state of a floor. The format of a data set is as follows.

W H

P11 P12 P13 ... P1W

P21 P22 P23 ... P2W

...

PH1 PH2
PH3 ... PHW

The positive integers W and H are the numbers of panels
on the living room
in the x- and y- direction, respectively.
The values of W and H are no more than 10.
The integer Pyx represents the state of the panel.
The value of Pyx means,

0: flawless panel (must not be covered),

1: scratched panel (must be covered).

Output

For each data set,
your program should output a line containing one integer which
represents the minimum number of the carpets to cover
all of the scratched panels.

Sample Input

4 3
0 1 1 1
1 1 1 1
1 1 1 1
8 5
0 1 1 1 1 1 1 1
1 1 1 1 1 1 1 1
1 1 1 1 1 1 1 1
1 1 1 0 1 1 1 1
0 1 1 1 0 1 1 1
8 8
0 1 1 0 0 1 1 0
1 1 1 1 1 1 1 1
1 1 1 1 1 1 1 1
0 1 1 0 0 1 1 0
0 1 1 0 0 1 1 0
1 1 1 1 1 1 1 1
1 1 1 1 1 1 1 1
0 1 1 0 0 1 1 0
10 10
1 1 1 1 1 1 1 1 1 1
1 1 1 1 1 1 1 1 1 1
1 1 0 1 1 0 1 1 0 1
1 1 1 1 1 1 1 1 1 1
1 1 1 1 1 1 1 1 1 1
1 1 0 1 1 0 1 1 0 1
1 1 1 1 1 1 1 1 1 1
1 1 1 1 1 1 1 1 1 1
1 1 0 1 1 0 1 1 0 1
1 1 1 1 1 1 1 1 1 1
0 0

Output for the Sample Input

2
6
14
29

Solution in JAVA (judge verdict: Wrong Answer)

import java.util.Scanner;

public class Main {
	static Scanner sc = new Scanner(System.in);
	static int W, H;
	static boolean[][] f;
	static int[][] size;
	static int[][] count;
	static int ans;

	public static void main(String[] args) {
		while (true) {
			W = sc.nextInt();
			if (W == 0) break;
			H = sc.nextInt();
			f = new boolean[H + 1][W + 1];
			size = new int[H + 1][W + 1];
			count = new int[H + 1][W + 1];
			for (int i = 0; i < H; ++i) {
				for (int j = 0; j < W; ++j) {
					f[i][j] = sc.next().equals("1");
				}
			}
			System.out.println(solve());
		}
	}

	static int solve() {
		ans = 999;
		int rest = 0;
		for (int i = 0; i < H; ++i) {
			for (int j = 0; j < W; ++j) {
				if (f[i][j]) ++rest;
				OUT: for (int k = 1;; ++k) {
					for (int r = i; r < i + k; ++r) {
						for (int c = j; c < j + k; ++c) {
							if (!f[r][c]) {
								size[i][j] = k - 1;
								break OUT;
							}
						}
					}
				}
			}
		}
		if (rest == 0) return 0;
		rec(0, 0, 0, rest);
		return ans;
	}

	static void rec(int r, int c, int num, int rest) {
		if (rest == 0) {
			ans = num;
			return;
		}
		if (num + 1 >= ans) return;
		if (c == W) {
			rec(r + 1, 0, num, rest);
			return;
		}
		if (!f[r][c] || count[r][c] != 0) {
			rec(r, c + 1, num, rest);
		}

		// prune
		boolean filled = true;
		for (int i = 0; i < size[r][c]; ++i) {
			if (count[r + i][c] == 0) {
				filled = false;
				break;
			}
		}
		if (filled && size[r][c + 1] >= size[r][c]) return;
		filled = true;
		for (int i = 0; i < size[r][c]; ++i) {
			if (count[r][c + 1] == 0) {
				filled = false;
				break;
			}
		}
		if (filled && size[r + 1][c] >= size[r][c]) return;

		boolean use = false;
		for (int i = 0; i < size[r][c]; ++i) {
			for (int j = 0; j < size[r][c]; ++j) {
				if (count[r + i][c + j] == 0) {
					use = true;
					--rest;
				}
				count[r + i][c + j]++;
			}
		}
		if (use) {
			rec(r, c + 1, num + 1, rest);
		}
		for (int i = 0; i < size[r][c]; ++i) {
			for (int j = 0; j < size[r][c]; ++j) {
				count[r + i][c + j]--;
			}
		}
	}
}