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.Linq;
using System.IO;
using System.Text;
using System.Threading.Tasks;
using System.Globalization;
namespace Atcoder
{
    class Program
    {
        static void Main(string[] args)
        {
            StreamScanner scan = new StreamScanner(System.Console.OpenStandardInput());
            int h = scan.Integer();
            int w = scan.Integer();
            int k = scan.Integer();
            int[,] S = new int[h, w];
            for (int i = 0; i < h; i++)
            {
                for (int j = 0; j < w; j++)
                {
                    S[i, j] = scan.Char()-'0';
                }
            }

            int max = 1;
            for (int i = 0; i < h - 1; i++)
            {
                max *= 2;
            }
            int[,] ruiseki = new int[h, w];
            for (int i = 0; i < h; i++)
            {
                for (int j = 0; j < w; j++)
                {
                    if (j != 0) { ruiseki[i, j] = ruiseki[i, j - 1] + S[i, j]; }
                    else { ruiseki[i, j] = S[i, j]; }
                }
            }
            int min = int.MaxValue;
            for (int i = 0; i < max; i++)
            {
                int setudancount = 0;
                List<int[]> ganbaru = new List<int[]>();
                string kon = Convert.ToString(i, 2);
                for (int sd = 0; sd < h-1-kon.Length; sd++)
                {
                    kon = "0" + kon;
                }
                int[] zantei = yousogotoplus(ruiseki, 0);
                int nowindex = 1;
                for (int j = 0; j < kon.Length; j++)
                {
                    if (nowindex >= w) { break; }
                    if (kon[kon.Length-1-j] == '0') { zantei = yousogotoplus(zantei, ruiseki, nowindex); }
                    if (kon[kon.Length-1-j] == '1')
                    {
                        ganbaru.Add(zantei);
                        setudancount++;
                        zantei = yousogotoplus(ruiseki, nowindex);
                    }
                    nowindex++;
                }
                ganbaru.Add(zantei);
                int nowk = k;
                int[] nowks = new int[ganbaru.Count];
                for (int dd = 0; dd < ganbaru.Count; dd++)
                {
                    nowks[dd] = k;
                }
                for (int j = 0; j < w; j++)
                {
                    for (int l = 0; l < ganbaru.Count; l++)
                    {
                        if (ganbaru[l][j] > nowks[l]) {
                            setudancount++;
                            if (j >= 1)
                            {
                                nowk += ganbaru[l][j - 1];
                                for (int dd = 0; dd < ganbaru.Count; dd++)
                                {
                                    nowks[dd] += ganbaru[dd][j - 1];
                                }
                                if (ganbaru[l][j] > nowks[l]) { min = 1000 * 100; }
                            }
                            else { min = 1000 * 100; }
                          }
                    }
                }
                if (min > setudancount) { min = setudancount; }
            }
            Console.WriteLine(min);
        }
       
        static int[]yousogotoplus(int[]a,int[]b)
        {
            int[] c = new int[a.Length];
            for (int i = 0; i < a.Length; i++)
            {
                c[i] = a[i] + b[i];
            }
            return c;
        }
        static int[] yousogotoplus(int[] a, int[,] b, int l)
        {
            int[] c = new int[a.Length];
            for (int i = 0; i < a.Length; i++)
            {
                c[i] = a[i] + b[l,i];
            }
            return c;
        }
        
        static int[] yousogotoplus(int[,] a,int l)
        {
            int[] c = new int[a.GetLength(1)];
            for (int i = 0; i < a.GetLength(1); i++)
            {
                c[i] = a[l, i];
            }
            return c;
        }
        static long solve(int mae,int ushiro,long[,]dp,int[]a,bool taro)
        {
            if (dp[mae, ushiro] > long.MinValue)
            {
                return dp[mae, ushiro];
            }
            if (ushiro - mae == 1)
            {
                if (taro) { dp[mae, ushiro] = a[mae]; return dp[mae, ushiro]; }
                else { dp[mae, ushiro] = -a[mae]; return dp[mae, ushiro]; }
            }

            if(taro)
            {
                long max = Math.Max(a[mae] + solve(mae + 1, ushiro, dp, a, !taro),
                    a[ushiro-1] + solve(mae, ushiro - 1, dp, a, !taro));
                dp[mae, ushiro] = max;
                return dp[mae, ushiro];
                
            }
            else
            {
                long min= Math.Min(-a[mae] + solve(mae + 1, ushiro, dp, a, !taro),
                    -a[ushiro-1] + solve(mae, ushiro - 1, dp, a, !taro));
                dp[mae, ushiro] = min;
                return dp[mae, ushiro];
            }
            
        }

        static int nibutan(int med,int[]a)//med以上が何個あるか
        {
            int mae = 0;
            int ato = a.Length;
            while (true)
            {
                if (ato - mae <= 1) {
                    return mae; }
                int mid = (mae + ato) / 2;
                if (a[mid] > med) { ato = mid; }
                if (a[mid] <= med) { mae = mid; }
            }
        }
        static int binary_search(int key, int[] moto)
        {
            int ng = -1;
            int ok = moto.Length;
            while (Math.Abs(ok - ng) > 1)
            {
                int mid = (ok + ng) / 2;
                if (isOK(mid, key, moto)) { ok = mid; }
                else { ng = mid; }
            }
            return ok;
        }
        static bool isOK(int index, int key,int[]moto)
        {
            return moto[index] >= key;
        }

        public static int h;
        public static int w;
        public static int BFS(int height, int width, char[,] map)
        {
            int[] dx = new int[] { -1, 0, 0, 1 };
            int[] dy = new int[] { 0, 1, -1, 0 };
            bool[,] visited = new bool[h, w];
            int[,] len = new int[h, w];
            var queue = new Queue<Tuple<int, int>>();
            queue.Enqueue(new Tuple<int, int>(height, width));
            int max = 0;
            visited[height, width] = true;

            while (queue.Count > 0)
            {
                var now = queue.Dequeue();
                for (int i = 0; i < 4; i++)
                {
                    int ny = now.Item1 + dy[i];
                    int nx = now.Item2 + dx[i];
                    if (ny >= h || ny < 0 || nx >= w || nx < 0)
                        continue;
                    if (visited[ny, nx] || map[ny, nx] == '#')
                        continue;
                    visited[ny, nx] = true;
                    len[ny, nx] = len[now.Item1, now.Item2] + 1;
                    max = Math.Max(max, len[ny, nx]);
                    queue.Enqueue(new Tuple<int, int>(ny, nx));
                }
            }
            return max;
        }
    }
    public class StreamScanner
    {
        public StreamScanner(Stream stream) { str = stream; }
        private readonly Stream str;
        private readonly byte[] buf = new byte[1024];
        private int len, ptr;
        public bool isEof = false;
        public bool IsEndOfStream { get { return isEof; } }
        private byte read()
        {
            if (isEof) throw new EndOfStreamException();
            if (ptr >= len)
            {
                ptr = 0;
                if ((len = str.Read(buf, 0, 1024)) <= 0)
                {
                    isEof = true;
                    return 0;
                }
            }
            return buf[ptr++];
        }
        public char Char()
        {
            byte b = 0;
            do b = read();
            while (b < 33 || 126 < b);
            return (char)b;
        }
        public char[] Char(int n)
        {
            char[] kore = new char[n];
            for (int i = 0; i < n; i++)
            {
                kore[i] = Char();
            }
            return kore;
        }
        public string Scan()
        {
            var sb = new StringBuilder();
            for (var b = Char(); b >= 33 && b <= 126; b = (char)read())
                sb.Append(b);
            return sb.ToString();
        }
        public long Long()
        {
            long ret = 0; byte b = 0; var ng = false;
            do b = read();
            while (b != '-' && (b < '0' || '9' < b));
            if (b == '-') { ng = true; b = read(); }
            for (; true; b = read())
            {
                if (b < '0' || '9' < b)
                    return ng ? -ret : ret;
                else ret = ret * 10 + b - '0';
            }
        }
        public long[]Long(int n)
        {
            long[] kore = new long[n];
            for (int i = 0; i < n; i++)
            {
                kore[i] = Long();
            }
            return kore;
        }
        public int Integer() { return (int)Long(); }
        public int[]Integer(int n)
        {
            int[] kore = new int[n];
            for (int i = 0; i < n; i++)
            {
                kore[i] = (int)Long();
            }
            return kore;
        }
        public double Double() { return double.Parse(Scan(), CultureInfo.InvariantCulture); }
    }
    public class UnionFind
    {
        public int[] data;

        public UnionFind(int size)
        {
            data = new int[size];
            for (int i = 0; i < size; i++) data[i] = -1;
        }

        public bool Unite(int x, int y)
        {
            x = Root(x);
            y = Root(y);

            if (x != y)
            {
                if (data[y] < data[x])
                {
                    var tmp = y;
                    y = x;
                    x = tmp;
                }
                data[x] += data[y];
                data[y] = x;
            }
            return x != y;
        }

        public bool IsSameGroup(int x, int y)
        {
            return Root(x) == Root(y);
        }

        public int Root(int x)
        {
            return data[x] < 0 ? x : data[x] = Root(data[x]);
        }
    }
}