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

h, w, k = map(int, input().split())
s = np.array([list(map(int, input())) for _ in range(h)], dtype=np.int16)


def hsplit(pat):
    prev = 2
    ret = np.zeros((h + 1, w), dtype=np.int16)
    white = np.zeros(w, dtype=np.int16)
    cnt = 0
    for i, e in enumerate(pat):
        if e == prev:
            white += s[i]
        else:
            ret[cnt] = white.copy()
            white = s[i].copy()
            cnt += 1

        prev = e

    ret[cnt] = white.copy()
    cnt += 1

    return ret[1:cnt]


def greedy(arr):
    acc = np.zeros_like(arr, dtype=np.int16)
    ww = arr.shape[1]
    cnt = 0
    for i in range(ww):
        if i == 0:
            acc[:, i] = arr[:, i]
        else:
            acc[:, i] = acc[:, i-1] + arr[:, i]

        if acc[:, i].max() > k:
            cnt += 1
            acc[:, i] = arr[:, i]

    return cnt


ans = float("inf")
for pat in product([0, 1], repeat=h):
    if pat[0]:
        break

    hsp = hsplit(pat)

    if hsp.max() > k:
        continue

    wsp_cnt = greedy(hsp)
    hsp_cnt = hsp.shape[0] - 1

    cnt = hsp_cnt + wsp_cnt
    ans = min(ans, cnt)

print(ans)


Fixed submission:
from itertools import product

h, w, k = map(int, input().split())
s = [list(map(int, input())) for _ in range(h)]

ans = float("inf")
for pat in product([1, 0], repeat=h):
    prev = 2
    sm = -1
    for e in pat:
        if e != prev:
            sm += 1
        prev = e
    #print(pat)
    #print(sm)

    ss = [[0] * w for _ in range(h)]
    for wi in range(w):
        acc = 0
        prev = 2
        for hi, e in enumerate(pat):
            if e != prev:
                acc = 0
            if s[hi][wi]:
                acc += 1
            ss[hi][wi] = acc
            prev = e

    sss = [[0] * w for _ in range(h)]
    for hi in range(h):
        for wi in range(w):
            if wi == 0:
                sss[hi][wi] = ss[hi][wi]
            else:
                sss[hi][wi] = sss[hi][wi-1] + ss[hi][wi]

    base = [0] * h
    cp_prev = [0] * h
    for wi in range(w):
        mx = 0
        cp = [0] * h
        for hi in range(h):
            mx = max(mx, sss[hi][wi] - base[hi])
            cp[hi] = sss[hi][wi]
        if mx > k:
            if wi == 0:
                sm = 10000
            sm += 1
            base = cp_prev
        cp_prev = cp

    ans = min(ans, sm)

print(ans)

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

h, w, k = map(int, input().split())
s = np.array([list(map(int, input())) for _ in range(h)], dtype=np.int32)


def hsplit(pat):
    prev = 2
    ret = np.zeros((h + 1, w), dtype=np.int32)
    white = np.zeros(w, dtype=np.int32)
    cnt = 0
    for i, e in enumerate(pat):
        if e == prev:
            white += s[i]
        else:
            ret[cnt] = white.copy()
            white = s[i].copy()
            cnt += 1

        prev = e

    ret[cnt] = white.copy()
    cnt += 1

    return ret[1:cnt]


def greedy(arr):
    acc = np.zeros_like(arr, dtype=np.int32)
    ww = arr.shape[1]
    cnt = 0
    for i in range(ww):
        if i == 0:
            acc[:, i] = arr[:, i]
        else:
            acc[:, i] = acc[:, i-1] + arr[:, i]

        if acc[:, i].max() > k:
            cnt += 1
            acc[:, i] = arr[:, i]

    return cnt


ans = float("inf")
for pat in product([0, 1], repeat=h):
    if pat[0]:
        break

    hsp = hsplit(pat)

    if hsp.max() > k:
        continue

    wsp_cnt = greedy(hsp)
    hsp_cnt = hsp.shape[0] - 1

    cnt = hsp_cnt + wsp_cnt
    ans = min(ans, cnt)

print(ans)


Fixed submission:
from itertools import product

h, w, k = map(int, input().split())
s = [list(map(int, input())) for _ in range(h)]

ans = float("inf")
for pat in product([1, 0], repeat=h):
    prev = 2
    sm = -1
    for e in pat:
        if e != prev:
            sm += 1
        prev = e
    #print(pat)
    #print(sm)

    ss = [[0] * w for _ in range(h)]
    for wi in range(w):
        acc = 0
        prev = 2
        for hi, e in enumerate(pat):
            if e != prev:
                acc = 0
            if s[hi][wi]:
                acc += 1
            ss[hi][wi] = acc
            prev = e

    sss = [[0] * w for _ in range(h)]
    for hi in range(h):
        for wi in range(w):
            if wi == 0:
                sss[hi][wi] = ss[hi][wi]
            else:
                sss[hi][wi] = sss[hi][wi-1] + ss[hi][wi]

    base = [0] * h
    cp_prev = [0] * h
    for wi in range(w):
        mx = 0
        cp = [0] * h
        for hi in range(h):
            mx = max(mx, sss[hi][wi] - base[hi])
            cp[hi] = sss[hi][wi]
        if mx > k:
            if wi == 0:
                sm = 10000
            sm += 1
            base = cp_prev
        cp_prev = cp

    ans = min(ans, sm)

print(ans)

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

h, w, k = map(int, input().split())
s = np.array([list(map(int, input())) for _ in range(h)], dtype=np.int32)


def hsplit(pat):
    prev = 2
    ret = np.zeros((0, w), dtype=np.int32)
    white = np.zeros(w, dtype=np.int32)
    for i, e in enumerate(pat):
        if e == prev:
            white += s[i]
        else:
            ret = np.vstack([ret, white])
            white = s[i].copy()

        prev = e

    ret = np.vstack([ret, white])

    return ret[1:]


def greedy(arr):
    acc = np.zeros_like(arr, dtype=np.int32)
    ww = arr.shape[1]
    cnt = 0
    for i in range(ww):
        if i == 0:
            acc[:, i] = arr[:, i]
        else:
            acc[:, i] = acc[:, i-1] + arr[:, i]

        if acc[:, i].max() > k:
            cnt += 1
            acc[:, i] = arr[:, i]

    return cnt


ans = float("inf")
for pat in product([0, 1], repeat=h):
    if pat[0]:
        break

    hsp = hsplit(pat)

    if hsp.max() > k:
        continue

    wsp_cnt = greedy(hsp)
    hsp_cnt = hsp.shape[0] - 1

    cnt = hsp_cnt + wsp_cnt
    ans = min(ans, cnt)

print(ans)


Fixed submission:
from itertools import product

h, w, k = map(int, input().split())
s = [list(map(int, input())) for _ in range(h)]

ans = float("inf")
for pat in product([1, 0], repeat=h):
    prev = 2
    sm = -1
    for e in pat:
        if e != prev:
            sm += 1
        prev = e
    #print(pat)
    #print(sm)

    ss = [[0] * w for _ in range(h)]
    for wi in range(w):
        acc = 0
        prev = 2
        for hi, e in enumerate(pat):
            if e != prev:
                acc = 0
            if s[hi][wi]:
                acc += 1
            ss[hi][wi] = acc
            prev = e

    sss = [[0] * w for _ in range(h)]
    for hi in range(h):
        for wi in range(w):
            if wi == 0:
                sss[hi][wi] = ss[hi][wi]
            else:
                sss[hi][wi] = sss[hi][wi-1] + ss[hi][wi]

    base = [0] * h
    cp_prev = [0] * h
    for wi in range(w):
        mx = 0
        cp = [0] * h
        for hi in range(h):
            mx = max(mx, sss[hi][wi] - base[hi])
            cp[hi] = sss[hi][wi]
        if mx > k:
            if wi == 0:
                sm = 10000
            sm += 1
            base = cp_prev
        cp_prev = cp

    ans = min(ans, sm)

print(ans)

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

h, w, k = map(int, input().split())
s = [list(map(int, input())) for _ in range(h)]

ans = float("inf")
for pat in product([1, 0], repeat=h):
    prev = 2
    sm = -1
    for e in pat:
        if e != prev:
            sm += 1
        prev = e
    #print(pat)
    #print(sm)

    ss = [[0] * w for _ in range(h)]
    for wi in range(w):
        acc = 0
        prev = 2
        for hi, e in enumerate(pat):
            if e != prev:
                acc = 0
            if s[hi][wi]:
                acc += 1
            ss[hi][wi] = acc
            prev = e

    sss = [[0] * w for _ in range(h)]
    for hi in range(h):
        for wi in range(w):
            if wi == 0:
                sss[hi][wi] = ss[hi][wi]
            else:
                sss[hi][wi] = sss[hi][wi-1] + ss[hi][wi]

    base = [0] * h
    cp_prev = [0] * h
    for wi in range(w):
        mx = 0
        cp = [0] * h
        for hi in range(h):
            mx = max(mx, sss[hi][wi] - base[hi])
            cp[hi] = sss[hi][wi]
        if mx > k:
            if wi == 0:
                sm = 10000
            sm += 1
            base = cp_prev
        cp_prev = cp

    ans = min(ans, sm)

print(ans)


Fixed submission:
from itertools import product

h, w, k = map(int, input().split())
s = [list(map(int, input())) for _ in range(h)]

ans = float("inf")
for pat in product([1, 0], repeat=h):
    prev = 2
    sm = -1
    for e in pat:
        if e != prev:
            sm += 1
        prev = e
    #print(pat)
    #print(sm)

    ss = [[0] * w for _ in range(h)]
    for wi in range(w):
        acc = 0
        prev = 2
        for hi, e in enumerate(pat):
            if e != prev:
                acc = 0
            if s[hi][wi]:
                acc += 1
            ss[hi][wi] = acc
            prev = e

    sss = [[0] * w for _ in range(h)]
    for hi in range(h):
        for wi in range(w):
            if wi == 0:
                sss[hi][wi] = ss[hi][wi]
            else:
                sss[hi][wi] = sss[hi][wi-1] + ss[hi][wi]

    base = [0] * h
    cp_prev = [0] * h
    for wi in range(w):
        mx = 0
        cp = [0] * h
        for hi in range(h):
            mx = max(mx, sss[hi][wi] - base[hi])
            cp[hi] = sss[hi][wi]
        if mx > k:
            if wi == 0:
                sm = 10000
            sm += 1
            base = cp_prev
        cp_prev = cp

    ans = min(ans, sm)

print(ans)

Buggy python submission (verdict: Time Limit Exceeded):
from itertools import product
from copy import deepcopy

h, w, k = map(int, input().split())
s = [list(map(int, input())) for _ in range(h)]

ans = float("inf")
for pat in product([1, 0], repeat=h):
    prev = 2
    sm = -1
    for e in pat:
        if e != prev:
            sm += 1
        prev = e
    #print(pat)
    #print(sm)

    ss = [[0] * w for _ in range(h)]
    for wi in range(w):
        acc = 0
        prev = 2
        for hi, e in enumerate(pat):
            if e != prev:
                acc = 0
            if s[hi][wi]:
                acc += 1
            ss[hi][wi] = acc
            prev = e

    sss = [[0] * w for _ in range(h)]
    for hi in range(h):
        for wi in range(w):
            if wi == 0:
                sss[hi][wi] = ss[hi][wi]
            else:
                sss[hi][wi] = sss[hi][wi-1] + ss[hi][wi]

    base = [0] * h
    cp_prev = [0] * h
    for wi in range(w):
        mx = 0
        cp = [0] * h
        for hi in range(h):
            mx = max(mx, sss[hi][wi] - base[hi])
            cp[hi] = sss[hi][wi]
        if mx > k:
            if wi == 0:
                sm = 10000
            sm += 1
            base = deepcopy(cp_prev)
        cp_prev = deepcopy(cp)

    ans = min(ans, sm)

print(ans)


Fixed submission:
from itertools import product

h, w, k = map(int, input().split())
s = [list(map(int, input())) for _ in range(h)]

ans = float("inf")
for pat in product([1, 0], repeat=h):
    prev = 2
    sm = -1
    for e in pat:
        if e != prev:
            sm += 1
        prev = e
    #print(pat)
    #print(sm)

    ss = [[0] * w for _ in range(h)]
    for wi in range(w):
        acc = 0
        prev = 2
        for hi, e in enumerate(pat):
            if e != prev:
                acc = 0
            if s[hi][wi]:
                acc += 1
            ss[hi][wi] = acc
            prev = e

    sss = [[0] * w for _ in range(h)]
    for hi in range(h):
        for wi in range(w):
            if wi == 0:
                sss[hi][wi] = ss[hi][wi]
            else:
                sss[hi][wi] = sss[hi][wi-1] + ss[hi][wi]

    base = [0] * h
    cp_prev = [0] * h
    for wi in range(w):
        mx = 0
        cp = [0] * h
        for hi in range(h):
            mx = max(mx, sss[hi][wi] - base[hi])
            cp[hi] = sss[hi][wi]
        if mx > k:
            if wi == 0:
                sm = 10000
            sm += 1
            base = cp_prev
        cp_prev = cp

    ans = min(ans, sm)

print(ans)