p00709 Square Carpets_13759
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.

1. Carpets are square-shaped.
2. Carpets may overlap each other.
3. Carpets cannot be folded.
4. 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.

<image>
---
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.

Example

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

2
6
14
29
#include<iostream>
#include<functional>
#include<algorithm>

const int N = 11;
const int inf = 1<<27;
const int di[] = {-1,1,1,-1};
const int dj[] = {1,1,-1,-1};

using namespace std;

int H,W;
static int M[N][N];
static int maxSizes[N][N][4];
int oneCount;
int minHW;
int ans;
int greedy_ans;
int depth;
int maxSize;

inline int getHeuristic();
void calc();
void solve(int pos);

inline int getHeuristic()
{
  int msz=maxSize;
  msz *= msz;
  return oneCount/msz + (oneCount%msz>0);
}

struct Cand{
  int i,j;
  Cand():i(0),j(0){}
  Cand(int ci, int cj):i(ci),j(cj){}
  bool operator<(const Cand& t)const{
    return (i==t.i)?(j<t.j):(i<t.i);
  }
};

int cand_num;
Cand cands[N*N];

void calc(){
  for(int i = 0; i < H; ++i){
    for(int j = 0; j < W; ++j){
      if(M[i][j] == 1){
	oneCount++;
	cands[cand_num++] = Cand(i,j);
      }
      for(int k = 0; k < 4; ++k){
	int size = 0;
	for(size = 1; size <= minHW; ++size){
	  for(int m = i; (k==0||k==3)?(m>i-size):(m<size+i); m+=di[k]){
	    for(int n = j; (k==2||k==3)?(n>j-size):(n<size+j); n+=dj[k]){
	      if( m<0||n<0||m>=H||n>=W||M[m][n]==0 ){
		--size;
		goto next;
	      }
	    }
	  }
	  if(k==0){
	    if(i-size<0||j+size>=W)break;
	  }else if(k==1){
	    if(i+size>=H||j+size>=W)break;
	  }else if(k==2){
	    if(i+size>=H||j-size<0)break;
	  }else{
	    if(i-size<0||j-size<0)break;
	  }
	}
      next:;
	maxSizes[i][j][k] = size;
	if(k==1){
	  if(size>1){
	    maxSize = max( maxSize, size );
	  }
	}
      }
    }
  }
  sort(cands,cands+cand_num);
  return ;
}

void solve_greedy()
{
  // completed.
  if(oneCount==0){
    greedy_ans = min(greedy_ans, depth);
    return ;
  }

  // embed carpet greedy
  int maxOneCount = -1;
  int maxp,maxk,maxsz;
  for(int p = 0; p < cand_num; ++p){
    int i = cands[p].i;
    int j = cands[p].j;
    if( M[i][j] == 1 ){
      for(int k = 0; k < 4; ++k){
	int cnt = 0;
	int size = maxSizes[i][j][k];
	for(int m = i; (k==0||k==3)?(m>i-size):(m<size+i); m+=di[k]){
	  for(int n = j; (k==2||k==3)?(n>j-size):(n<size+j); n+=dj[k]){
	    if(M[m][n]==1){
	      ++cnt;
	    }
	  }
	}
	if(maxOneCount<cnt){
	  maxp = p;
	  maxk = k;
	  maxsz = size;
	  maxOneCount = cnt;
	}
      }
    }
  }
  int i = cands[maxp].i;
  int j = cands[maxp].j;
  int k = maxk;
  int size = maxsz;
  for(int m = i; (k==0||k==3)?(m>i-size):(m<size+i); m+=di[k]){
    for(int n = j; (k==2||k==3)?(n>j-size):(n<size+j); n+=dj[k]){
      if(M[m][n]==1){
	M[m][n]=depth;
	--oneCount;
      }
    }
  }
  ++depth;
  solve_greedy();
  --depth;
  for(int m = i; (k==0||k==3)?(m>i-size):(m<size+i); m+=di[k]){
    for(int n = j; (k==2||k==3)?(n>j-size):(n<size+j); n+=dj[k]){
      if( M[m][n] == depth ){
	M[m][n]=1;
	++oneCount;
      }
    }
  }
  return ;
}

void solve(int pos){
  /*
    printf("now i = %d, now j = %d, h = %d\n", pos/W, pos%W, getHeuristic());
    for(int k = 0; k < H; ++k){
      for(int l = 0; l < W; ++l){
	if(l>0)putchar(' ');
	printf("%2d", M[k][l]);
      }
      putchar('\n');
    }
    putchar('\n');
  */

  // completed.
  if(oneCount==0){
    ans = min(ans, depth);
    return ;
  }

  // back
  if(depth+getHeuristic()>=ans){
    return ;
  }

  // embed carpet
  for(int p = pos; p < cand_num; ++p){
    int i = cands[p].i;
    int j = cands[p].j;
    int size = maxSizes[i][j][1];
    int valid=0;
    for(int m = i; m<size+i; m++){
      for(int n = j; n<size+j; n++){
	if(M[m][n]==1){
	  valid=1;
	  --oneCount;
	  M[m][n]=depth;
	}
      }
    }
    if( valid ){
      ++depth;
      solve(p+1);
      --depth;
    }
    for(int m = i; m<size+i; m++){
      for(int n = j; n<size+j; n++){
	if( M[m][n] == depth ){
	  ++oneCount;
	  M[m][n]=1;
	}
      }
    }
    if( M[i][j] > 1 )
      solve(p+1);
    return ;
  }
}

int main()
{
  while(true){
    scanf("%d%d", &W, &H);
    if(W==H&&H==0)break;
    for(int i = 0; i < H; ++i){
      for(int j = 0; j < W; ++j){
	scanf("%d", &M[i][j]);
      }
    }

    minHW = min(H,W);
    oneCount = 0;
    cand_num = 0;
    ans=inf;
    greedy_ans = inf;
    depth=2;
    maxSize = 1;

    calc();

    solve_greedy();
    ans = greedy_ans;
    solve(0);

    printf("%d\n", ans-2);
  }
  return 0;
}