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: Wrong Answer)

/*
これを入れて実行
g++ code.cpp
./a.out
 */
 
#include <iostream>
#include <stdio.h>
#include <vector>
#include <string>
#include <cstring>
#include <queue>
#include <deque>
#include <algorithm>
#include <utility>
#include <set>
#include <map>
#include <unordered_map>
#include <cmath>
#include <math.h>
#include <tuple>
#include <iomanip>
#include <bitset>
#include <functional>

#define all(x) (x).begin(),(x).end()
#define rep(i, n) for (int i = 0; i < (int)(n); i++)
 
using namespace std;
typedef long long ll;
typedef long double ld;
 
int dy4[4] = {-1, 0, +1, 0};
int dx4[4] = {0, +1, 0, -1};
int dy8[8] = {-1, -1, 0, 1, 1, 1, 0, -1};
int dx8[8] = {0, 1, 1, 1, 0, -1, -1, -1};
 
const long long INF = 1LL << 62;
const ll MOD = 1e9 + 7;
 
bool greaterSecond(const pair<int, int>& f, const pair<int, int>& s){
    return f.second > s.second;
}
 
ll gcd(ll a, ll b){
	if (b == 0)return a;
	return gcd(b, a % b);
}
 
ll lcm(ll a, ll b){
    return a / gcd(a, b) * b;
}
 
ll nCr(ll n, ll r){
    if(r == 0 || r == n){
        return 1;
    } else if(r == 1){
        return n;
    }
    return (nCr(n - 1, r) + nCr(n - 1, r - 1));
}
 
ll nPr(ll n, ll r){
    r = n - r;
    ll ret = 1;
    for (ll i = n; i >= r + 1; i--) ret *= i;
    return ret;
}
 
//-----------------------ここから-----------
int main(void){
    int h, w, K;
    cin >> h >> w >> K;

    vector<string> s(h);
    rep(i, h){
        cin >> s[i];
    }

    vector<vector<int>> hsum(w, vector<int>(h + 1, 0));
    //cout << "---" << endl;
    rep(i, w){
        rep(j, h){
            if(s[j][i] == '1'){
                hsum[i][j + 1] += hsum[i][j] + 1;
            } else {
                hsum[i][j + 1] += hsum[i][j];
            }
        }
    }
    // rep(i, h + 1){
    //     rep(j, w){
    //         cout << hsum[j][i];
    //     }
    //     cout << endl;
    // }
    bool dflag = false;
    int ans = 1e9;
    for(int bit = 0; bit < (1 << h - 1); bit++){
        vector<int> cut;
        for(int i = 0; i < h - 1; i++){
            if(bit & (1 << i)){
                cut.push_back(i);
            }
        }
        //if(bit == 1) dflag = true;
        vector<vector<int>> sum(cut.size() + 1, vector<int>(w + 1, 0));
        int lastcut = 0;
        int tmpans = 0;
        bool fff = false;
        for(int i = 0; i < w; i++){
            int now = 0;
            bool c = false;
            for(int j = 0; j < cut.size(); j++){
                int tmpsum = 0;
                for(int jj = now; jj <= cut[j]; jj++){
                    if(s[jj][i] == '1') tmpsum++;
                }
                sum[j][i + 1] = sum[j][i] + tmpsum;
                if(dflag) cout << j << " " << i + 1 << " " << sum[j][i + 1] << endl;
                if(sum[j][i + 1] - sum[j][lastcut] > K){
                    c = true;
                }
                now = cut[j] + 1;
            }
            int tmpsum = 0;
            for(int jj = now; jj < h; jj++){
                if(s[jj][i] == '1') tmpsum++;
            }
            sum[cut.size()][i + 1] = sum[cut.size()][i] + tmpsum;
            if(dflag) cout << i + 1 << " f " <<sum[cut.size()][i + 1] << endl;
            if(sum[cut.size()][i + 1] - sum[cut.size()][lastcut] > K){
                c = true;
            }

            if(c){
                if(w == 1){
                    fff = true;
                    break;
                }
                lastcut = i;
                tmpans++;
                if(dflag) cout << "H" << endl;
            }
        }
        if(fff) continue;
        ans = min(ans, tmpans + __builtin_popcount(bit));
        dflag = false;
    }
    cout << ans << endl;
}