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 Scala (2.11.7) (judge verdict: Accepted)

import java.util.Scanner

import scala.collection.mutable.ArrayBuffer

object Main {

  case class Range(begin: Int, end: Int)

  def read() = {
    val sc = new Scanner(System.in)
    val h, w, k = sc.nextInt()
    val cho = IndexedSeq.fill(h)(sc.next().map(c => if (c == '0') 0 else 1))
    (h, w, k, cho)
  }

  def solve(h: Int, w: Int, k: Int, cho: IndexedSeq[IndexedSeq[Int]]): Int = {
    // h=1がこわい
    // 2次元累積和
    def addVec(a: IndexedSeq[Int], b: IndexedSeq[Int]): IndexedSeq[Int] = {
      (a, b).zipped.map(_ + _)
    }

    val chocum = cho.map(_.scanLeft(0)(_ + _)).scanLeft(IndexedSeq.fill(w + 1)(0))(addVec)

    //左上含める右下含めない
    def sumwch(x1: Int, y1: Int, x2: Int, y2: Int): Int =
      chocum(y2)(x2) - chocum(y1)(x2) - chocum(y2)(x1) + chocum(y1)(x1)

    // 横切りを指定して最小縦切り数を求める
    def calcBest(cutsHol: IndexedSeq[Boolean]): Int = {
      val parHol = ArrayBuffer.empty[Range]
      var tmp = 0
      for (i <- cutsHol.indices) {
        if (cutsHol(i)) {
          parHol.append(Range(tmp, i + 1))
          tmp = i + 1
        }
      }
      parHol.append(Range(tmp, h))

      // x-1とxの間を切る
      var tmpx = 0
      var cnt = 0//カット回数
      for(x <- 1 until w+1){
        // [tmpx, x), range
        if(parHol.forall(rng => sumwch(tmpx, rng.begin, x, rng.end)<= k)){
          // カットしない
        }else{
          cnt += 1
          if(tmpx == x-1){
            val inf = 1000000007
            // 無理
            return inf
          }
          tmpx = x-1
        }
      }
        cnt
    }

    val result = for (pattern <- 0 until 1 << h - 1) yield {
      val patternList =
        (0 until h - 1).map(i => (pattern >> i) & 1).map(_ == 1)
      // patternList(y): yとy+1の間で切るかどうか
      val numHol = patternList.count(p => p)
      val numVer = calcBest(patternList)
      numHol + numVer
    }
    result.min
  }

  def main(args: Array[String]): Unit = {
    val (h, w, k, cho) = read()
    println(solve(h, w, k, cho))
  }
}