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-----
3 5 4
11100
10001
00111

-----Sample Output-----
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.

Difficulty: MEDIUM

Solutions:
import sys
read = sys.stdin.read
readline = sys.stdin.readline
readlines = sys.stdin.readlines

def main():
	(H, W, K) = list(map(int, readline().split()))
	S = [list(map(int, readline().strip())) for j in range(H)]
	white = 0
	for line in S:
		white += sum(line)
	if white <= K:
		print(0)
		return
	ans = 10 ** 5
	for pattern in range(2 ** (H - 1)):
		impossible = False
		x = 0
		ly = bin(pattern).count('1')
		y = [[0] * W] * (ly + 1)
		line = 0
		for i in range(H):
			y[line] = [y[line][j] + S[i][j] for j in range(W)]
			if pattern >> i & 1:
				line += 1
		count = [0] * (ly + 1)
		for j in range(W):
			for i in range(line + 1):
				if y[i][j] > K:
					impossible = True
					break
				count[i] += y[i][j]
				if count[i] > K:
					x += 1
					for i in range(line + 1):
						count[i] = y[i][j]
					break
			if x + ly > ans or impossible:
				impossible = True
				break
		if impossible:
			x = 10 ** 6
		ans = min(ans, x + ly)
	print(ans)

def __starting_point():
	main()
__starting_point()