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)

#include"bits/stdc++.h"

using namespace std; 

typedef vector<int> VI;
typedef vector<VI> VVI;
typedef vector<string> VS;
typedef pair<int, int> PII;
typedef vector<PII> VPII;
typedef long long LL;
typedef priority_queue<int> PQ_DESC;
typedef priority_queue<int, vector<int>, greater<int>> PQ_ASC;
typedef priority_queue<PII> PQ_DESC_PII;
typedef priority_queue<PII, vector<PII>, greater<PII>> PQ_ASC_PII;

#define ALL(c) (c).begin(),(c).end()
#define PB push_back
#define MP make_pair
#define SORT_ASC(c) sort(ALL(c))
#define SORT_DESC(c) sort(ALL(c), greater<typeof(*((c).begin()))>())
#define SIZE(a) int((a).size())

#define FOR(i,a,b) for(int i=(a);i<(b);++i)
#define ROF(i,a,b) for(int i=(b-1);i>=(a);--i)
#define REP(i,n) FOR(i,0,n)
#define PER(i,n) ROF(i,0,n)

const double EPS = 1e-10;
const double PI  = acos(-1.0);
const int LARGE_INT = 1e9+100;
const int INF = 2e9+100;
const LL INF_LL = (LL)INF * 300 * 300;
const int MOD = 1e9+7;

//debug
#define dump(x)  cerr << #x << " = " << (x) << endl;
#define debug(x) cerr << #x << " = " << (x) << " (L" << __LINE__ << ")" << " " << __FILE__ << endl;

LL modpow(LL a, LL n) {
    LL res = 1;
    while (n > 0) {
        if (n & 1) res = res * a % MOD;
        a = a * a % MOD;
        n >>= 1;
    }
    return res;
}

LL modinv(LL a) {
    LL b = MOD, u = 1, v = 0;
    while (b) {
        LL t = a / b;
        a -= t * b; swap(a, b);
        u -= t * v; swap(u, v);
    }
    u %= MOD;
    if (u < 0) u += MOD;
    return u;
}

LL moddev(LL a, LL b){
	return a * modinv(b) % MOD;
}

VVI s;

int check(int a, int b, int c, int d){
    int r=0;
    FOR(i,a,b){
        FOR(j,c,d){
            r += s[i][j];
        }
    }
    return r;


}

void Main()
{
    int h,w,z;cin>>h>>w>>z;

    s.resize(h);
    REP(i,h){
        s[i].resize(w);
        REP(j,w){
            char tmp;cin>>tmp;
            s[i][j] = tmp-'0';
        }
    }

    int x = modpow(2,h-1);
    //dump(x)

    int result = INF;

    REP(i,x){
        int cuts=0;
        //VVI t;
        //t.push_back(s[0]);
        int tt = 0;
        int xx = i;
        while(xx>0){
            cuts += xx%2;
            xx/=2;
        }
        xx = i;
        //t.resize(cuts+1);
        VVI t(cuts+1);
        REP(j,cuts+1){
            t[j].resize(w);
        }
        REP(j,w){
            t[0][j] = s[0][j];
        }
        FOR(k,1,h){
            dump(xx)
            if(xx%2 == 0){
                REP(j,w){
                   t[tt][j] += s[k][j];
                }
            }
            else{
                tt++;
                REP(j,w){
                   t[tt][j] = s[k][j];
                }
                //cuts++;
            }
            xx /= 2;
        }
        int s_t = tt+1;
        VI now(s_t);
        
        //dump(s_t)
        //dump(tt)
        dump(i)
        REP(ii,s_t){
            REP(jj,w){
                cerr<<t[ii][jj];
            }
            cerr<<endl;
        }

        REP(j,w){
            //VI now_new(s_t);

            int m_t=0;

            REP(k,s_t){
                //now_new[k] = now[k]+t[k][w]; 
                //m_t = max(m_t,now_new[k]);
                now[k] += t[k][j]; 
                m_t = max(m_t,now[k]);
            }


            if(m_t > z){
                cuts++;
                REP(k,s_t){
                    now[k]=t[k][j];
                }
            }

        }


        result = min(result,cuts);

    }

    cout<<result<<endl;








	return;
}

int main()
{
	cin.tie(nullptr);
	ios_base::sync_with_stdio(false);
	cout << fixed << setprecision(15);
	Main();
    return 0;
}