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 Go (1.6) (judge verdict: Accepted)

package main

import (
	"bufio"
	"fmt"
	"math"
	"os"
	"strconv"
)

func exec(stdin *Stdin, stdout *Stdout) {
	h := stdin.ReadInt()
	w := stdin.ReadInt()
	k := stdin.ReadInt()
	s := make([][]int, h)
	for i := 0; i < h; i++ {
		line := stdin.Read()
		s[i] = make([]int, w)
		for j := 0; j < w; j++ {
			if line[j] == '0' {
				s[i][j] = 0
			} else {
				s[i][j] = 1
			}
		}
	}

	ans := h * w
	for _, crack := range Combinations(h-1, -1) {
		crack = append(crack, -1)

		x := 0
		cur := make([]int, len(crack)+1)
		for i := 0; i < h; i++ {
			cur[x] += s[i][0]
			if crack[x] == i {
				x++
			}
		}

		ng := false
		count := 0
		for j := 1; j < w; j++ {
			nxt := make([]int, len(crack)+1)
			for i := 0; i < len(cur); i++ {
				if k < cur[i] {
					ng = true
					break
				}
				nxt[i] = cur[i]
			}

			if ng {
				break
			}

			x = 0
			cracking := false
			for i := 0; i < h; i++ {
				nxt[x] += s[i][j]
				if k < nxt[x] {
					cracking = true
				}
				if crack[x] == i {
					x++
				}
			}

			if cracking {
				count++
				for i := 0; i < len(cur); i++ {
					nxt[i] -= cur[i]
				}
			}
			cur = nxt
		}

		if !ng {
			ans = Min(ans, count+len(crack)-1)
		}
	}

	stdout.Println(ans)
}

func Combinations(n int, r int) [][]int {
	ans := [][]int{}
	for x := uint(0); x < (uint(1) << uint(n)); x++ {
		idxs := []int{}
		for i := uint(0); i < uint(n); i++ {
			if x&(uint(1)<<i) != 0 {
				idxs = append(idxs, int(i))
			}
		}
		if r < 0 || len(idxs) == r {
			ans = append(ans, idxs)
		}
	}
	return ans
}

func Min(a int, b ...int) int {
	for _, v := range b {
		if v < a {
			a = v
		}
	}
	return a
}

func main() {
	stdout := NewStdout()
	defer stdout.Flush()
	exec(NewStdin(bufio.ScanWords), stdout)
}

type Stdin struct {
	stdin *bufio.Scanner
}

func NewStdin(split bufio.SplitFunc) *Stdin {
	s := Stdin{bufio.NewScanner(os.Stdin)}
	s.stdin.Split(split)
	s.stdin.Buffer(make([]byte, bufio.MaxScanTokenSize), int(math.MaxInt32))
	return &s
}

func (s *Stdin) Read() string {
	s.stdin.Scan()
	return s.stdin.Text()
}

func (s *Stdin) ReadInt() int {
	n, _ := strconv.Atoi(s.Read())
	return n
}

func (s *Stdin) ReadFloat64() float64 {
	n, _ := strconv.ParseFloat(s.Read(), 64)
	return n
}

type Stdout struct {
	stdout *bufio.Writer
}

func NewStdout() *Stdout {
	return &Stdout{bufio.NewWriter(os.Stdout)}
}

func (s *Stdout) Flush() {
	s.stdout.Flush()
}

func (s *Stdout) Println(a ...interface{}) {
	fmt.Fprintln(s.stdout, a...)
}