Problem:
Score : 500 points

Problem Statement

We have a chocolate bar partitioned into H horizontal rows and W vertical columns of squares.

The square (i, j) at the i-th row from the top and the j-th column from the left is dark if S_{i,j} is 0, and white if S_{i,j} is 1.

We will cut the bar some number of times to divide it into some number of blocks. In each cut, we cut the whole bar by a line running along some boundaries of squares from end to end of the bar.

How many times do we need to cut the bar so that every block after the cuts has K or less white squares?

Constraints

1 \leq H \leq 10

1 \leq W \leq 1000

1 \leq K \leq H \times W

S_{i,j} is 0 or 1.

Input

Input is given from Standard Input in the following format:

H W K
S_{1,1}S_{1,2}...S_{1,W}
:
S_{H,1}S_{H,2}...S_{H,W}

Output

Print the number of minimum times the bar needs to be cut so that every block after the cuts has K or less white squares.

Sample Input 1

3 5 4
11100
10001
00111

Sample Output 1

2

For example, cutting between the 1-st and 2-nd rows and between the 3-rd and 4-th columns - as shown in the figure to the left - works.

Note that we cannot cut the bar in the ways shown in the two figures to the right.

Sample Input 2

3 5 8
11100
10001
00111

Sample Output 2

0

No cut is needed.

Sample Input 3

4 10 4
1110010010
1000101110
0011101001
1101000111

Sample Output 3

3

Solution in C++14 (GCC 5.4.1) (judge verdict: Time Limit Exceeded)

#include <iostream>
#include <string>
#include <vector>
#include <algorithm>
#include <functional>
#include <cmath>
#include <iomanip>
#include <stack>
#include <queue>
#include <numeric>
#include <map>
#include <unordered_map>
#include <set>
#include <fstream>
#include <chrono>
#include <random>
#include <bitset>
#define rep(i,n) for(int i=0;i<(n);i++)
#define all(x) x.begin(), x.end()
#define rall(x) x.rbegin(), x.rend()
#define sz(x) ((int)(x).size())
#define pb push_back
#define mod 1000000007
using ll = long long;
using namespace std;
ll gcd(ll a, ll b) {return b?gcd(b,a%b):a;}
ll lcm(ll a, ll b) {return a/gcd(a,b)*b;}

vector<vector<int>> wcnt;

int whites(int w1, int w2, int y1, int y2){
  return wcnt[y2][w2]-wcnt[y2][w1]-wcnt[y1][w2]+wcnt[y1][w1];
}

int main(){
  int H,W,K; cin >> H >> W >> K;
  wcnt.resize(H+1, vector<int>(W+1,0));
  vector<string> S(H);
  rep(i,H) cin >> S[i];
  rep(y,H){
    rep(x,W){
      if(S[y][x] == '1') wcnt[y+1][x+1]=1;
    }
  }
  rep(y,H+1){
    rep(x,W){
      wcnt[y][x+1] += wcnt[y][x];
    }
  }
  rep(x,W+1){
    rep(y,H){
      wcnt[y+1][x] += wcnt[y][x];
    }
  }
  ll loop = pow(2,H-1);
  int ans = (H-1)*(W-1);
  rep(i,loop){
    //cout << 'a' << endl;
    int preans;
    vector<int> cutline;
    cutline.pb(0);
    int now = i;
    rep(x,10){
      if(now%2) cutline.pb(x+1);
      now >>= 1;
    }
    cutline.pb(H);
    int cuts = sz(cutline);
    //rep(j,cuts) cout << cutline[j] << " ";
    preans = cuts-2;
    int cut1 = 0;
    int cut2 = 0;
    bool flag = true;
    while(true){
      bool ok = true;
      rep(j,cuts-1){
        int count = whites(cut1, cut2+1, cutline[j], cutline[j+1]);
        if(count>K) ok = false;
      }
      //cout << "a";
      if(!ok){
        cut1 = cut2;
        if(cut2!=W)preans++;
        if(cut1 == W) {
          flag = false;
          break;
        }
      }
      else{
        if(cut2+1 == W)break;
        cut2++;
      }
    }
    if(flag) ans = min(preans, ans);
  }

  cout << ans << endl;
  return 0;
}