Buggy python submission (verdict: Time Limit Exceeded):
n, k = map(int, input().split())
s = input()
s2 = [["1", 0, 0]]
zero = 0
one_count = 1
for i, c in enumerate(s):
    if s2[-1][0] == c:
        s2[-1][2] += 1
    else:
        s2.append([c, i, 1])
        zero += (1 - int(c))
        if c == "1":
            one_count += 1
if zero <= k:
    print(n)
    exit()
if s2[-1][0] == "0":
    s2.append(["1", len(s)-1, 0])
    one_count += 1
ans = -1
sum_ = -1
for i in range(one_count - k):
    a = 0
    if sum_ < 0:
        for e in s2[i*2:(i+k)*2+1]:
            a += e[2]
    else:
        sum_ = sum_ - s2[i-2][2] + s2[(i+k)*2+1][2]
    if ans < a:
        ans = a
print(ans)

Fixed submission:
n, k = map(int, input().split())
s = input()
cur = "1"
sum_ = [0]
ans = -1
for i, c in enumerate(s):
    if cur != c:
        sum_.append(i)
        if c == "0":
            if len(sum_) >= 2 + k + k:
                ans = max(ans, sum_[-1] - sum_[-2-k-k])
        cur = c
sum_.append(len(s))
if s[-1] == "0":
    sum_.append(len(s))
if len(sum_) > 2 + k + k:
    ans = max(ans, sum_[-1] - sum_[-2-k-k])
else:
    ans = n
print(ans)

Buggy python submission (verdict: Time Limit Exceeded):
n, k = map(int, input().split())
s = input()
s2 = [["1", 0, 0]]
zero = 0
one_count = 1
for i, c in enumerate(s):
    if s2[-1][0] == c:
        s2[-1][2] += 1
    else:
        s2.append([c, i, 1])
        zero += (1 - int(c))
        if c == "1":
            one_count += 1
if zero <= k:
    print(n)
    exit()
if s2[-1][0] == "0":
    s2.append(["1", len(s)-1, 0])
    one_count += 1
ans = -1
for i in range(one_count - k):
    a = 0
    for e in s2[i*2:(i+k)*2+1]:
        a += e[2]
    if ans < a:
        ans = a
print(ans)

Fixed submission:
n, k = map(int, input().split())
s = input()
cur = "1"
sum_ = [0]
ans = -1
for i, c in enumerate(s):
    if cur != c:
        sum_.append(i)
        if c == "0":
            if len(sum_) >= 2 + k + k:
                ans = max(ans, sum_[-1] - sum_[-2-k-k])
        cur = c
sum_.append(len(s))
if s[-1] == "0":
    sum_.append(len(s))
if len(sum_) > 2 + k + k:
    ans = max(ans, sum_[-1] - sum_[-2-k-k])
else:
    ans = n
print(ans)

Buggy python submission (verdict: Time Limit Exceeded):
n, k = map(int, input().split())
s = input()
s2 = [["1", 0, 0]]
zero = 0
one_count = 1
for i, c in enumerate(s):
    if s2[-1][0] == c:
        s2[-1][2] += 1
    else:
        s2.append([c, i, 1])
        zero += (1 - int(c))
        if c == "1":
            one_count += 1
if zero <= k:
    print(n)
    exit()
if s2[-1][0] == "0":
    s2.append(["1", len(s)-1, 0])
    one_count += 1
ans = -1
for i in range(one_count - k):
    a = 0
    for e in s2[i*2:(i+k)*2+1]:
        a += e[2]
    if ans < a:
        ans = a
print(ans)

Fixed submission:
n, k = map(int, input().split())
s = input()
cur = "1"
sum_ = [0]
ans = -1
for i, c in enumerate(s):
    if cur != c:
        sum_.append(i)
        if c == "0":
            if len(sum_) >= 2 + k + k:
                ans = max(ans, sum_[-1] - sum_[-2-k-k])
        cur = c
sum_.append(len(s))
if s[-1] == "0":
    sum_.append(len(s))
if len(sum_) > 2 + k + k:
    ans = max(ans, sum_[-1] - sum_[-2-k-k])
else:
    ans = n
print(ans)