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 Rust (1.15.1) (judge verdict: Accepted)

use std::cmp::min;
use std::fs;
use std::io::{stdin, Read};

macro_rules! parse {
    ($it: ident) => {
        $it.next().unwrap().parse().unwrap()
    };
    ($it: ident,2) => {
        (parse!($it), parse!($it))
    };
    ($it: ident,3) => {
        (parse!($it), parse!($it), parse!($it))
    };
    ($it: ident,4) => {
        (parse!($it), parse!($it), parse!($it), parse!($it))
    };
    ($it: ident, $n: expr) => {
        (&mut $it).take($n).map(|e| e.parse().unwrap()).collect();
    };
}

fn solve(s: &str) {
    let mut it = s.split_whitespace();
    let (h, w, k): (usize, usize, usize) = parse!(it, 3);
    let g: Vec<Vec<bool>> = (0..h)
        .map(|_| it.next().unwrap().chars().map(|x| x == '1').collect())
        .collect();
    let mut cwhites = vec![vec![0; w + 1]; h + 1];
    for i in 0..h {
        for j in 0..w {
            cwhites[i + 1][j + 1] =
                cwhites[i][j + 1] + cwhites[i + 1][j] - cwhites[i][j] + (g[i][j] as usize);
        }
    }
    let count_whites = |l: usize, r: usize, u: usize, d: usize| -> usize {
        cwhites[d][r] + cwhites[u][l] - cwhites[d][l] - cwhites[u][r]
    };
    let mut ret = std::usize::MAX / 10;
    'outer: for hi in 0..(1 << (h - 1)) {
        let hi: usize = hi;
        let mut pret = hi.count_ones() as usize;
        let mut bounds: Vec<usize> = (0..(h - 1))
            .filter(|i| (hi >> i) & 1 == 1)
            .map(|x| x + 1)
            .collect();
        bounds.push(h);
        let mut l = 0;
        for r in 1..(w + 1) {
            let mut u = 0;
            for &d in bounds.iter() {
                if count_whites(l, r, u, d) > k {
                    if l == r - 1 {
                        continue 'outer;
                    }
                    l = r - 1;
                    pret += 1;
                }
                u = d;
            }
        }
        ret = min(pret, ret);
    }
    println!("{}", ret);
}

fn test() {
    let mut f = fs::File::open("foo.txt").unwrap();
    let mut s = String::new();
    f.read_to_string(&mut s).unwrap();
    solve(&s);
}

fn main() {
    let mut s = String::new();
    stdin().read_to_string(&mut s).unwrap();
    solve(&s);
}

#[cfg(test)]
mod tests {
    use super::*;
    #[test]
    fn test_input() {
        let s = "
        3 5 4
11100
10001
00111
        ";
        solve(s);
    }
}