Buggy python submission (verdict: Time Limit Exceeded):
h,w,k = map(int,input().split())
s = []
for i in range(h):
    s0 = list(input())
    s.append(s0)
al = []
wl = [[0 for i in range(h)] for i in range(w)]
for i in range(w):
    count = 0
    for j in range(h):
        if s[j][i] == "1":
            count += 1
        wl[i][j] = count
for i in range(2**(h-1)):
    ans = 0
    b = []
    for j in range(h-1):
        if ((i >> j) & 1):
            b.append(j)
    #print(b)
    b.append(h-1)
    bl = []
    now = -1
    for j in b:
        if now == -1:
            bl.append(wl[0][j])
            now = j
        else:
            bl.append(wl[0][j]-wl[0][now])
            now = j
    ans = len(bl)-1
    key = 1
    key2 = 0
    for j in range(1,w):
        now = -1
        aa = []
        key2 = 0
        for x in  range(len(bl)):
            #print(bl)
            if now == -1:
                if bl[x] > k:
                    key = 0
                    break
                else:
                    bl[x] += wl[j][b[x]]
                    aa.append(wl[j][b[x]])
                    if bl[x] > k:
                        key2 = 1
                    now = b[x]
            else:
                if bl[x] > k:
                    key = 0
                    break
                else:
                    bl[x] += wl[j][b[x]] - wl[j][now]
                    aa.append(wl[j][b[x]]-wl[j][now])
                    now = b[x]
                    if bl[x] > k:
                        key2 = 1
        if key2 == 1 and key == 1:
            for x in range(len(bl)):
                bl[x] = aa[x]
            ans += 1
        elif key == 0:
            break
    if key == 1:
        al.append(ans)
print(min(al))

Fixed submission:
h,w,k = map(int,input().split())
s = []
for i in range(h):
    s0 = list(input())
    s.append(s0)
al = []
wl = [[0 for i in range(h)] for i in range(w)]
for i in range(w):
    count = 0
    for j in range(h):
        if s[j][i] == "1":
            count += 1
        wl[i][j] = count
for i in range(2**(h-1)):
    ans = 0
    b = []
    for j in range(h-1):
        if ((i >> j) & 1):
            b.append(j)
    #print(b)
    b.append(h-1)
    bl = []
    now = -1
    for j in b:
        if now == -1:
            bl.append(wl[0][j])
            now = j
        else:
            bl.append(wl[0][j]-wl[0][now])
            now = j
    ans = len(bl)-1
    key = 1
    key2 = 0
    for j in range(1,w):
        now = -1
        aa = []
        key2 = 0
        for x in  range(len(bl)):
            #print(bl)
            if now == -1:
                if bl[x] > k:
                    key = 0
                    break
                else:
                    bl[x] += wl[j][b[x]]
                    aa.append(wl[j][b[x]])
                    if bl[x] > k:
                        key2 = 1
                    now = b[x]
            else:
                if bl[x] > k:
                    key = 0
                    break
                else:
                    bl[x] += wl[j][b[x]] - wl[j][now]
                    aa.append(wl[j][b[x]]-wl[j][now])
                    now = b[x]
                    if bl[x] > k:
                        key2 = 1
        if key2 == 1 and key == 1:
            for x in range(len(bl)):
                bl[x] = aa[x]
            ans += 1
        elif key == 0:
            break
    if key == 1:
        al.append(ans)
print(min(al))