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>
#include <iomanip>
#include <math.h>
#define ll long long
#define lld double
#define vi vector<int>
#define pii pair<int,int>
#define piii pair<pii, ll>
#define pb push_back
#define mp make_pair
#define fs first
#define sc second
#define ppb pop_back
#define d0(x) cout<<x<<" "
#define d1(x) cout<<(x)<<"\n"
#define d2(x,y) cout<<x<<" "<<y<<"\n"
#define d3(x,y,z) cout<<x<<" "<<y<<" "<<z<<endl
#define d4(x,y,z,w) cout<<x<<" "<<y<<" "<<z<<" "<<w<<endl
#define d5(x,y,z,w,s) cout<<x<<" "<<y<<" "<<z<<" "<<w<<" "<<s<<endl
using namespace std;
const ll mod = 998244353;


int a[11][1001];int H,W,K;char c[11][1001];int po[11];int s[11][1001];

int sum(int x1,int y1,int x2,int y2)
{
    return s[x2][y2]-s[x1][y2] - s[x2][y1] + s[x1][y1];
}
void solve(int t)
{
    cin>>H>>W>>K;
    for(int i=1;i<=H;i++)
    {
        cin>>(c[i]+1);
        for(int j=1;j<=W;j++)
            a[i][j] = ('1' - c[i][j]);
    }
    for(int i=0;i<=H;i++)s[i][0]=0;
    for(int i=0;i<=W;i++)s[0][i]=0;

    for(int i=1;i<=H;i++)
        for(int j=1;j<=W;j++)
        {
            s[i][j] = a[i][j]+s[i-1][j]+s[i][j-1]-s[i-1][j-1];
        }
    /*for(int i=0;i<=H;i++)
    {
        for(int j=0;j<=W;j++)
        {
            d0(s[i][j]);
        }
        d1("");
    }*/
    int ans = (H-1)+(W-1);

    po[0]=1;
    for(int i=1;i<=H;i++)
        po[i] = 2*po[i-1];

    for(int ii=0;ii<po[H-1];ii++)
    {
        int tmpans = 0;
        vi cuts;
        cuts.pb(0);
        for(int j=0;j<H-1;j++)
        {
            if((ii&po[j])!=0)
            {
                tmpans++;
                cuts.pb(j+1);
            }
        }
        cuts.pb(H);
        if(tmpans>=ans)
            continue;
        int m = cuts.size()-1;
        vi crrs(m);
        for(int i=0;i<m;i++)
            crrs[i]=0;

        //d2("#m",m);
        for(int i=1;i<=W;i++)
        {
            int maxs = 0;
            for(int j=0;j<m;j++)
            {
                crrs[j] += sum(cuts[j],i-1,cuts[j+1],i);

                maxs = max(crrs[j],maxs);
            }
            if(maxs>K )
            {
                //d3(maxs,K,ii);
                tmpans++;
                for(int j=0;j<m;j++)
                    crrs[j]=0;
            }
        }
        /*if(tmpans<ans)
        {
            for(int i=0;i<=m;i++)
                d0(cuts[i]);
            d1("");
            d2("#",tmpans);
        }*/
        ans = min(tmpans,ans);

    }

    cout<<ans<<"\n";

}


int main()
{
    ios_base::sync_with_stdio(0);cin.tie(0);cout.tie(0);
    int T=1;
    //cin>>T;
    for(int t=1;t<=T;t++)
        solve(t);
}