Buggy python submission (verdict: Time Limit Exceeded):
h, w, k = map(int, input().split())
ar = [list(map(int, input())) for _ in range(h)]

ans = None
for i in range(2**(h - 1)):
    t = 0
    to_add = [0]
    for a in range(h - 1):
        if i & (2**a):
            t += 1
        to_add.append(t)
    n_h = t
    n_w = 0
    zz = [0 for _ in range(n_h + 1)]
    cnts = zz.copy()
    for x in range(w):
        is_over = False
        for y in range(h):
            z = to_add[y]
            cnts[z] += ar[y][x]
            if cnts[z] > k:
                is_over = True
                break
        if is_over:
            n_w += 1
            if ans is not None and ans <= n_h + n_w:
                break
            cnts = [0 for _ in range(n_h + 1)]
            cnts = zz.copy()
            for y in range(h):
                cnts[to_add[y]] += ar[y][x]
    if ans is None or ans > n_h + n_w:
        ans = n_h + n_w
print(ans)


Fixed submission:
h, w, k = map(int, input().split())
ar = [list(map(int, input())) for _ in range(h)]

ans = None
for i in range(2**(h - 1)):
    t = 0
    to_add = [0]
    for a in range(h - 1):
        if i & (2**a):
            t += 1
        to_add.append(t)
    n_h = t
    n_w = 0
    zz = [0 for _ in range(n_h + 1)]
    cnts = zz.copy()
    is_new = True
    is_fail = False
    for x in range(w):
        is_over = False
        for y in range(h):
            z = to_add[y]
            cnts[z] += ar[y][x]
            if cnts[z] > k:
                is_over = True
                break
        if is_over:
            n_w += 1
            if ans is not None and ans <= n_h + n_w:
                break
            cnts = zz.copy()
            for y in range(h):
                z = to_add[y]
                cnts[z] += ar[y][x]
                if cnts[z] > k:
                    is_fail = True
                    break
            if is_fail:
                break
    if not is_fail and (ans is None or ans > n_h + n_w):
        ans = n_h + n_w
print(ans)

Buggy python submission (verdict: Time Limit Exceeded):
h, w, k = map(int, input().split())
ar = [list(map(int, input())) for _ in range(h)]

ans = None
for i in range(2**(h - 1)):
    t = 0
    to_add = [0]
    for a in range(h - 1):
        if i & (2**a):
            t += 1
        to_add.append(t)
    n_h = t
    n_w = 0
    cnts = [0 for _ in range(n_h + 1)]
    for x in range(w):
        for y in range(h):
            cnts[to_add[y]] += ar[y][x]
        if max(cnts) > k:
            n_w += 1
            for z in range(n_h + 1):
                cnts[z] = 0
            for y in range(h):
                cnts[to_add[y]] += ar[y][x]
            if ans is not None and ans <= n_h + n_w:
                break
    if ans is None or ans > n_h + n_w:
        ans = n_h + n_w
print(ans)


Fixed submission:
h, w, k = map(int, input().split())
ar = [list(map(int, input())) for _ in range(h)]

ans = None
for i in range(2**(h - 1)):
    t = 0
    to_add = [0]
    for a in range(h - 1):
        if i & (2**a):
            t += 1
        to_add.append(t)
    n_h = t
    n_w = 0
    zz = [0 for _ in range(n_h + 1)]
    cnts = zz.copy()
    is_new = True
    is_fail = False
    for x in range(w):
        is_over = False
        for y in range(h):
            z = to_add[y]
            cnts[z] += ar[y][x]
            if cnts[z] > k:
                is_over = True
                break
        if is_over:
            n_w += 1
            if ans is not None and ans <= n_h + n_w:
                break
            cnts = zz.copy()
            for y in range(h):
                z = to_add[y]
                cnts[z] += ar[y][x]
                if cnts[z] > k:
                    is_fail = True
                    break
            if is_fail:
                break
    if not is_fail and (ans is None or ans > n_h + n_w):
        ans = n_h + n_w
print(ans)

Buggy python submission (verdict: Time Limit Exceeded):
h, w, k = map(int, input().split())
ar = [list(map(int, input())) for _ in range(h)]

ans = None
for i in range(2**(h - 1)):
    cut = [i & (2**a) != 0 for a in range(h - 1)]
    t = 0
    to_add = [0]
    for cut_ in cut:
        if cut_:
            t += 1
        to_add.append(t)
    n_h = t
    n_w = 0
    cnts = [0 for _ in range(n_h + 1)]
    for x in range(w):
        for y in range(h):
            cnts[to_add[y]] += ar[y][x]
        if max(cnts) > k:
            n_w += 1
            cnts = [0 for _ in range(n_h + 1)]
            for y in range(h):
                cnts[to_add[y]] += ar[y][x]
    if ans is None or ans > n_h + n_w:
        ans = n_h + n_w
print(ans)


Fixed submission:
h, w, k = map(int, input().split())
ar = [list(map(int, input())) for _ in range(h)]

ans = None
for i in range(2**(h - 1)):
    t = 0
    to_add = [0]
    for a in range(h - 1):
        if i & (2**a):
            t += 1
        to_add.append(t)
    n_h = t
    n_w = 0
    zz = [0 for _ in range(n_h + 1)]
    cnts = zz.copy()
    is_new = True
    is_fail = False
    for x in range(w):
        is_over = False
        for y in range(h):
            z = to_add[y]
            cnts[z] += ar[y][x]
            if cnts[z] > k:
                is_over = True
                break
        if is_over:
            n_w += 1
            if ans is not None and ans <= n_h + n_w:
                break
            cnts = zz.copy()
            for y in range(h):
                z = to_add[y]
                cnts[z] += ar[y][x]
                if cnts[z] > k:
                    is_fail = True
                    break
            if is_fail:
                break
    if not is_fail and (ans is None or ans > n_h + n_w):
        ans = n_h + n_w
print(ans)