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 Java8 (OpenJDK 1.8.0) (judge verdict: Accepted)

import java.util.*;

public class Main {

    static Map<Integer, Integer> ansMap = new HashMap<>();
    static int H,W,K;
    static int[][] sums;
    static int[][] s;
    public static void main(String args[]){
        Scanner sc = new Scanner(System.in);
        H = sc.nextInt();
        W = sc.nextInt();
        K = sc.nextInt();
        sums = new int[W][H+1];
        s = new int[W][H];
        List<List<Integer>> S = new ArrayList<>();
        for(int i=0;i<W;i++){
            sums[i][0]=0;
        }
        for(int r=0;r<H;r++){
            List<Integer> row = new ArrayList<>();
            String sline = sc.next();
            for(int c=0;c<W;c++){
                int val = sline.charAt(c)-'0';
                s[c][r] = val;
                row.add(val);
                sums[c][r+1] = sums[c][r] + val;
            }
            S.add(row);
        }
        int minCut = 1_000_000_000;
        for(int code=0;code<Math.pow(2,H-1);code++){
            //System.out.println(code +":"+getAns(code) + "-" + bitcnt(code));
            minCut = Math.min(minCut, getAns(code)+bitcnt(code));
        }
        System.out.println(minCut);
    }

    private static int bitcnt(int a){
        int sum=0;
        while(a > 0){
            if((a & 0x01) >0){
                sum++;
            }
            a = (a >> 1);
        }
        return sum;
    }

    private static int getAns(int code){
        int[] groups = new int[H];
        groups[0]=0;
        for(int i=0;i<H-1;i++){
            groups[i+1] = groups[i] + ((code >> i) & 0x01);
        }
        int groupCnt = groups[H-1]+1;
        int[] nowSum = new int[groupCnt];
        int[] nxtSum = new int[groupCnt];
        Arrays.fill(nowSum, 0);
        Arrays.fill(nxtSum, 0);
        int split=0;
        for(int w=0;w<W;w++){
            boolean cutFlag = false;
            for(int g=0;g<groupCnt;g++){
                nxtSum[g] = nowSum[g];
            }
            for(int h=0;h<H;h++){
                nxtSum[groups[h]] += s[w][h];
                if(nxtSum[groups[h]] > K){
                    cutFlag = true;
                }
            }
            if(cutFlag){
                split++;
                for(int i=0;i<groupCnt;i++) {
                    nowSum[i] = nxtSum[i]-nowSum[i];
                    if(nowSum[i] > K){
                        return 1_000_000_000;
                    }
                }
            }else{
                for(int i=0;i<groupCnt;i++) {
                    nowSum[i] = nxtSum[i];
                }
            }
        }
        return split;
    }
    private static void printInts(int[] a){
        for(int p:a){
            System.out.print(p);
        }
        System.out.println();
    }
}