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: Accepted)

// https://atcoder.jp/contests/abc159/tasks/abc159_e
// 
#include<bits/stdc++.h>
using namespace std;
#define rep(i, n) for (int i = 0; i < (int)(n); i++)
#define is_in(i, s) (s.find(i) != s.end())
#define all(x) (x).begin(),(x).end()
#define outl(x) cout<<x<<endl
#define outsp(x) cout<<x<<" "
typedef long long ll;
typedef pair<int,int> P;


#define MAX_H 11
#define MAX_W 1005
const int INF = 1e9;
bool fld[MAX_H][MAX_W];
int count_bit(int bits){
	// 2進数表記時の1の数を数える
    bits = (bits & 0x55555555) + (bits >> 1 & 0x55555555);
    bits = (bits & 0x33333333) + (bits >> 2 & 0x33333333);
    bits = (bits & 0x0f0f0f0f) + (bits >> 4 & 0x0f0f0f0f);
    bits = (bits & 0x00ff00ff) + (bits >> 8 & 0x00ff00ff);
    return (bits & 0x0000ffff) + (bits >>16 & 0x0000ffff);	
}
int main() {
	int h,w,K;
	cin >> h >> w >> K;
	rep(i,h){
		string s;
		cin >> s;
		rep(j,w) fld[i][j] = s[j]=='1';
	}
	int ans = INF;
	int block_n[MAX_H]; // ブロックごとのホワイトチョコの数
	rep(S,1<<(h-1)){
		int cut_n=0;
		int bn = count_bit(S)+1;
		rep(i,bn)block_n[i]=0;
		bool impossible=false;
		rep(j,w){
			int block_i=0;
			int cur_block_n[MAX_H];
			bool do_cut=false;
			rep(k,bn)cur_block_n[k]=0;
			rep(k,h){
				cur_block_n[block_i] += (int)fld[k][j];
				if(cur_block_n[block_i]>K){
					impossible = true;
					break;
				}
				if(S>>k&1)block_i++;
			}
			if(impossible)break;
			rep(k,bn){
				if(block_n[k]+cur_block_n[k]>K){
					do_cut = true;
					break;
				}
			}
			if(do_cut){
				cut_n++;
				rep(k,bn)block_n[k] = cur_block_n[k];
			}else{
				rep(k,bn)block_n[k] += cur_block_n[k];
			}
		}
		if(!impossible)ans = min(ans,bn-1+cut_n);
	}
	outl(ans);
    return 0;
}