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: Wrong Answer)

# n = gets.to_i
# s = gets.chomp
class Array
  def cumsum
    sum = 0
    self.map{|x| sum += x}
  end
  
  def cut(k)
      v = 0
      res = 0
      self.each_with_index do |c,i|
          if c > k
              return res = 10000
          end
          v += c
          if v > k
              res += 1
              v = c
          end
      end
      return res
   end
end

h,w,k = gets.split.map(&:to_i)

a = Array.new(h){ [] }
cs = Array.new(h){ [] }
h.times do |i|
    a[i] = gets.chomp.chars.map(&:to_i)
    s = a.inject(:+)
    cs[i] = a[i].cumsum 
end

ccs = Array.new(h){ Array.new(w) }
ccs[0] = cs[0]
(h-1).times do |i|
    w.times do |j|
        ccs[i+1] = ccs[i] + cs[i+1]
    end
end

csa = Array.new(h){ Array.new(w) }
csa[0] = a[0]
(h-1).times do |i|
    w.times do |j|
        csa[i+1][j] = csa[i][j] + a[i+1][j]
    end
end

# p csa
# p csa[2].cut(4)
# p csa[0].cut(2)
# p 1 << (h-1)
# p cs[0,h]
# (1<<(h-1)).times do |k|
ans = 10000
[0, 1].repeated_permutation(h-1) do |bits|
    m = 0
    t = []
    bits.each_with_index do |b,i|
        if b == 1
            t.push(i)
            m += 1
        end
    end
    t.push(h-1)
    
    t.each_with_index do |c,i|
        
        if i == 0
            m += csa[c].cut(k)
            # if t == [2,3]
            #     p ["a",csa[c].cut(k),c,k]
            # end
        else
            m += csa[c].cut(k) - csa[t[i-1]].cut(k)
        end
    end
    
    if t == [h-1]
        m = csa[h-1].cut(k)
    end
    
    # p [t,m] if ans > m
    ans = m if ans > m
    # p [bits,t,m, m-(t.size-1)]
end

[0, 1].repeated_permutation(5) do |bits|
    # p ["bits", bits]
end

# p [] == []
# p a[0].cut(4)
# p a[1].cut(4)
# p a[2].cut(4)
# p csa[2].cut(4)
p ans