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: Runtime Error)

#include <bits/stdc++.h>
using namespace std;

#define ll long long
#define fw(p) for(int w=0;w<(p);w++)
#define fx(p) for(int x=0;x<(p);x++)
#define fy(p) for(int y=0;y<(p);y++)
#define fz(p) for(int z=0;z<(p);z++)
#define fyg(p,g) for(int y=(g);y<(p);y++)
#define fzg(p,g) for(int z=(g);z<(p);z++)
#define ce(d) cout<<d<<endl;
#define vecp(p) int aa;cin>>aa;(p).push_back(aa);
#define vecpl(p) long long aa;cin>>aa;(p).push_back(aa);
#define vecps(p) string aa;cin>>aa;(p).push_back(aa);
#define vecp2(p) cin>>aa;(p).push_back(aa);
#define vecpl2(p) long long a	b;cin>>ab;(p).push_back(ab);
#define vecps2(p) string ab;cin>>ab;(p).push_back(ab);
#define sorts(c) sort((c).begin(),(c).end());
#define reverses(c) reverse((c).begin(),(c).end());
#define vec(b) vector<int> (b);
#define vecl(b) vector<long long> (b);
#define vecs(b) vector<string> (b);
#define pb(b,a) (b).push_back((a));
#define doublece(a,b) cout<<(a)<<' '<<(b)<<endl;
#define pairs(s) vector<pair<int,int>> (s);
#define pairsp(s) int aa,bb;cin>>aa>>bb;(s).push_back(make_pair(aa,bb));
#define MOD 1000000007
#define cey ce("Yes")
#define cen ce("No")
#define ceY ce("YES")
#define ceN ce("NO")

int choco[11][1001];
int main()
{
	int H, W, K;
	cin >> H >> W >> K;
	vector<string> S;
	fx(H) {
		vecps(S);
	}

	fy(W) {
		choco[1][y+1] = S[0][y] == '1' ? 1 : 0;
		fx(H - 1) {
			choco[x + 2][y+1] = choco[x+1][y+1];
			if (S[x + 1][y] == '1') {
				choco[x + 2][y+1]++;
			}
		}
	}
	int ans = INT_MAX;
	fx(2 << H ) {
		vec(B);
		pb(B, 0);
		int y = x;
		int i = 0;
		while (y > 0) {
			if ((y & 1) == 1) {
				pb(B, i+1);
			}
			i++;
			y >>= 1;
		}
		pb(B, H)
		int sum = B.size() - 2;
		vector<int> SUMS(B.size() - 1);
		fw(W+1) {
			bool flag = false;
			fz(B.size() - 1) {
				SUMS[z] += choco[B[z + 1]][w] - choco[B[z]][w];
				if (w + 1 <= W && SUMS[z] + choco[B[z + 1]][w + 1] - choco[B[z]][w+1] > K) {
					flag = true;
					break;
				}
			}
			if (flag) {
				sum++;
				SUMS = vector<int>(B.size() - 1, 0);
			}
		}

		ans = min(ans, sum);

	}
	ce(ans)
    return 0;
}