Problem: Great performance strategy

Taro, an examinee, participated in an N-day study camp. In this training camp, M subjects are tested every day, and after the training camp, a report card with all the test scores is distributed. The report card consists of N sheets of paper, and on the i-th sheet, only the subject names and scores of the tests of all M subjects on the i-day are printed.

Taro noticed that the date was not written on the report card, and decided to add some work before giving the report card to his mother. By rearranging the order of the papers in the report card and writing the page numbers in order from the first sheet to make a booklet, a "fake report card" was created. Taro's purpose is to maximize the number of subjects whose test scores are monotonously increasing with respect to page numbers in the "False Report Card".

When Taro makes a "false grade report", find the maximum number of subjects whose test scores are monotonously increasing with respect to the page number.

However, the N-day test score is a broad monotonous increase with respect to the page number. For 1 ≤ i <N, the score on the i + 1 sheet is the score on the i-th sheet. It means that it is the above.

Input

The input consists of 50 or less datasets. Each dataset is represented in the following format.

> N M a11 ... a1M ... aN1 ... aNM

The first line gives the number of days N and the number of subjects M for the study camp. N is an integer between 1 and 103, and M is an integer between 1 and 40.

Taro's score is given to the following N lines. Each line consists of M integers, where aij is the score of Taro-kun's subject j on day i, and aij is an integer between 0 and 103.

The end of the input is represented by a line of two zeros.

Output

For each dataset, output the maximum number of subjects whose test scores are monotonously increasing with respect to the page number on one line.

Sample Input


3 3
one two Three
2 1 3
3 3 3
8 5
3 3 4 3 4
2 1 2 3 2
7 9 7 8 7
3 4 3 3 4
1 2 2 3 2
4 5 6 5 6
7 9 7 7 8
4 5 5 6 6
5 5
1 2 3 4 5
2 3 4 5 1
3 4 5 1 2
4 5 1 2 3
5 1 2 3 4
5 9
1 1 1 1 1 1 1 1 1
2 2 2 2 2 2 2 2 2
3 3 3 3 3 3 3 3 3
4 4 4 4 4 4 4 4 4
5 5 5 5 5 5 5 5 5
0 0


Output for the Sample Input


2
3
1
9






Example

Input

3 3
1 2 3
2 1 3
3 3 3
8 5
3 3 4 3 4
2 1 2 3 2
7 9 7 8 7
3 4 3 3 4
1 2 2 3 2
4 5 6 5 6
7 9 7 7 8
4 5 5 6 6
5 5
1 2 3 4 5
2 3 4 5 1
3 4 5 1 2
4 5 1 2 3
5 1 2 3 4
5 9
1 1 1 1 1 1 1 1 1
2 2 2 2 2 2 2 2 2
3 3 3 3 3 3 3 3 3
4 4 4 4 4 4 4 4 4
5 5 5 5 5 5 5 5 5
0 0


Output

2
3
1
9

Solution (in CPP):

Solution:
#include <iostream>
#include <utility>
#include <vector>
#include <utility>
#include <algorithm>
#define inf 1e9

using namespace std;
typedef pair<int, int> P;

int n, m;
int a[1005][45];
int G[45][45];
int mask[45], mask2[25];
vector<P> vec, vec2;
int dp[1<<20], dp2[1<<20];

void dfs(int p, int l, int r, int s, int cnt, vector<P> &vec)
{
  if(p > r){
    vec.push_back(make_pair(s, cnt));
    return;
  }
  dfs(p+1, l, r, s, cnt, vec);
  if(mask[p] & s) return;
  dfs(p+1, l, r, s|1<<(p-l), cnt+1, vec);
}

int main(void)
{
  while(1){
    cin >> n >> m;
    if(n == 0 && m == 0) break;
    for(int i = 1; i <= n; i++){
      for(int j = 0; j < m; j++) cin >> a[i][j];
    }
    if(m == 1){
      cout << 1 << endl;
      continue;
    }

    for(int i = 0; i < m; i++){
      for(int j = 0; j < m; j++){
        G[i][j] = 0;
      }
    }
    for(int i = 0; i < m; i++){
      for(int j = 0; j < m; j++){
        if(i >= j) continue;
        vector<P> vec;
        for(int k = 1; k <= n; k++){
          vec.push_back(make_pair(a[k][i], a[k][j]));
        }
        sort(vec.begin(), vec.end());
        for(int k = 1; k < vec.size(); k++){
          if(vec[k-1].second > vec[k].second){
            G[i][j] = G[j][i] = 1;
            break;
          }
        }
      }
    }

    /*for(int i = 0; i < m; i++){
      for(int j = 0; j < m; j++){
        cout << G[i][j] << " ";
      }
      cout << endl;
    }*/

    for(int i = 0; i < m/2; i++){
      mask[i] = 0;
      for(int j = 0; j < m/2; j++) if(G[i][j]) mask[i] |= 1<<j;
    }
    for(int i = m/2; i < m; i++){
      mask[i] = 0;
      for(int j = m/2; j < m; j++) if(G[i][j]) mask[i] |= 1<<(j-m/2);
    }

    vec.clear(), vec2.clear();
    dfs(0, 0, m/2-1, 0, 0, vec);
    dfs(m/2, m/2, m-1, 0, 0, vec2);

    int S = 1<<(m/2), S2 = 1<<(m-m/2);
    for(int i = 0; i < S2; i++) dp2[i] = -inf;
    for(int i = 0; i < vec2.size(); i++) dp2[vec2[i].first] = vec2[i].second;
    for(int i = 0; i < S2; i++){
      for(int j = 0; j < m-m/2; j++){
        if(i && 1<<j) dp2[i] = max(dp2[i], dp2[i & ~(1<<j)]);
      }
    }

    for(int i = 0; i < m/2; i++){
      mask2[i] = 0;
      for(int j = m/2; j < m; j++) if(G[i][j]) mask2[i] |= 1<<(j-m/2);
    }

    int ans = 0;
    for(int i = 0; i < vec.size(); i++){
      int adj = 0;
      for(int j = 0; j < m/2; j++) if(vec[i].first & (1<<j)) adj |= mask2[j];
      ans = max(ans, vec[i].second + dp2[((S2-1)-adj)]);
    }
    cout << ans << endl;
  }

  return 0;
}