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++ (GCC 9.2.1) (judge verdict: Wrong Answer)

#include<bits/stdc++.h>
#include <ext/pb_ds/tree_policy.hpp>
#include <ext/pb_ds/assoc_container.hpp>
#include<chrono>
using namespace std;
using namespace std::chrono;      
using namespace __gnu_pbds; 
#define fastio     ios_base::sync_with_stdio(0);cin.tie(0);cout.tie(0)
#define fi         first
#define se         second
#define int         long long 
#define pb         push_back
#define emp        emplace_back
#define vv(x)      vector<x>
#define mp(x,y)    map<x,y>
#define dq(x)      deque<x>
#define pql(x)     priority_queue<x>
#define pqs(x)     priority_queue<x,vv(x),greater<x> >
#define M          1000000007
#define forf(i,a,b) for(int i=a;i<b;i++)
#define it(x)      x::iterator
#define ll         long long 
#define debug(...) fprintf(stderr, __VA_ARGS__), fflush(stderr)
#define time__(d)  for(long blockTime = 0; (blockTime == 0 ? (blockTime=clock()) != 0 : false); debug("%s time : %.4fs", d, (double)(clock() - blockTime) / CLOCKS_PER_SEC))
#define vii        vector<int>
#define big        1e18
#define sm         -2e9
#define mkr        make_pair
#define vpi        vector<pair<int,int> >
#define pii        pair<int,int>
#define rng        500005
#define sz(x)      (int)x.size()
#define rv(x)      reverse(x.begin(),x.end())
#define out(x)     cout<<x.fi<<" "<<x.se<<endl;

//#define ordered_set tree<pii, null_type,less<pii>, rb_tree_tag,tree_order_statistics_node_update> 

void pr_init()
{

#ifndef ONLINE_JUDGE
freopen("gin.txt", "r", stdin);
freopen("gout.txt", "w", stdout);

#endif
}


int h,w,ch;
char mat[11][1001];
vector<vpi> segs;

void recur(int i,int n,vpi cur)
{
if(i>n)
{
   segs.pb(cur);
   return ;
}

for(int j=i;j<=n;j++)
{
    cur.pb({i,j});
    recur(j+1,n,cur);
    cur.pop_back();
}

}
void solve()
{

cin >> h >> w >> ch;

for(int i=1;i<=h;i++)
{
for(int j=1;j<=w;j++) cin >> mat[i][j];
}
vpi cur;
recur(1,h,cur);
int an = big;
for(auto x:segs)
{
int cur = sz(x)-1;
vii vls(sz(x),0);

for(int j=1;j<=w;j++)
{
    for(int k=0;k<sz(x);k++)
    {
      for(int l = x[k].fi ; l<=x[k].se ; l++)
      {
        if(mat[l][j] == '1') vls[k]++;
      }
    }
    

    for(int k=0;k<sz(x);k++) 
    if(vls[k]>ch)
    {
      cur ++ ;
      for(int k=0;k<sz(x);k++) 
        {
             vls[k]=0;
             for(int l = x[k].fi ; l<=x[k].se ; l++)
              {
                 if(mat[l][j] == '1') vls[k]++;
              }
              if(vls[k]>ch) cur += big;
            
        }
        break;
    }

}
an = min(an,cur);
}
if(an>=big) an = -1;

cout<<an<<"\n";

}


int32_t main()
{
pr_init();
fastio;

auto start = high_resolution_clock::now(); 

solve();
auto stop = high_resolution_clock::now();
auto duration = duration_cast<microseconds>(stop - start); 
// cout << "Time taken by function: "
//       << duration.count() << " microseconds" << endl; 

}