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: Accepted)

#include <limits.h>
#include <math.h>
#include <stdio.h>
#include <stdlib.h>
#include <algorithm>
#include <cassert>
#include <cfloat>
#include <complex>
#include <functional>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <unordered_map>
#include <vector>

using namespace std;

#define chmax(x, y) x = max(x, y)
#define chmin(x, y) x = min(x, y)
#define rep(i, n) for (ll i = 0; i < (n); ++i)
#define Sort(v) sort(v.begin(), v.end())
#define Reverse(v) reverse(v.begin(), v.end())
#define Lower_bound(v, x) \
  distance(v.begin(), lower_bound(v.begin(), v.end(), x))
#define Upper_bound(v, x) \
  distance(v.begin(), upper_bound(v.begin(), v.end(), x))

using ll = long long;
using ull = unsigned long long;
using P = pair<ll, ll>;
using T = tuple<ll, ll, ll>;
using vll = vector<ll>;
using vP = vector<P>;
using vT = vector<T>;
using vvll = vector<vector<ll>>;
using vvP = vector<vector<P>>;
using dqll = deque<ll>;

ll dx[9] = {-1, 1, 0, 0, -1, -1, 1, 1, 0};
ll dy[9] = {0, 0, -1, 1, -1, 1, -1, 1, 0};

const ll INF = 1LL << 50;

ll get(vvll& csum, ll x1, ll y1, ll x2, ll y2) {
  return csum[y1][x1] - csum[y1][x2] - csum[y2][x1] + csum[y2][x2];
}

int main() {
  ll h, w, k;
  cin >> h >> w >> k;
  vector<string> mp(h);
  rep(i, h) { cin >> mp[i]; }

  vvll csum(h + 1, vll(w + 1, 0));
  rep(i, h) {
    rep(j, w) {
      ll add = (mp[i][j] == '1') ? 1 : 0;
      csum[i + 1][j + 1] = csum[i + 1][j] + csum[i][j + 1] - csum[i][j] + add;
    }
  }

  ll ans = LONG_LONG_MAX;
  rep(dh, 1 << (h - 1)) {
    bool dhfail = false;
    vll hlst;
    rep(hh, h - 1) {
      if (dh & 1 << hh) hlst.push_back(hh);
    }
    hlst.push_back(h - 1);
    ll cnt = 0;
    ll prevx = 0;
    for (ll x = 1; x <= w; x++) {
      ll prevy = 0;
      rep(ii, (ll)hlst.size()) {
        ll y = hlst[ii] + 1;
        bool cond = get(csum, x, y, prevx, prevy) > k;
        if (cond) {
          cnt++;
          if(prevx == x - 1){
            dhfail = true;
          } else {
            prevx = x - 1;
            x--;
          }
          break;
        }
        prevy = y;
      }  // ii
    }    // x
    if (dhfail) continue;
    chmin(ans, cnt + (ll)hlst.size() - 1);
  }  // dh

  cout << ans << endl;

  return 0;
}