Buggy python submission (verdict: Time Limit Exceeded):
import sys
import math
import string
import fractions
import random
from operator import itemgetter
import itertools
from collections import deque
import copy
import heapq
from bisect import bisect, bisect_left, bisect_right

MOD = 10 ** 9 + 7
INF = float('inf')
input = lambda: sys.stdin.readline().strip()

sys.setrecursionlimit(10 ** 8)

def solve():
    H, W, K = map(int, input().split())
    S = [list(map(int, list(input()))) for _ in range(H)]
    ans = INF
    for i in range(2 ** (H - 1)):
        p = deque()
        for j in range(H - 1):
            if (i >> j) & 1:
                p.append(j + 1)
        num = len(p)
        if len(p) == 0:
            check = 0
            for j in range(W):
                temp_sum = sum([S[l][j] for l in range(H)])
                if temp_sum > K:
                    num = INF
                if check + temp_sum > K:
                    check = temp_sum
                    num += 1
                else:
                    check += temp_sum
        else:
            p.appendleft(0)
            p.append(H)
            check = [0] * (len(p) - 1)
            for j in range(W):
                for l in range(len(p) - 1):
                    if p[l + 1] - p[l] == 1:
                        check[l] += S[l][j]
                    else:
                        temp_sum = sum([S[t][j] for t in range(p[l], p[l + 1])])
                        if temp_sum > K:
                            num = INF
                            break
                        check[l] += temp_sum
                for l in check:
                    if l > K:
                        num += 1
                        for k in range(len(p) - 1):
                            if p[k + 1] - p[k] == 1:
                                check[k] = S[k][j]
                            else:
                                temp_sum = sum([S[t][j] for t in range(p[k], p[k + 1])])
                                if temp_sum > K:
                                    num = INF
                                check[k] = temp_sum
                        break
        if num < ans:
            ans = num
    print(ans)


if __name__ == "__main__":
    solve()



Fixed submission:
import sys
import math
import string
import fractions
import random
from operator import itemgetter
import itertools
from collections import deque
import copy
import heapq
from bisect import bisect, bisect_left, bisect_right

MOD = 10 ** 9 + 7
INF = float('inf')
input = lambda: sys.stdin.readline().strip()

sys.setrecursionlimit(10 ** 8)

H, W, K = map(int, input().split())
S = [list(map(int, list(input()))) for _ in range(H)]
ans = INF
for i in range(2 ** (H - 1)):
    p = deque()
    for j in range(H - 1):
        if (i >> j) & 1:
            p.append(j + 1)
    num = len(p)
    if len(p) == 0:
        check = 0
        for j in range(W):
            temp_sum = sum([S[l][j] for l in range(H)])
            if temp_sum > K:
                num = INF
            if check + temp_sum > K:
                check = temp_sum
                num += 1
            else:
                check += temp_sum
    else:
        p.appendleft(0)
        p.append(H)
        check = [0] * (len(p) - 1)
        for j in range(W):
            for l in range(len(p) - 1):
                if p[l + 1] - p[l] == 1:
                    check[l] += S[l][j]
                else:
                    temp_sum = sum([S[t][j] for t in range(p[l], p[l + 1])])
                    if temp_sum > K:
                        num = INF
                        break
                    check[l] += temp_sum
            for l in check:
                if l > K:
                    num += 1
                    for k in range(len(p) - 1):
                        if p[k + 1] - p[k] == 1:
                            check[k] = S[k][j]
                        else:
                            temp_sum = sum([S[t][j] for t in range(p[k], p[k + 1])])
                            if temp_sum > K:
                                num = INF
                            check[k] = temp_sum
                    break
    if num < ans:
        ans = num
print(ans)

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

MOD = 10 ** 9 + 7
INF = float('inf')
input = lambda: sys.stdin.readline().strip()

sys.setrecursionlimit(10 ** 8)


def solve():
    H, W, K = map(int, input().split())
    S = [list(map(int, list(input()))) for _ in range(H)]
    ans = INF
    for i in range(2 ** (H - 1)):
        num = 0
        p = [0]
        S_part = []
        for j in range(H - 1):
            if (i >> j) & 1:
                if j + 1 - p[-1] > 1:
                    S_part.append(S[p[-1]:j + 1])
                else:
                    temp_S_part = S[p[-1]:j + 1]
                    temp_S_part = np.insert(temp_S_part, 1, np.zeros(W), axis=0)
                    S_part.append(temp_S_part)
                p.append(j + 1)
        if p[-1] != H:
            temp_S_part = S[p[-1]:H]
            temp_S_part = np.insert(temp_S_part, 1, np.zeros(W), axis=0)
            S_part.append(temp_S_part)
        num = len(S_part) - 1
        if len(S_part) == 1:
            S_part.append(np.zeros((2, W)))
        for j in range(len(S_part)):
            S_part[j] = np.cumsum(S_part[j], axis=0)
        check = [0] * (len(S_part))
        for j in range(W):
            for l in range(len(S_part)):
                check[l] += S_part[l][-1][j]
            for l in range(len(S_part)):
                if check[l] > K:
                    if j == 0:
                        num += INF
                    check = [S_part[l][-1][j] for l in range(len(S_part))]
                    num += 1
                    break
        if ans > num:
            ans = num
    print(ans)


if __name__ == "__main__":
    solve()

Fixed submission:
import sys
import math
import string
import fractions
import random
from operator import itemgetter
import itertools
from collections import deque
import copy
import heapq
from bisect import bisect, bisect_left, bisect_right

MOD = 10 ** 9 + 7
INF = float('inf')
input = lambda: sys.stdin.readline().strip()

sys.setrecursionlimit(10 ** 8)

H, W, K = map(int, input().split())
S = [list(map(int, list(input()))) for _ in range(H)]
ans = INF
for i in range(2 ** (H - 1)):
    p = deque()
    for j in range(H - 1):
        if (i >> j) & 1:
            p.append(j + 1)
    num = len(p)
    if len(p) == 0:
        check = 0
        for j in range(W):
            temp_sum = sum([S[l][j] for l in range(H)])
            if temp_sum > K:
                num = INF
            if check + temp_sum > K:
                check = temp_sum
                num += 1
            else:
                check += temp_sum
    else:
        p.appendleft(0)
        p.append(H)
        check = [0] * (len(p) - 1)
        for j in range(W):
            for l in range(len(p) - 1):
                if p[l + 1] - p[l] == 1:
                    check[l] += S[l][j]
                else:
                    temp_sum = sum([S[t][j] for t in range(p[l], p[l + 1])])
                    if temp_sum > K:
                        num = INF
                        break
                    check[l] += temp_sum
            for l in check:
                if l > K:
                    num += 1
                    for k in range(len(p) - 1):
                        if p[k + 1] - p[k] == 1:
                            check[k] = S[k][j]
                        else:
                            temp_sum = sum([S[t][j] for t in range(p[k], p[k + 1])])
                            if temp_sum > K:
                                num = INF
                            check[k] = temp_sum
                    break
    if num < ans:
        ans = num
print(ans)

Buggy python submission (verdict: Runtime Error):
import sys
import math
import string
import fractions
import random
from operator import itemgetter
import itertools
from collections import deque
import copy
import heapq
from bisect import bisect, bisect_left, bisect_right
import numpy as np

MOD = 10 ** 9 + 7
INF = float('inf')
input = lambda: sys.stdin.readline().strip()

sys.setrecursionlimit(10 ** 8)

H, W, K = map(int, input().split())
S = [list(map(int, list(input()))) for _ in range(H)]
ans = INF
for i in range(2 ** (H - 1)):
    p = deque()
    for j in range(H - 1):
        if (i >> j) & 1:
            p.append(j + 1)
    num = len(p)
    if len(p) == 0:
        check = 0
        for j in range(W):
            temp_sum = sum([S[l][j] for l in range(H)])
            if check + temp_sum > K:
                check = temp_sum
                num += 1
            else:
                check += temp_sum
    else:
        p.appendleft(0)
        p.append(H)
        check = [0] * (len(p) - 1)
        for j in range(W):
            for l in range(len(p)-1):
                if p[l+1] - p[l] == 1:
                    check[l] += S[l][j]
                else:
                    check[l] += sum([S[t][j] for t in range(p[l], p[l+1])])
            for l in check:
                if l > K:
                    num += 1
                    for l in range(len(p) - 1):
                        if p[l + 1] - p[l] == 1:
                            check[l] = S[l][j]
                        else:
                            check[l] = sum([S[t][j] for t in range(p[l], p[l + 1])])
                    break
    if num < ans:
        ans = num
print(ans)

Fixed submission:
import sys
import math
import string
import fractions
import random
from operator import itemgetter
import itertools
from collections import deque
import copy
import heapq
from bisect import bisect, bisect_left, bisect_right

MOD = 10 ** 9 + 7
INF = float('inf')
input = lambda: sys.stdin.readline().strip()

sys.setrecursionlimit(10 ** 8)

H, W, K = map(int, input().split())
S = [list(map(int, list(input()))) for _ in range(H)]
ans = INF
for i in range(2 ** (H - 1)):
    p = deque()
    for j in range(H - 1):
        if (i >> j) & 1:
            p.append(j + 1)
    num = len(p)
    if len(p) == 0:
        check = 0
        for j in range(W):
            temp_sum = sum([S[l][j] for l in range(H)])
            if temp_sum > K:
                num = INF
            if check + temp_sum > K:
                check = temp_sum
                num += 1
            else:
                check += temp_sum
    else:
        p.appendleft(0)
        p.append(H)
        check = [0] * (len(p) - 1)
        for j in range(W):
            for l in range(len(p) - 1):
                if p[l + 1] - p[l] == 1:
                    check[l] += S[l][j]
                else:
                    temp_sum = sum([S[t][j] for t in range(p[l], p[l + 1])])
                    if temp_sum > K:
                        num = INF
                        break
                    check[l] += temp_sum
            for l in check:
                if l > K:
                    num += 1
                    for k in range(len(p) - 1):
                        if p[k + 1] - p[k] == 1:
                            check[k] = S[k][j]
                        else:
                            temp_sum = sum([S[t][j] for t in range(p[k], p[k + 1])])
                            if temp_sum > K:
                                num = INF
                            check[k] = temp_sum
                    break
    if num < ans:
        ans = num
print(ans)

Buggy python submission (verdict: Time Limit Exceeded):
import sys
import math
import string
import fractions
import random
from operator import itemgetter
import itertools
from collections import deque
import copy
import heapq
from bisect import bisect, bisect_left, bisect_right
import numpy as np

MOD = 10 ** 9 + 7
INF = float('inf')
input = lambda: sys.stdin.readline().strip()

sys.setrecursionlimit(10 ** 8)

H, W, K = map(int, input().split())
S = [list(map(int, list(input()))) for _ in range(H)]
ans = INF
for i in range(2 ** (H - 1)):
    p = deque()
    for j in range(H - 1):
        if (i >> j) & 1:
            p.append(j + 1)
    num = len(p)
    if len(p) == 0:
        check = 0
        for j in range(W):
            temp_sum = sum([S[l][j] for l in range(H)])
            if check + temp_sum > K:
                check = temp_sum
                num += 1
            else:
                check += temp_sum
    else:
        p.appendleft(0)
        p.append(H)
        check = [0] * (len(p) - 1)
        for j in range(W):
            for l in range(len(p)-1):
                if p[l+1] - p[l] == 1:
                    check[l] += S[l][j]
                else:
                    check[l] += sum([S[t][j] for t in range(p[l], p[l+1])])
            for l in check:
                if l > K:
                    num += 1
                    for l in range(len(p) - 1):
                        if p[l + 1] - p[l] == 1:
                            check[l] = S[l][j]
                        else:
                            check[l] = sum([S[t][j] for t in range(p[l], p[l + 1])])
                    break
    if num < ans:
        ans = num
print(ans)


Fixed submission:
import sys
import math
import string
import fractions
import random
from operator import itemgetter
import itertools
from collections import deque
import copy
import heapq
from bisect import bisect, bisect_left, bisect_right

MOD = 10 ** 9 + 7
INF = float('inf')
input = lambda: sys.stdin.readline().strip()

sys.setrecursionlimit(10 ** 8)

H, W, K = map(int, input().split())
S = [list(map(int, list(input()))) for _ in range(H)]
ans = INF
for i in range(2 ** (H - 1)):
    p = deque()
    for j in range(H - 1):
        if (i >> j) & 1:
            p.append(j + 1)
    num = len(p)
    if len(p) == 0:
        check = 0
        for j in range(W):
            temp_sum = sum([S[l][j] for l in range(H)])
            if temp_sum > K:
                num = INF
            if check + temp_sum > K:
                check = temp_sum
                num += 1
            else:
                check += temp_sum
    else:
        p.appendleft(0)
        p.append(H)
        check = [0] * (len(p) - 1)
        for j in range(W):
            for l in range(len(p) - 1):
                if p[l + 1] - p[l] == 1:
                    check[l] += S[l][j]
                else:
                    temp_sum = sum([S[t][j] for t in range(p[l], p[l + 1])])
                    if temp_sum > K:
                        num = INF
                        break
                    check[l] += temp_sum
            for l in check:
                if l > K:
                    num += 1
                    for k in range(len(p) - 1):
                        if p[k + 1] - p[k] == 1:
                            check[k] = S[k][j]
                        else:
                            temp_sum = sum([S[t][j] for t in range(p[k], p[k + 1])])
                            if temp_sum > K:
                                num = INF
                            check[k] = temp_sum
                    break
    if num < ans:
        ans = num
print(ans)

Buggy python submission (verdict: Time Limit Exceeded):
import sys
import math
import string
import fractions
import random
from operator import itemgetter
import itertools
from collections import deque
import copy
import heapq
from bisect import bisect, bisect_left, bisect_right
import numpy as np

MOD = 10 ** 9 + 7
INF = float('inf')
input = lambda: sys.stdin.readline().strip()

sys.setrecursionlimit(10 ** 8)

H, W, K = map(int, input().split())
S = [list(map(int, list(input()))) for _ in range(H)]
ans = INF
for i in range(2 ** (H - 1)):
    num = 0
    p = [0]
    S_part = []
    for j in range(H - 1):
        if (i >> j) & 1:
            if j + 1 - p[-1] > 1:
                S_part.append(S[p[-1]:j + 1])
            else:
                temp_S_part = S[p[-1]:j + 1]
                temp_S_part = np.insert(temp_S_part, 1, np.zeros(W), axis=0)
                S_part.append(temp_S_part)
            p.append(j + 1)
    if p[-1] != H:
        temp_S_part = S[p[-1]:H]
        temp_S_part = np.insert(temp_S_part, 1, np.zeros(W), axis=0)
        S_part.append(temp_S_part)
    num = len(S_part) - 1
    if len(S_part) == 1:
        S_part.append(np.zeros((2, W)))
    for j in range(len(S_part)):
        S_part[j] = np.cumsum(S_part[j], axis=0)
    check = [0] * (len(S_part))
    for j in range(W):
        for l in range(len(S_part)):
            check[l] += S_part[l][-1][j]
        for l in range(len(S_part)):
            if check[l] > K:
                if j == 0:
                    num += INF
                check = [S_part[l][-1][j] for l in range(len(S_part))]
                num += 1
                break
    if ans > num:
        ans = num
print(ans)


Fixed submission:
import sys
import math
import string
import fractions
import random
from operator import itemgetter
import itertools
from collections import deque
import copy
import heapq
from bisect import bisect, bisect_left, bisect_right

MOD = 10 ** 9 + 7
INF = float('inf')
input = lambda: sys.stdin.readline().strip()

sys.setrecursionlimit(10 ** 8)

H, W, K = map(int, input().split())
S = [list(map(int, list(input()))) for _ in range(H)]
ans = INF
for i in range(2 ** (H - 1)):
    p = deque()
    for j in range(H - 1):
        if (i >> j) & 1:
            p.append(j + 1)
    num = len(p)
    if len(p) == 0:
        check = 0
        for j in range(W):
            temp_sum = sum([S[l][j] for l in range(H)])
            if temp_sum > K:
                num = INF
            if check + temp_sum > K:
                check = temp_sum
                num += 1
            else:
                check += temp_sum
    else:
        p.appendleft(0)
        p.append(H)
        check = [0] * (len(p) - 1)
        for j in range(W):
            for l in range(len(p) - 1):
                if p[l + 1] - p[l] == 1:
                    check[l] += S[l][j]
                else:
                    temp_sum = sum([S[t][j] for t in range(p[l], p[l + 1])])
                    if temp_sum > K:
                        num = INF
                        break
                    check[l] += temp_sum
            for l in check:
                if l > K:
                    num += 1
                    for k in range(len(p) - 1):
                        if p[k + 1] - p[k] == 1:
                            check[k] = S[k][j]
                        else:
                            temp_sum = sum([S[t][j] for t in range(p[k], p[k + 1])])
                            if temp_sum > K:
                                num = INF
                            check[k] = temp_sum
                    break
    if num < ans:
        ans = num
print(ans)

Buggy python submission (verdict: Time Limit Exceeded):
import sys
import math
import string
import fractions
import random
from operator import itemgetter
import itertools
from collections import deque
import copy
import heapq
from bisect import bisect, bisect_left, bisect_right
import numpy as np

MOD = 10 ** 9 + 7
INF = float('inf')
input = lambda: sys.stdin.readline().strip()

sys.setrecursionlimit(10 ** 8)

H, W, K = map(int, input().split())
S = [list(map(int, list(input()))) for _ in range(H)]
ans = INF
for i in range(2 ** (H - 1)):
    num = 0
    p = [0]
    S_part = []
    for j in range(H - 1):
        if (i >> j) & 1:
            if j + 1 - p[-1] > 1:
                S_part.append(S[p[-1]:j + 1])
            else:
                temp_S_part = S[p[-1]:j + 1]
                temp_S_part = np.insert(temp_S_part, 1, np.zeros(W), axis=0)
                S_part.append(temp_S_part)
            p.append(j + 1)
    if p[-1] != H:
        temp_S_part = S[p[-1]:H]
        temp_S_part = np.insert(temp_S_part, 1, np.zeros(W), axis=0)
        S_part.append(temp_S_part)
    num = len(S_part) - 1
    if len(S_part) == 1:
        S_part.append(np.zeros((2, W)))
    for j in range(len(S_part)):
        S_part[j] = np.cumsum(S_part[j], axis=0)
    check = [0] * (len(S_part))
    for j in range(W):
        for l in range(len(S_part)):
            check[l] += S_part[l][-1][j]
        for l in range(len(S_part)):
            if check[l] > K:
                if j == 0:
                    num += INF
                check = [0] * (len(S_part))
                num += 1
                for l in range(len(S_part)):
                    check[l] += S_part[l][-1][j]
                break
    if ans > num:
        ans = num
print(ans)


Fixed submission:
import sys
import math
import string
import fractions
import random
from operator import itemgetter
import itertools
from collections import deque
import copy
import heapq
from bisect import bisect, bisect_left, bisect_right

MOD = 10 ** 9 + 7
INF = float('inf')
input = lambda: sys.stdin.readline().strip()

sys.setrecursionlimit(10 ** 8)

H, W, K = map(int, input().split())
S = [list(map(int, list(input()))) for _ in range(H)]
ans = INF
for i in range(2 ** (H - 1)):
    p = deque()
    for j in range(H - 1):
        if (i >> j) & 1:
            p.append(j + 1)
    num = len(p)
    if len(p) == 0:
        check = 0
        for j in range(W):
            temp_sum = sum([S[l][j] for l in range(H)])
            if temp_sum > K:
                num = INF
            if check + temp_sum > K:
                check = temp_sum
                num += 1
            else:
                check += temp_sum
    else:
        p.appendleft(0)
        p.append(H)
        check = [0] * (len(p) - 1)
        for j in range(W):
            for l in range(len(p) - 1):
                if p[l + 1] - p[l] == 1:
                    check[l] += S[l][j]
                else:
                    temp_sum = sum([S[t][j] for t in range(p[l], p[l + 1])])
                    if temp_sum > K:
                        num = INF
                        break
                    check[l] += temp_sum
            for l in check:
                if l > K:
                    num += 1
                    for k in range(len(p) - 1):
                        if p[k + 1] - p[k] == 1:
                            check[k] = S[k][j]
                        else:
                            temp_sum = sum([S[t][j] for t in range(p[k], p[k + 1])])
                            if temp_sum > K:
                                num = INF
                            check[k] = temp_sum
                    break
    if num < ans:
        ans = num
print(ans)