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

from itertools import product
ans = 2000

for idx,cond in enumerate(product((True,False),repeat=h-1)):
    div = sum(cond)
    spl = [arr[0][:]]
    for i in range(h-1):
        if cond[i]:
            spl.append(arr[i+1][:])
        else:
            spl[-1] = [spl[-1][j] + arr[i+1][j] for j in range(w)]
    num = len(spl)
    cnt = [0]*num
    
    for v in range(w):
        tmp = [spl[i][v] for i in range(num)]
        if max(tmp) > k:
            break
        if max([cnt[i] + tmp[i] for i in range(num)]) > k:
            div += 1
            cnt = tmp[:]
        else:
            cnt  = [cnt[i] + tmp[i] for i in range(num)]
    else:    
        ans = min(ans,div)
            
print(ans)

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

from itertools import product
ans = 2000

for idx,cond in enumerate(product((True,False),repeat=h-1)):
    div = sum(cond)
    spl = [arr[0][:]]
    for i in range(h-1):
        if cond[i]:
            spl.append(arr[i+1][:])
        else:
            spl[-1] = [spl[-1][j] + arr[i+1][j] for j in range(w)]
    num = len(spl)
    cnt = [0]*num
    
    for v in range(w):
        tmp = [spl[i][v] for i in range(num)]
        if max(tmp) > k:
            break
        if max([cnt[i] + tmp[i] for i in range(num)]) > k:
            div += 1
            cnt = tmp[:]
        else:
            cnt  = [cnt[i] + tmp[i] for i in range(num)]
    else:    
        ans = min(ans,div)
            
print(ans)

Buggy python submission (verdict: Runtime Error):
h,w,k = list(map(int,input().split()))
arr = [[int(i) for i in input()] for _ in range(h)]

from itertools import product
ans = [0]*(2**(h-1))

for idx,cond in enumerate(product((True,False),repeat=h-1)):
    div = sum(cond)
    spl = [arr[0][:]]
    for i in range(h-1):
        if h_cond[i]:
            spl.append(arr[i+1][:])
        else:
            spl[-1] = [spl[-1][j] + arr[i+1][j] for j in range(w)]
    num = len(spl)
    cnt = [0]*num
    
    for v in range(w):
        tmp = [spl[i][v] for i in range(num)]
        if max(tmp) > k:
            break
        if max([cnt[i] + tmp[i] for i in range(num)]) > k:
            div += 1
            cnt = tmp[:]
        else:
            cnt  = [cnt[i] + tmp[i] for i in range(num)]
    else:    
        ans = min(ans,div)
            
print(min(ans))

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

from itertools import product
ans = 2000

for idx,cond in enumerate(product((True,False),repeat=h-1)):
    div = sum(cond)
    spl = [arr[0][:]]
    for i in range(h-1):
        if cond[i]:
            spl.append(arr[i+1][:])
        else:
            spl[-1] = [spl[-1][j] + arr[i+1][j] for j in range(w)]
    num = len(spl)
    cnt = [0]*num
    
    for v in range(w):
        tmp = [spl[i][v] for i in range(num)]
        if max(tmp) > k:
            break
        if max([cnt[i] + tmp[i] for i in range(num)]) > k:
            div += 1
            cnt = tmp[:]
        else:
            cnt  = [cnt[i] + tmp[i] for i in range(num)]
    else:    
        ans = min(ans,div)
            
print(ans)

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

from itertools import product
ans = [0]*(2**(h-1))

for idx,h_cond in enumerate(product((True,False),repeat=h-1)):
    div = sum(h_cond)
    splits = [arr[0][:]]
    for i in range(h-1):
        if h_cond[i]:
            splits.append(arr[i+1][:])
        else:
            splits[-1] = [splits[-1][j] + arr[i+1][j] for j in range(w)]
    block = len(splits)
    count = [0]*block
    
    for v_pos in range(w):
        tmp = [splits[i][v_pos] for i in range(block)]
        if max(tmp) > k:
            div = 2000
            break
        if max([count[i] + tmp[i] for i in range(block)]) > k:
            div += 1
            count = tmp[:]
        else:
            count  = [count[i] + tmp[i] for i in range(block)]
    
    ans[idx] = div
            
print(min(ans))

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

from itertools import product
ans = 2000

for idx,cond in enumerate(product((True,False),repeat=h-1)):
    div = sum(cond)
    spl = [arr[0][:]]
    for i in range(h-1):
        if cond[i]:
            spl.append(arr[i+1][:])
        else:
            spl[-1] = [spl[-1][j] + arr[i+1][j] for j in range(w)]
    num = len(spl)
    cnt = [0]*num
    
    for v in range(w):
        tmp = [spl[i][v] for i in range(num)]
        if max(tmp) > k:
            break
        if max([cnt[i] + tmp[i] for i in range(num)]) > k:
            div += 1
            cnt = tmp[:]
        else:
            cnt  = [cnt[i] + tmp[i] for i in range(num)]
    else:    
        ans = min(ans,div)
            
print(ans)

Buggy python submission (verdict: Runtime Error):
h,w,k = list(map(int,input().split()))
arr = [input() for _ in range(h)]

from itertools import product
import numpy as np
ans = [0] * (2**(h-1))
ans_index = 0

for horizontal_condition in product((True,False),repeat=h-1):
    count = np.zeros(h)
    divide = sum(horizontal_condition)
    trans_matrix = np.zeros((h,h))
    trans_matrix[0,0] = 1
    down = 0
    right = 0
    
    for i in horizontal_condition:
        if i:
            down += 1
        right += 1
        trans_matrix[down,right] = 1
        
    for vertical_divide in range(w):
        tmparr = np.array([int(i[vertical_divide]) for i in arr])
        addarr = np.dot(trans_matrix,tmparr)
        if max(count+addarr) > k:
            count = addarr
            divide += 1
        else:
            count += addarr
            
    ans[ans_index] = divide
    ans_index += 1
    
print(min(ans))

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

from itertools import product
ans = 2000

for idx,cond in enumerate(product((True,False),repeat=h-1)):
    div = sum(cond)
    spl = [arr[0][:]]
    for i in range(h-1):
        if cond[i]:
            spl.append(arr[i+1][:])
        else:
            spl[-1] = [spl[-1][j] + arr[i+1][j] for j in range(w)]
    num = len(spl)
    cnt = [0]*num
    
    for v in range(w):
        tmp = [spl[i][v] for i in range(num)]
        if max(tmp) > k:
            break
        if max([cnt[i] + tmp[i] for i in range(num)]) > k:
            div += 1
            cnt = tmp[:]
        else:
            cnt  = [cnt[i] + tmp[i] for i in range(num)]
    else:    
        ans = min(ans,div)
            
print(ans)

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

from itertools import product
import numpy as np
ans = [0] * (2**(h-1))
ans_index = 0

for horizontal_condition in product((True,False),repeat=h-1):
    count = np.zeros(h)
    divide = sum(horizontal_condition)
    trans_matrix = np.zeros((h,h))
    trans_matrix[0,0] = 1
    down = 0
    right = 0
    
    for i in horizontal_condition:
        if i:
            down += 1
        right += 1
        trans_matrix[down,right] = 1
        
    for vertical_divide in range(w):
        tmparr = np.array([int(i[vertical_divide]) for i in arr])
        addarr = np.dot(trans_matrix,tmparr)
        if max(count+addarr) > k:
            count = addarr
            divide += 1
        else:
            count += addarr
            
    ans[ans_index] = divide
    ans_index += 1
    
print(min(ans))

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

from itertools import product
ans = 2000

for idx,cond in enumerate(product((True,False),repeat=h-1)):
    div = sum(cond)
    spl = [arr[0][:]]
    for i in range(h-1):
        if cond[i]:
            spl.append(arr[i+1][:])
        else:
            spl[-1] = [spl[-1][j] + arr[i+1][j] for j in range(w)]
    num = len(spl)
    cnt = [0]*num
    
    for v in range(w):
        tmp = [spl[i][v] for i in range(num)]
        if max(tmp) > k:
            break
        if max([cnt[i] + tmp[i] for i in range(num)]) > k:
            div += 1
            cnt = tmp[:]
        else:
            cnt  = [cnt[i] + tmp[i] for i in range(num)]
    else:    
        ans = min(ans,div)
            
print(ans)