Problem p02733 (python)

Fixed submission:
import numpy as np

h, w, k = map(int, input().split())
s = []
for i in range(h):
    string = input()
    temp = []
    for j in range(w):
        temp.append(int(string[j]))
    s.append(temp)

s_arr = np.array(s)

ans = 10**9
for i in range(2**(h-1)):
    bin_str = format(i, '0'+str(h-1)+'b')
    kouryo = np.array([[0 for _ in range(w)]])
    cnt = 0
    start = 0
    for j in range(h-1):
        if bin_str[j] == '1':
            cnt += 1
            kouryo = np.concatenate((kouryo, s_arr[start:j+1].sum(axis=0, keepdims=True)), axis=0)
            start=j+1
    kouryo = np.concatenate((kouryo, s_arr[start:].sum(axis=0, keepdims=True)), axis=0)

    if cnt > ans: continue
    if np.any(kouryo>k): continue

    wa = np.array([0 for _ in range(len(kouryo))])
    for j in range(w):
        wa += kouryo[:, j]
        if np.max(wa)>k:
            cnt += 1
            if cnt > ans: break
            wa = kouryo[:, j]
    ans = min(ans, cnt)
print(ans)

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

h, w, k = map(int, input().split())
s = []
for i in range(h):
    string = input()
    temp = []
    for j in range(w):
        temp.append(int(string[j]))
    s.append(temp)

s_arr = np.array(s)

ans = 10**9
for i in range(2**(h-1)):
    bin_str = format(i, '0'+str(h-1)+'b')
    kouryo = np.array([[0 for _ in range(w)]])
    cnt = 0
    start = 0
    new = 0
    for j in range(h-1):
        if bin_str[j] == '1':
            cnt += 1
            new = s_arr[start:j+1].sum(axis=0, keepdims=True)
            if np.any(new>k):
                new = -1
                break
            kouryo = np.concatenate((kouryo, new), axis=0)
            start=j+1
    if new == -1: continue
    kouryo = np.concatenate((kouryo, s_arr[start:].sum(axis=0, keepdims=True)), axis=0)

    wa = kouryo[:, 0]
    for j in range(1, w):
        wa = wa + kouryo[:, j]
        if np.any(wa>k):
            cnt += 1
            wa = kouryo[:, j]
    ans = min(ans, cnt)
print(ans)

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

h, w, k = map(int, input().split())
s = []
for i in range(h):
    string = input()
    temp = []
    for j in range(w):
        temp.append(int(string[j]))
    s.append(temp)

s_arr = np.array(s)

ans = 10**9
for i in range(2**(h-1)):
    bin_str = format(i, '0'+str(h-1)+'b')
    kouryo = np.array([[0 for _ in range(w)]])
    cnt = 0
    start = 0
    for j in range(h-1):
        if bin_str[j] == '1':
            cnt += 1
            new = s_arr[start:j+1].sum(axis=0, keepdims=True)
            if np.any(new>k):
                new = 0
                break
            kouryo = np.concatenate((kouryo, new), axis=0)
            start=j+1
    if new == 0: continue
    kouryo = np.concatenate((kouryo, s_arr[start:].sum(axis=0, keepdims=True)), axis=0)

    wa = kouryo[:, 0]
    for j in range(1, w):
        wa = wa + kouryo[:, j]
        if np.any(wa>k):
            cnt += 1
            wa = kouryo[:, j]
    ans = min(ans, cnt)
print(ans)

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

h, w, k = map(int, input().split())
s = []
for i in range(h):
    string = input()
    temp = []
    for j in range(w):
        temp.append(int(string[j]))
    s.append(temp)

sum_s = [[0 for i in range(w)] for j in range(h)]
if s[0][0] == 1: sum_s[0][0] = 1
for j in range(1, w):
    sum_s[0][j] = sum_s[0][j-1] + s[0][j]
for i in range(1, h):
    sum_s[i][0] = sum_s[i-1][0] + s[i][0]
for i in range(1, h):
    for j in range(1, w):
        sum_s[i][j] = sum_s[i-1][j] + sum_s[i][j-1] - sum_s[i-1][j-1] + s[i][j]
sum_s = np.array(sum_s)

ans = 10**9
for i in range(2**(h-1)):
    bin_str = format(i, '0'+str(h-1)+'b')
    kouryo = []
    temp_s = sum_s[:]
    cnt = 0
    for j in range(h-1):
        if bin_str[j] == '1':
            cnt += 1
            kouryo.append(list(temp_s[j, :]))
            temp_s[j+1:, :] = temp_s[j+1:, :] - temp_s[j, :]
    kouryo.append(list(sum_s[h-1, :]))

    kouryo = np.array(kouryo)
    for j in range(1, w):
        if cnt >= 10**9: break
        for m in range(len(kouryo)):
            if cnt >= 10**9: break
            if kouryo[m, j] > k:
                kouryo[:, j:] = kouryo[:, j:] - kouryo[:, j-1]
                cnt += 1
                break
        for m in range(len(kouryo)):
            if kouryo[m, j-1] > k:
                cnt += 10**9
                break
    ans = min(ans, cnt)
print(ans)