Buggy python submission (verdict: Runtime Error):
import numpy as np
import itertools

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

ans = 2000
for t in itertools.product([0, 1], repeat=H - 1):
    cut = sum(t)
    lst = np.zeros((cut + 1, W), dtype=np.int)
    lst[0] += S[0]
    c = 0
    for h in range(H - 1):
        if t[h]:
            c += 1
        lst[c] += S[h + 1]
    L = len(lst)
    sum_lst = np.zeros(L)
    for w in range(W):
        lst_w = np.max(lst, axis=0)
        if lst_w[w] > K:
            break

        lst_s = sum_lst + lst_w
        if lst_s.max() > K:
            cut += 1
            sum_lst = lst_w
        else:
            sum_lst = lst_s
    else:
        ans = min(ans, cut)
print(ans)


Fixed submission:
import itertools
 
H, W, K = map(int, input().split())
S = []
for _ in range(H):
    S.append([int(s) for s in input()])
 
ans = 2000
for t in itertools.product([0, 1], repeat=H - 1):
    cnt = t.count(1)
    lst = [[s for s in S[0]]]
    for h in range(H - 1):
        if t[h]:
            lst.append(S[h + 1])
        else:
            lst[-1] = [lst[-1][i] + S[h + 1][i] for i in range(W)]
    L = len(lst)
    sum_lst = [0] * L
    for w in range(W):
        if max(lst[i][w] for i in range(L)) > K:
            break
        tmp = [sum_lst[i] + lst[i][w] for i in range(L)]
        if max(tmp) > K:
            cnt += 1
            sum_lst = [lst[i][w] for i in range(L)]
        else:
            sum_lst = tmp
    else:
        ans = min(ans, cnt)
print(ans)

Buggy python submission (verdict: Time Limit Exceeded):
import numpy as np
import itertools

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

ans = 2000
for t in itertools.product([0, 1], repeat=H - 1):
    cut = t.count(1)
    lst = np.zeros((cut + 1, W), dtype=np.int)
    lst[0] += S[0]
    c = 0
    for h in range(H - 1):
        if t[h]:
            c += 1
        lst[c] += S[h + 1]
    L = len(lst)
    sum_lst = np.zeros(L)
    for w in range(W):
        lst_w = lst[:, w]
        if lst_w.max() > K:
            break

        lst_s = sum_lst + lst_w
        if lst_s.max() > K:
            cut += 1
            sum_lst = lst_w
        else:
            sum_lst = lst_s
    else:
        ans = min(ans, cut)
print(ans)

Fixed submission:
import itertools
 
H, W, K = map(int, input().split())
S = []
for _ in range(H):
    S.append([int(s) for s in input()])
 
ans = 2000
for t in itertools.product([0, 1], repeat=H - 1):
    cnt = t.count(1)
    lst = [[s for s in S[0]]]
    for h in range(H - 1):
        if t[h]:
            lst.append(S[h + 1])
        else:
            lst[-1] = [lst[-1][i] + S[h + 1][i] for i in range(W)]
    L = len(lst)
    sum_lst = [0] * L
    for w in range(W):
        if max(lst[i][w] for i in range(L)) > K:
            break
        tmp = [sum_lst[i] + lst[i][w] for i in range(L)]
        if max(tmp) > K:
            cnt += 1
            sum_lst = [lst[i][w] for i in range(L)]
        else:
            sum_lst = tmp
    else:
        ans = min(ans, cnt)
print(ans)

Buggy python submission (verdict: Runtime Error):
import itertools
 
H, W, K = map(int, input().split())
S = []
for _ in range(H):
    S.append(list(map(int, input().split())))
 
ans = 2000
for t in itertools.product([0, 1], repeat=H - 1):
    cnt = t.count(1)
    lst = [[s for s in S[0]]]
    for h in range(H - 1):
        if t[h]:
            lst.append(S[h + 1])
        else:
            lst[-1] = [lst[-1][i] + S[h + 1][i] for i in range(W)]
    L = len(lst)
    sum_lst = [0] * L
    for w in range(W):
        if max(lst[i][w] for i in range(L)) > K:
            break
        tmp = [sum_lst[i] + lst[i][w] for i in range(L)]
        if max(tmp) > K:
            cnt += 1
            sum_lst = [lst[i][w] for i in range(L)]
        else:
            sum_lst = tmp
    else:
        ans = min(ans, cnt)
print(ans)

Fixed submission:
import itertools
 
H, W, K = map(int, input().split())
S = []
for _ in range(H):
    S.append([int(s) for s in input()])
 
ans = 2000
for t in itertools.product([0, 1], repeat=H - 1):
    cnt = t.count(1)
    lst = [[s for s in S[0]]]
    for h in range(H - 1):
        if t[h]:
            lst.append(S[h + 1])
        else:
            lst[-1] = [lst[-1][i] + S[h + 1][i] for i in range(W)]
    L = len(lst)
    sum_lst = [0] * L
    for w in range(W):
        if max(lst[i][w] for i in range(L)) > K:
            break
        tmp = [sum_lst[i] + lst[i][w] for i in range(L)]
        if max(tmp) > K:
            cnt += 1
            sum_lst = [lst[i][w] for i in range(L)]
        else:
            sum_lst = tmp
    else:
        ans = min(ans, cnt)
print(ans)