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(clippy::needless_range_loop)]

macro_rules! input {
    (source = $s:expr, $($r:tt)*) => {
        let mut iter = $s.split_whitespace();
        input_inner!{iter, $($r)*}
    };
    ($($r:tt)*) => {
        #[allow(unused_mut)]
        let mut s = {
            use std::io::Read;
            let mut s = String::new();
            std::io::stdin().read_to_string(&mut s).unwrap();
            s
        };
        let mut iter = s.split_whitespace();
        input_inner!{iter, $($r)*}
    };
}

macro_rules! input_inner {
    ($iter:expr) => {};
    ($iter:expr, ) => {};

    ($iter:expr, $var:ident : $t:tt $($r:tt)*) => {
        let $var = read_value!($iter, $t);
        input_inner!{$iter $($r)*}
    };
}

macro_rules! read_value {
    ($iter:expr, ( $($t:tt),* )) => {
        ( $(read_value!($iter, $t)),* )
    };

    ($iter:expr, [ $t:tt ; $len:expr ]) => {
        (0..$len).map(|_| read_value!($iter, $t)).collect::<Vec<_>>()
    };

    ($iter:expr, chars) => {
        read_value!($iter, String).chars().collect::<Vec<char>>()
    };

    ($iter:expr, usize1) => {
        read_value!($iter, usize) - 1
    };

    ($iter:expr, $t:ty) => {
        $iter.next().unwrap().parse::<$t>().expect("Parse error")
    };
}

struct Bits(usize);

impl Bits {
    fn at(&self, index: usize) -> bool {
        self.0 & 1 << index > 0
    }

    fn set_bits(&self) -> usize {
        let mut n = self.0;
        let mut count = 0;
        while n != 0 {
            n &= n - 1;
            count += 1;
        }
        count
    }
}

macro_rules! n_bits_range {
    ($bits:expr) => {
        (0..(1 << $bits)).map(Bits)
    };
}

fn main() {
    input! {
        h: usize,
        w: usize,
        k: usize,
        s: [chars; h]
    }

    let mut ans = 99999999;
    for bits in n_bits_range!(h - 1) {
        let indices = (0..h).scan(0, |state, x| {
            let current = *state;
            *state += bits.at(x) as usize;
            Some(current)
        }).collect::<Vec<_>>();
        let mut sum = vec![0; h];
        let mut cnt = bits.set_bits();
        'quit: for x in 0..w {
            for y in 0..h {
                sum[indices[y]] += (s[y][x] == '1') as usize;
                if sum[indices[y]] > k {
                    sum = vec![0; h];
                    for i in 0..h {
                        sum[indices[i]] += (s[i][x] == '1') as usize
                    }
                    let max = *sum.iter().max().unwrap();
                    if max > k {
                        cnt = 1000000000;
                        break 'quit;
                    }
                    cnt += 1;
                    break;
                }
            }
        }

        ans = std::cmp::min(ans, cnt);
    }
    println!("{}", ans);
}