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::collections::HashSet;

fn solve() {
    let s = std::io::stdin();
    let mut sc = Scanner { stdin: s.lock() };
    let h: usize = sc.read();
    let w: usize = sc.read();
    let k: i32 = sc.read();
    let board: Vec<Vec<char>> = (0..h).map(|_| sc.chars()).collect();
    let mut white: Vec<Vec<i32>> = vec![vec![0; w]; h];
    //white[0][0] = if board[0][0] == '0' { 1 } else { 0 };
    for i in 0..h {
        for j in 0..w {
            let c = if board[i][j] == '1' { 1 } else { 0 };
            let up = if i >= 1 { white[i - 1][j] } else { 0 };
            let left = if j >= 1 { white[i][j - 1] } else { 0 };
            let up_left = if i >= 1 && j >= 1 {
                white[i - 1][j - 1]
            } else {
                0
            };
            white[i][j] = c + up + left - up_left;
        }
    }
    let mut ans = 1 << 30;
    for bit in 0..(1 << h) {
        let mut cuts = vec![];

        for i in 0..h - 1 {
            if (bit >> i) & 1 > 0 {
                cuts.push(i);
            }
        }
        cuts.push(h - 1);
        let mut x_cuts: HashSet<usize> = HashSet::new();
        let mut result = 0;
        let mut pre_x: Option<usize> = None;
        let mut ok = true;
        for x in 0..w {
            let mut pre_y: Option<usize> = None;
            for &y in cuts.iter() {
                let mut sum_white = white[y][x];
                if let Some(pre_y) = pre_y {
                    sum_white -= white[pre_y][x];
                    if let Some(pre_x) = pre_x {
                        sum_white -= white[y][pre_x];
                        sum_white += white[pre_y][pre_x];
                    }
                } else {
                    if let Some(pre_x) = pre_x {
                        sum_white -= white[y][pre_x];
                    }
                }
                pre_y = Some(y);
                //eprintln!("{} {} {}", y, x, sum_white);
                if sum_white > k {
                    if x >= 1 {
                        pre_x = Some(x - 1);
                        x_cuts.insert(x - 1);
                    } else {
                        ok = false;
                    }
                    result += 1;
                    break;
                }
            }
        }
        if ok {
            ans = std::cmp::min(ans, result + cuts.len() - 1);
        }
    }
    println!("{}", ans);
}

fn main() {
    std::thread::Builder::new()
        .stack_size(64 * 1024 * 1024) // 64MB
        .spawn(|| solve())
        .unwrap()
        .join()
        .unwrap();
}
pub mod utils {
    const DYX: [(isize, isize); 8] = [
        (0, 1),   //right
        (1, 0),   //down
        (0, -1),  //left
        (-1, 0),  //top
        (1, 1),   //down right
        (-1, 1),  //top right
        (1, -1),  //down left
        (-1, -1), //top left
    ];

    pub fn try_adj(y: usize, x: usize, dir: usize, h: usize, w: usize) -> Option<(usize, usize)> {
        let ny = y as isize + DYX[dir].0;
        let nx = x as isize + DYX[dir].1;
        if ny >= 0 && nx >= 0 {
            let ny = ny as usize;
            let nx = nx as usize;
            if ny < h && nx < w {
                Some((ny, nx))
            } else {
                None
            }
        } else {
            None
        }
    }
}

//snippet from kenkoooo
pub struct Scanner<R> {
    stdin: R,
}

impl<R: std::io::Read> Scanner<R> {
    pub fn read<T: std::str::FromStr>(&mut self) -> T {
        use std::io::Read;
        let buf = self
            .stdin
            .by_ref()
            .bytes()
            .map(|b| b.unwrap())
            .skip_while(|&b| b == b' ' || b == b'\n' || b == b'\r')
            .take_while(|&b| b != b' ' && b != b'\n' && b != b'\r')
            .collect::<Vec<_>>();
        unsafe { std::str::from_utf8_unchecked(&buf) }
            .parse()
            .ok()
            .expect("Parse error.")
    }
    pub fn read_line(&mut self) -> String {
        use std::io::Read;
        let buf = self
            .stdin
            .by_ref()
            .bytes()
            .map(|b| b.unwrap())
            .skip_while(|&b| b == b'\n' || b == b'\r')
            .take_while(|&b| b != b'\n' && b != b'\r')
            .collect::<Vec<_>>();
        unsafe { std::str::from_utf8_unchecked(&buf) }
            .parse()
            .ok()
            .expect("Parse error.")
    }
    pub fn vec<T: std::str::FromStr>(&mut self, n: usize) -> Vec<T> {
        (0..n).map(|_| self.read()).collect()
    }

    pub fn chars(&mut self) -> Vec<char> {
        self.read::<String>().chars().collect()
    }
}