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 C# (Mono 4.6.2.0) (judge verdict: Wrong Answer)

using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using static System.Math;
using System.Text.RegularExpressions;

namespace CsharpSpiral {
    partial class Program {
        static void Main(string[] args) {
            var sw = new StreamWriter(Console.OpenStandardOutput()) { AutoFlush = false };
            Console.SetOut(sw);
            new Program().Solve(new ConsoleInput(Console.In, ' '));
            Console.Out.Flush();
        }
        public void Solve(ConsoleInput cin) {
            var H = cin.ReadInt;
            var W = cin.ReadInt;
            var K = cin.ReadInt;
            var S = new int[H, W];
            var T = new int[H, W + 1];
            for(int i = 0; i < H; i++) {
                var row = cin.ReadLine;
                for(int j = 0; j < W; j++) {
                    S[i, j] = row[j] == '1' ? 1 : 0;
                    T[i, j + 1] = T[i, j] + S[i, j];
                    //Console.Write(T[i, j + 1]+",");
                }
                //Console.WriteLine("");
            }
            var U = new int[H + 1, W + 1];
            for(int i = 0; i < H; i++) {
                for(int j = 0; j <= W; j++) {
                    U[i + 1, j] = U[i, j] + T[i, j];
                    //Console.Write(U[i + 1, j] + ",");
                }
                //Console.WriteLine("");
            }
            var flags = Enumerable.Range(0, (1 << (H - 1)));
            Console.WriteLine(flags.Min(i => CutAndSearch(i, U, K)));
        }
        public int CutAndSearch(int flag, int[,] T, int K) {
            //Console.WriteLine($"flag:{flag}");
            var lastRow = 0;
            var rowCut = 0;
            Dictionary<int, int> Target2Last = new Dictionary<int, int>();
            for(int i = 1; i < T.GetLength(0)-1; i++) {
                if((flag & (1 << (i - 1))) != 0) {
                    Target2Last.Add(i, lastRow);
                    lastRow = i;
                    rowCut++;
                }
            }
            Target2Last.Add(T.GetLength(0) - 1, lastRow);
            var res= Search(T, K, Target2Last) + rowCut;
            //Console.WriteLine(res);
            return res;
        }

        public int Search(int[,] T, int K, Dictionary<int,int> L) {
            //Console.WriteLine(L.Count);
            foreach(var p in L) {
                //Console.WriteLine($"{p.Key}->{p.Value}");
            }
            var H = T.GetLength(0) - 1;
            var E = L.Keys;
            var W = T.GetLength(1);
            var count = 0;
            var lastCut = 0;
            for(int j =1; j < W; j++) {
                var K1 = E.Max(i => (T[i, j] - T[i, lastCut]) - (T[L[i], j] - T[L[i], lastCut]));
                //Console.Write(K1+",");
                if(K < K1) {
                    count++;
                    lastCut = j - 1;
                }
            }
            //Console.WriteLine("");
            return count;
        }
    }

    static class ExtendClass {
        public static string Join<T>(this T[] X, string separator = " ") => string.Join(separator, X);
        public static S[] Clone<S>(this S[] src) where S : struct {
            var res = new S[src.Length];
            Array.Copy(src, res, src.Length);
            return res;
        }
    }

    class ConsoleInput {
        private readonly TextReader _stream;
        private readonly char _separator = ' ';
        private readonly Queue<string> inputStream;
        public ConsoleInput(TextReader stream, char separator = ' ') {
            this._separator = separator;
            this._stream = stream;
            inputStream = new Queue<string>();
        }
        public string Read {
            get {
                if(inputStream.Count != 0) return inputStream.Dequeue();
                string[] tmp = _stream.ReadLine().Split(_separator);
                for(int i = 0; i < tmp.Length; ++i)
                    inputStream.Enqueue(tmp[i]);
                return inputStream.Dequeue();
            }
        }
        public string ReadLine { get { return _stream.ReadLine(); } }
        public int ReadInt { get { return int.Parse(Read); } }
        public long ReadLong { get { return long.Parse(Read); } }
        public double ReadDouble { get { return double.Parse(Read); } }
        public string[] ReadStrArray(long N) {
            var ret = new string[N];
            for(long i = 0; i < N; ++i)
                ret[i] = Read;
            return ret;
        }
        public int[] ReadIntArray(long N) { var ret = new int[N]; for(long i = 0; i < N; ++i) ret[i] = ReadInt; return ret; }
        public long[] ReadLongArray(long N) { var ret = new long[N]; for(long i = 0; i < N; ++i) ret[i] = ReadLong; return ret; }
        public double[] ReadDoubleArray(long N) { var ret = new double[N]; for(long i = 0; i < N; ++i) ret[i] = ReadDouble; return ret; }
    }
}