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: Accepted)

using System;
using System.Collections.Generic;
using System.Windows;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Atcoder
{
    public class Program
    {
        static void Main(string[] args)
        {
            var r = ReadSplit();
            var h = r[0].ToInt();
            var w = r[1].ToInt();
            var k = r[2].ToInt();
            var ls = new List<string>();
            for (int i = 0; i < h; i++)
            {
                var s = Read();
                ls.Add(s);
            }
            var group = new int[10][];
            for (int i = 0; i < 10; i++) group[i] = new int[1005];
            var INF = 100100101;
            var ans = INF;
            for (int div = 0; div < 1 << (h - 1); div++)
            {
                var g = 0;
                var gId = new List<int>();
                for (int i = 0; i < h; i++)
                {
                    gId.Add(g);
                    if ((div >> i & 1) == 1)
                    {
                        g++;
                    }
                }
                g++;
                for (int i = 0; i < g; i++)
                {
                    for (int j = 0; j < w; j++)
                    {
                        group[i][j] = 0;
                    }
                }
                for (int i = 0; i < h; i++)
                {
                    for (int j = 0; j < w; j++)
                    {
                        group[gId[i]][j] += ls[i][j] == '1' ? 1 : 0;
                    }
                }

                var ok = true;
                for (int i = 0; i < g; i++)
                {
                    for (int j = 0; j < w; j++)
                    {
                        if (group[i][j] > k) ok = false;
                    }
                }
                if (!ok) continue;

                int num = g - 1;
                var now = new int[g];

                var add = new Predicate<int>(x =>
                {
                    for (int i = 0; i < g; i++)
                    {
                        now[i] += group[i][x];
                    }
                    for (int i = 0; i < g; i++)
                    {
                        if (now[i] > k) return false;
                    }
                    return true;
                });

                for (int j = 0; j < w; j++)
                {
                    if (!add(j))
                    {
                        num++;
                        now = new int[g];
                        add(j);
                    }
                }
                ans = Math.Min(ans, num);
            }
            WriteRead(ans);
        }

        public static string Read()
        {
            return Console.ReadLine();
        }

        public static string[] ReadSplit()
        {
            return Console.ReadLine().Split(' ');
        }

        public static void WriteRead(object str)
        {
            Console.WriteLine(str);
            Read();
        }

        public static void Write(object str)
        {
            Console.WriteLine(str);
        }
    }
}


public static class Extentions
{
    public static int ToInt(this object obj)
    {
        return int.Parse(obj.ToString());
    }

    public static decimal ToDecimal(this object obj)
    {
        return decimal.Parse(obj.ToString());
    }

    public static double ToDouble(this object obj)
    {
        return double.Parse(obj.ToString());
    }

    public static T2 GetValueZero<T1, T2>(this Dictionary<T1, T2> d, T1 key) where T2 : struct
    {
        T2 value;
        if (d.TryGetValue(key, out value))
        {
            return value;
        }
        return new T2();
    }
}