Buggy python submission (verdict: Runtime Error):
H,W,K = map(int, input().split())
S = [[int(i) for i in list(input())] for _ in range(H)]

ans = float("inf")
for state in range(2 ** (H - 1)):
    # 横棒のカウント
    yoko = 0
    for i in range(H - 1):
        if (state >> i) & 1:
            yoko += 1

    res = yoko

    # ホワイトチョコレートの個数
    cntHW = [0] * (yoko + 1)
    for j in range(W):
        # 今のグループ
        now = 0
        # 列を探索
        cntH = [0] * (yoko + 1)
        for i in range(H):
            if S[i][j] == 1:
                cntH[now] += 1

            if (state >> i) & 1: # 区切り
                now += 1
        # 個数の最大値
        MAX = max(cH + cHW for cH,cHW in zip(cntH,cntHW))
        if MAX > K:
            res += 1
            cntHW = cntH.copy()
        elif MAX == K:
            if j == W - 1: continue
            res += 1
            cntHW = [0] * (yoko + 1)
        else:
            for i in range(yoko + 1):
                cntHW[i] += cntH[i]

    ans = min(ans, res)

print(ans)

Fixed submission:
H,W,K = map(int, input().split())
S = [[int(i) for i in list(input())] for _ in range(H)]

ans = float("inf")
for state in range(2 ** (H - 1)):
    flag = True

    # 横棒のカウント
    yoko = 0
    for i in range(H - 1):
        if (state >> i) & 1:
            yoko += 1

    res = yoko

    # ホワイトチョコレートの個数
    cntHW = [0] * (yoko + 1)
    for j in range(W):
        # 今のグループ
        now = 0
        # 列を探索
        cntH = [0] * (yoko + 1)
        for i in range(H):
            if S[i][j] == 1:
                cntH[now] += 1

            if (state >> i) & 1: # 区切り
                now += 1
        
        for cH in cntH:
            if cH > K:
                flag = False

        # 個数の最大値
        MAX = max(cH + cHW for cH,cHW in zip(cntH,cntHW))
        if MAX > K:
            res += 1
            cntHW = cntH[::]
        else:
            for i in range(yoko + 1):
                cntHW[i] += cntH[i]

    if flag:
        ans = min(ans, res)

print(ans)

Buggy python submission (verdict: Runtime Error):
def main():
    H,W,K = map(int, input().split())
    S = [[int(i) for i in list(input())] for _ in range(H)]

    ans = float("inf")
    for state in range(2 ** (H - 1)):
        # 横棒のカウント
        yoko = 0
        for i in range(H - 1):
            if (state >> i) & 1:
                yoko += 1

        res = yoko

        # ホワイトチョコレートの個数
        cntHW = [0] * (yoko + 1)
        for j in range(W):
            # 今のグループ
            now = 0
            # 列を探索
            cntH = [0] * (yoko + 1)
            for i in range(H):
                if S[i][j] == 1:
                    cntH[now] += 1

                if (state >> i) & 1: # 区切り
                    now += 1
            # 個数の最大値
            MAX = max(cH + cHW for cH,cHW in zip(cntH,cntHW))
            if MAX > K:
                res += 1
                cntHW = cntH.copy()
            elif MAX == K:
                if j == W - 1: continue
                res += 1
                cntHW = [0] * (yoko + 1)
            else:
                for i in range(yoko + 1):
                    cntHW[i] += cntH[i]

        ans = min(ans, res)

    print(ans)

if __name__ == "__main__":
    main()

Fixed submission:
H,W,K = map(int, input().split())
S = [[int(i) for i in list(input())] for _ in range(H)]

ans = float("inf")
for state in range(2 ** (H - 1)):
    flag = True

    # 横棒のカウント
    yoko = 0
    for i in range(H - 1):
        if (state >> i) & 1:
            yoko += 1

    res = yoko

    # ホワイトチョコレートの個数
    cntHW = [0] * (yoko + 1)
    for j in range(W):
        # 今のグループ
        now = 0
        # 列を探索
        cntH = [0] * (yoko + 1)
        for i in range(H):
            if S[i][j] == 1:
                cntH[now] += 1

            if (state >> i) & 1: # 区切り
                now += 1
        
        for cH in cntH:
            if cH > K:
                flag = False

        # 個数の最大値
        MAX = max(cH + cHW for cH,cHW in zip(cntH,cntHW))
        if MAX > K:
            res += 1
            cntHW = cntH[::]
        else:
            for i in range(yoko + 1):
                cntHW[i] += cntH[i]

    if flag:
        ans = min(ans, res)

print(ans)