Buggy python submission (verdict: Time Limit Exceeded):
from collections import defaultdict
from itertools import product

H, W, K = map(int, input().split())
S = [input() for _ in range(H)]

Count = [sum(c == '1' for c in s) for s in S]
total = sum(Count)

while 0 in Count:
  i = Count.index(0)
  del S[i]
  del Count[i]
H = len(S)

if total <= K:
  answer = 0
else:
  answer = H * W
  for X in product([False, True], repeat=H-1):
    ans = sum(X)
    if ans > answer:
      continue
    DS = []
    for s, x in zip(S, [True] + list(X)):
      if x:
        DS.append([])
      DS[-1].append(s)
    DC = [[sum(s[j] == '1' for s in ds) for ds in DS] for j in range(W)]
    D = [0] * len(DS)
    for dc in DC:
      if any(d + c > K for d, c in zip(D, dc)):
        ans += 1
        if ans > answer:
          break
        D = [0] * len(DS)
      for k, c in enumerate(dc):
        D[k] += c
    answer = min(answer, ans)

print(answer)

Fixed submission:
from collections import defaultdict
from itertools import product

H, W, K = map(int, input().split())
S = [input() for _ in range(H)]

i = 0
while i < len(S):
  if '1' in S[i]:
    i += 1
  else:
    del S[i]
H = len(S)

C = [[int(s[i]) for s in S] for i in range(W)]

total = sum(sum(c) for c in C)

if total <= K:
  answer = 0
else:
  answer = H * W
  for X in product([False, True], repeat=H-1):
    ans = sum(X)
    if ans > answer:
      continue
    M = [[0]]
    for i, x in enumerate(X):
      if x:
        M.append([])
      M[-1].append(i+1)
    D = [0] * len(M)
    for c in C:
      for k, m in enumerate(M):
        D[k] += sum(c[i] for i in m)
      if any(d > K for d in D):
        ans += 1
        if ans > answer:
          break
        D = [sum(c[i] for i in m) for m in M]
        if any(d > K for d in D):
          ans = answer + 1
          break
    answer = min(answer, ans)

print(answer)

Buggy python submission (verdict: Time Limit Exceeded):
from collections import defaultdict
from itertools import product

H, W, K = map(int, input().split())
S = [input() for _ in range(H)]

Count = [sum(c == '1' for c in s) for s in S]
total = sum(Count)

while 0 in Count:
  i = Count.index(0)
  del S[i]
  del Count[i]
H = len(S)

if total <= K:
  answer = 0
else:
  answer = H * W
  for X in product([False, True], repeat=H-1):
    DS = defaultdict(list)
    i = 0
    for j, s in enumerate(S):
      if j > 0 and X[j-1]:
        i += 1
      DS[i].append(s)
    ans = sum(X)
    DC = defaultdict(int)
    for j in range(W):
      if any(DC[k] + sum(s[j] == '1' for s in s_list) > K for k, s_list in DS.items()):
        ans += 1
        DC = defaultdict(int)
      for k, s_list in DS.items():
        DC[k] += sum(s[j] == '1' for s in s_list)
    answer = min(answer, ans)

print(answer)

Fixed submission:
from collections import defaultdict
from itertools import product

H, W, K = map(int, input().split())
S = [input() for _ in range(H)]

i = 0
while i < len(S):
  if '1' in S[i]:
    i += 1
  else:
    del S[i]
H = len(S)

C = [[int(s[i]) for s in S] for i in range(W)]

total = sum(sum(c) for c in C)

if total <= K:
  answer = 0
else:
  answer = H * W
  for X in product([False, True], repeat=H-1):
    ans = sum(X)
    if ans > answer:
      continue
    M = [[0]]
    for i, x in enumerate(X):
      if x:
        M.append([])
      M[-1].append(i+1)
    D = [0] * len(M)
    for c in C:
      for k, m in enumerate(M):
        D[k] += sum(c[i] for i in m)
      if any(d > K for d in D):
        ans += 1
        if ans > answer:
          break
        D = [sum(c[i] for i in m) for m in M]
        if any(d > K for d in D):
          ans = answer + 1
          break
    answer = min(answer, ans)

print(answer)