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.42.0) (judge verdict: Accepted)

#[allow(unused_imports)]
use itertools::Itertools;
#[allow(unused_imports)]
use num::*;
use proconio::input;
#[allow(unused_imports)]
use proconio::marker::*;
#[allow(unused_imports)]
use std::collections::*;

pub fn bsearch<F>(ok: i64, ng: i64, pred: F) -> Option<i64>
where
    F: Fn(i64) -> bool,
{
    let orig_ok = ok;
    let mut ok = ok;
    let mut ng = ng;
    while (ok - ng).abs() > 1 {
        let mid = (ok + ng) / 2;
        if pred(mid) {
            ok = mid;
        } else {
            ng = mid;
        }
    }
    if ok == orig_ok {
        None
    } else {
        Some(ok)
    }
}

fn solve() {
    input! {
        h: usize, w: usize, k: usize,
        grid: [Chars; h]
    };

    let mut cusum = vec![vec![0; w + 1]; h + 1];
    for i in 0..h {
        for j in 0..w {
            let c = if grid[i][j] == '1' { 1 } else { 0 } + cusum[i + 1][j] + cusum[i][j + 1]
                - cusum[i][j];
            cusum[i + 1][j + 1] = c;
        }
    }

    let mut res = std::usize::MAX;
    for h_bits in 0..1 << (h - 1) {
        let mut range = Vec::new();
        let mut s = 0;
        for i in 0..(h - 1) {
            if (h_bits >> i) & 1 == 1 {
                range.push((s, i));
                s = i + 1;
            }
        }
        range.push((s, h - 1));
        let mut count = range.len() - 1;
        let mut l = 0;
        while l < w {
            let j = bsearch(l as i64 - 1, w as i64, |x| {
                let j = x as usize;
                range.iter().all(|&(s, e)| {
                    let c = cusum[e + 1][j + 1] + cusum[s][l] - cusum[e + 1][l] - cusum[s][j + 1];
                    c <= k
                })
            });
            if let Some(j) = j {
                l = j as usize + 1;
                if l < w {
                    count += 1;
                }
            } else {
                count = std::usize::MAX;
                break;
            }
        }
        if count < res {
            res = count;
        }
    }
    println!("{}", res);
}

fn main() {
    std::thread::Builder::new()
        .name("big stack size".into())
        .stack_size(256 * 1024 * 1024)
        .spawn(|| {
            solve();
        })
        .unwrap()
        .join()
        .unwrap();
}