using System;
using System.Collections.Generic;
using System.Linq;
class Program
{
static string InputPattern = "InputX";
static List<string> GetInputList()
{
var WillReturn = new List<string>();
if (InputPattern == "Input1") {
WillReturn.Add("3 5 4");
WillReturn.Add("11100");
WillReturn.Add("10001");
WillReturn.Add("00111");
//2
}
else if (InputPattern == "Input2") {
WillReturn.Add("3 5 8");
WillReturn.Add("11100");
WillReturn.Add("10001");
WillReturn.Add("00111");
//0
}
else if (InputPattern == "Input3") {
WillReturn.Add("4 10 4");
WillReturn.Add("1110010010");
WillReturn.Add("1000101110");
WillReturn.Add("0011101001");
WillReturn.Add("1101000111");
//3
}
else {
string wkStr;
while ((wkStr = Console.ReadLine()) != null) WillReturn.Add(wkStr);
}
return WillReturn;
}
static int UB_X;
static int UB_Y;
static void Main()
{
List<string> InputList = GetInputList();
int[] wkArr = InputList[0].Split(' ').Select(pX => int.Parse(pX)).ToArray();
int K = wkArr[2];
int[,] BanArr = CreateBanArr(InputList.Skip(1));
UB_X = BanArr.GetUpperBound(0);
UB_Y = BanArr.GetUpperBound(1);
List<JoutaiDef> DFSResultList = ExecDFS();
int Answer = int.MaxValue;
foreach (JoutaiDef EachDFSResult in DFSResultList) {
List<int> ClearIndList = EachDFSResult.ClearIndList;
int[] SumArr = new int[ClearIndList.Count + 1];
int AnswerKouho = ClearIndList.Count;
bool IsNG = false;
for (int LoopX = 0; LoopX <= UB_X; LoopX++) {
int YInd = 0;
int[] CurrSumArr = new int[ClearIndList.Count + 1];
for (int LoopY = 0; LoopY <= UB_Y; LoopY++) {
CurrSumArr[YInd] += BanArr[LoopX, LoopY];
if (ClearIndList.Contains(LoopY)) {
YInd++;
}
}
// 現在の列で、Kマスを超える場合は不可
if (CurrSumArr.Max() > K) {
IsNG = true;
break;
}
// 現在の列の分を足すとKを超える場合は、分割が必要
bool NeedDiv = false;
for (int I = 0; I <= SumArr.GetUpperBound(0); I++) {
SumArr[I] += CurrSumArr[I];
if (SumArr[I] > K) {
NeedDiv = true;
break;
}
}
if (NeedDiv) {
AnswerKouho++;
SumArr = (int[])CurrSumArr.Clone();
}
}
if (IsNG == false) {
Answer = Math.Min(Answer, AnswerKouho);
}
}
Console.WriteLine(Answer);
}
struct JoutaiDef
{
internal List<int> ClearIndList;
internal int CurrInd;
}
// DFSで、集計をクリアする行の組み合わせを全探索する
static List<JoutaiDef> ExecDFS()
{
List<JoutaiDef> WillRetuen = new List<JoutaiDef>();
var Stk = new Stack<JoutaiDef>();
JoutaiDef WillPush;
WillPush.ClearIndList = new List<int>();
WillPush.CurrInd = 0;
Stk.Push(WillPush);
while (Stk.Count > 0) {
JoutaiDef Popped = Stk.Pop();
WillRetuen.Add(Popped);
if (Popped.CurrInd > UB_Y) {
continue;
}
for (int I = Popped.CurrInd; I <= UB_Y - 1; I++) {
WillPush.CurrInd = I + 1;
WillPush.ClearIndList = new List<int>(Popped.ClearIndList);
WillPush.ClearIndList.Add(I);
Stk.Push(WillPush);
}
}
return WillRetuen;
}
////////////////////////////////////////////////////////////////
// IEnumerable<string>をintの2次元配列に設定する
////////////////////////////////////////////////////////////////
static int[,] CreateBanArr(IEnumerable<string> pStrEnum)
{
var StrList = pStrEnum.ToList();
if (StrList.Count == 0) {
return new int[0, 0];
}
int UB_X = StrList[0].Length - 1;
int UB_Y = StrList.Count - 1;
int[,] WillReturn = new int[UB_X + 1, UB_Y + 1];
for (int Y = 0; Y <= UB_Y; Y++) {
for (int X = 0; X <= UB_X; X++) {
WillReturn[X, Y] = StrList[Y][X] - '0';
}
}
return WillReturn;
}
}