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 Ruby (2.3.3) (judge verdict: Accepted)

#require 'prime'
MOD=10**9+7;  cnt=0; sum=0; prev=nil; can=true; h=Hash.new(0)
def gs() gets.chomp end
def gi() gets.chomp.to_i end
def gsmi() gets.chomp.split.map(&:to_i) end

def ar2(s1,s2,de=nil) Array.new(s1){Array.new(s2,de)} end
def ar3(s1,s2,s3,de=nil) Array.new(s1){Array.new(s2){Array.new(s3,de)}} end

def desc(ar) ar.sort!{|x,y| y<=>x} end
def min(a,b) a<=b ? a : b end
def max(a,b) a>=b ? a : b end
def sum(ar) ar.inject(:+) end
def ceil(a,b) (a.to_f/b).ceil end

def C(a,b) b==0||a==b ? 1 : (b=a-b if a/2<b;(a-b+1..a).inject(:*)/(1..b).inject(:*)) end
def rui(ar) s=[0]; ar.size.times{|i| s<<s[-1]+(1 if ar[i]=='1').to_i}; s end ###
def rev_rui(ar) s=[0]; ar.size.times.reverse_each{|i| s<<s[-1]+ar[i]}; s end

def puts_yesno(b) puts(b ? 'Yes' : 'No') end
def putsend(s) puts s; exit end
def pu(h) begin; require './../nemi_lib/debug.rb'; puts_val(h); rescue LoadError; end; end
########### ( ˘ω˘ )ｽﾔｧ… ###########
h,w,k=gsmi
s=h.times.map{gs.chars}

# 各列の白累積和
rui=[]
st=s.transpose
w.times do |i|
  tar=st[i]
  ar=rui(tar)
  rui<<ar
end

# 横線引くパターン全探索
pat=2**(h-1)
min=1000+10
pat.times do |i|
  idxs=[]
  h.times do |j|
    idxs<<j+1 if i[j]==1
  end
  #pu idxs:idxs.to_s
  cnt=idxs.size
  w_c_ar=Array.new(cnt+1,0)
  idxs<<h
  w.times do |j|
    cnt_ar=[]
    prev=0
    idxs.each do |kk|
      #pu kk:kk,prev:prev
      c = rui[j][kk]-rui[j][prev]
      cnt_ar << c
      prev=kk
    end

    # １列でkを超える場合はbreak
    if cnt_ar.select{|x|x>k}.any?
      cnt=1000+10
      break
    end

    tmp_ar = w_c_ar.zip(cnt_ar).map{|a,b|a+b}
    #pu tmp_ar:tmp_ar.to_s
    # この列を加えてkを超えたら左に縦線を引く
    if tmp_ar.select{|x|x>k}.any?
      cnt+=1
      w_c_ar=cnt_ar
    else
      w_c_ar=tmp_ar
    end
  end
  #pu cnt:cnt
  min=cnt if min>cnt
end

puts min