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: Wrong Answer)

#![allow(unused_imports)]
#![allow(non_snake_case)]
use std::cmp::*;
use std::collections::*;
use std::io::Write;

#[allow(unused_macros)]
macro_rules! debug {
    ($($e:expr),*) => {
        #[cfg(debug_assertions)]
        $({
            let (e, mut err) = (stringify!($e), std::io::stderr());
            writeln!(err, "{} = {:?}", e, $e).unwrap()
        })*
    };
}

fn main() {
    let v = read_vec::<usize>();
    let (h, w, k) = (v[0], v[1], v[2] as i64);

    let mut grid = vec![vec![]; h];
    for y in 0..h {
        grid[y] = read::<String>()
            .chars()
            .map(|x| x.to_digit(10).unwrap() as i64)
            .collect::<Vec<_>>();
    }
    let mut cum_sum = CumulativeSum2D::new(w, h);
    for y in 0..h {
        for x in 0..w {
            cum_sum.add(x, y, grid[y][x]);
        }
    }
    cum_sum.build();

    let mut ans = std::i64::MAX;
    'outer: for state in 0..1 << h - 1 {
        let mut count = 0;
        let mut spans = vec![];
        let mut cur = 0;
        for i in 0..h - 1 {
            if (state >> i) & 1 == 1 {
                spans.push((cur, i));
                cur = i;
            }
        }
        spans.push((cur, h));
        // debug!(spans);

        let mut start_x = 0;
        for last_x in 0..w {
            let mut found = false;
            for &(start_y, end_y) in &spans {
                let white_in_area = cum_sum.query(start_x, start_y, last_x + 1, end_y);
                if white_in_area > k {
                    if last_x == start_x {
                        continue 'outer;
                    }
                    found = true;
                }
            }
            if found {
                count += 1;
                start_x = last_x;
            }
        }

        ans = min(ans, count + (spans.len() - 1) as i64);
    }
    println!("{}", ans);
}

fn read<T: std::str::FromStr>() -> T {
    let mut s = String::new();
    std::io::stdin().read_line(&mut s).ok();
    s.trim().parse().ok().unwrap()
}

fn read_vec<T: std::str::FromStr>() -> Vec<T> {
    read::<String>()
        .split_whitespace()
        .map(|e| e.parse().ok().unwrap())
        .collect()
}

struct CumulativeSum2D {
    data: Vec<Vec<i64>>,
}

impl CumulativeSum2D {
    fn new(w: usize, h: usize) -> CumulativeSum2D {
        CumulativeSum2D {
            data: vec![vec![0; w + 1]; h + 1],
        }
    }

    fn add(&mut self, mut x: usize, mut y: usize, z: i64) {
        x += 1;
        y += 1;
        if x >= self.data[0].len() || y >= self.data.len() {
            return;
        }
        self.data[y][x] += z;
    }

    fn build(&mut self) {
        for i in 1..self.data.len() {
            for j in 1..self.data[i].len() {
                self.data[i][j] +=
                    self.data[i][j - 1] + self.data[i - 1][j] - self.data[i - 1][j - 1];
            }
        }
    }

    // sum of [sx, gx), [sy, gy)
    fn query(&self, sx: usize, sy: usize, gx: usize, gy: usize) -> i64 {
        self.data[gy][gx] + self.data[sy][sx] - self.data[gy][sx] - self.data[sy][gx]
    }
}