Buggy python submission (verdict: Time Limit Exceeded):
# import sys
# input = sys.stdin.readline

_, k = map(int, input().split())
s = list(map(int, list(input())))
# print(s)
is_count_zero = True
s0 = [0]
s1 = []
s0_ptr = 0
s1_ptr = -1
for c in s:
    if is_count_zero and c == 0:
        s0[s0_ptr] += 1
    elif not is_count_zero and c == 0:
        s0.append(1)
        s0_ptr += 1
        is_count_zero = True
    elif is_count_zero and c == 1:
        s1.append(1)
        s1_ptr += 1
        is_count_zero = False
    else: # not is_count_zero and c == 1
        s1[s1_ptr] += 1

# print(s0,s1)
# is_end_one = True if len(s0) == len(s1) else False

if k >= len(s0):
    print(sum(s0)+sum(s1))
elif k == 0:
    if s1 == []:
        print(0)
    else:
        print(max(s1))
else:
    ans = 0
    n = len(s0)-k+1
    for i in range(n):
        if i == 0 and s0[0] > 0:
            ans = sum(s0[:k]) + sum(s1[:k])
        elif i == n-1:
            ans = max(ans, sum(s0[i:]) + sum(s1[i-1:]))
        elif i != 0:
            ans = max(ans, sum(s0[i:i+k]) + sum(s1[i-1:i+k]) )
        # print(ans)
    print(ans)


Fixed submission:
# import sys
# input = sys.stdin.readline

_, k = map(int, input().split())
s = list(map(int, list(input())))
# print(s)
is_count_zero = True
s0 = [0]
s1 = []
s0_ptr = 0
s1_ptr = -1
for c in s:
    if is_count_zero and c == 0:
        s0[s0_ptr] += 1
    elif not is_count_zero and c == 0:
        s0.append(1)
        s0_ptr += 1
        is_count_zero = True
    elif is_count_zero and c == 1:
        s1.append(1)
        s1_ptr += 1
        is_count_zero = False
    else: # not is_count_zero and c == 1
        s1[s1_ptr] += 1

# print(s0,s1)
is_end_one = True if len(s0) == len(s1) else False

if k >= len(s0):
    print(sum(s0)+sum(s1))
# elif k == 0:
#     if s1 == []:
#         print(0)
#     else:
#         print(max(s1))
else:
    ans = 0
    sum_range = 0
    n = len(s0)-k+1
    for i in range(n):
        if i == 0:
            sum_range = sum(s0[:k]) + sum(s1[:k])
            ans = max(ans, sum_range)
        elif i == n-1:
            sum_range = sum(s0[i:]) + sum(s1[i-1:])
            ans = max(ans, sum_range)
        elif i == 1:
            sum_range = sum_range - s0[0] + s0[k] + s1[k]
            ans = max(ans, sum_range)
        elif i != 0:
            sum_range = sum_range - s0[i-1] + s0[i+k-1] - s1[i-2] + s1[i+k-1]
            ans = max(ans, sum_range)
    print(ans)

Buggy python submission (verdict: Runtime Error):
s = list(input())
is_count_zero = True
sl = [0]
sl_ptr = 0
for c in s:
    if is_count_zero and c == '1':
        sl.append(1)
        sl_ptr += 1
        is_count_zero = False
    elif not is_count_zero and c == '0':
        sl.append(1)
        sl_ptr += 1
        is_count_zero = True
    else:
        sl[sl_ptr] += 1

# print(sl)
# print("len(sl):{} len(sl)-1)//2:{}".format(len(sl), (len(sl)-1)//2))

if k > (len(sl)-1)//2:
    print(sum(sl))
elif k == (len(sl)-1)//2:
    if sl[0] == 0:
        print(sum(sl))
    else:
        print(max(sum(sl[1:]), sum(sl[:-1])))
else:
    ans = 0
    for i in range((len(sl)+1)//2-(k-1)):
        if i == 0:
            ans = max(ans, sum(sl[:k*2-1]))
            # print("sl[:k*2-1]:{} sum:{}".format(sl[:k*2-1], sum(sl[:k*2-1])))
        else:
            ans = max(ans, sum(sl[i*2-1:(i+k)*2]))
            # print("sl[i*2-1:(i+k)*2-1]: {}".format(sl[i*2-1:(i+k)*2]))
    print(ans)


Fixed submission:
# import sys
# input = sys.stdin.readline

_, k = map(int, input().split())
s = list(map(int, list(input())))
# print(s)
is_count_zero = True
s0 = [0]
s1 = []
s0_ptr = 0
s1_ptr = -1
for c in s:
    if is_count_zero and c == 0:
        s0[s0_ptr] += 1
    elif not is_count_zero and c == 0:
        s0.append(1)
        s0_ptr += 1
        is_count_zero = True
    elif is_count_zero and c == 1:
        s1.append(1)
        s1_ptr += 1
        is_count_zero = False
    else: # not is_count_zero and c == 1
        s1[s1_ptr] += 1

# print(s0,s1)
is_end_one = True if len(s0) == len(s1) else False

if k >= len(s0):
    print(sum(s0)+sum(s1))
# elif k == 0:
#     if s1 == []:
#         print(0)
#     else:
#         print(max(s1))
else:
    ans = 0
    sum_range = 0
    n = len(s0)-k+1
    for i in range(n):
        if i == 0:
            sum_range = sum(s0[:k]) + sum(s1[:k])
            ans = max(ans, sum_range)
        elif i == n-1:
            sum_range = sum(s0[i:]) + sum(s1[i-1:])
            ans = max(ans, sum_range)
        elif i == 1:
            sum_range = sum_range - s0[0] + s0[k] + s1[k]
            ans = max(ans, sum_range)
        elif i != 0:
            sum_range = sum_range - s0[i-1] + s0[i+k-1] - s1[i-2] + s1[i+k-1]
            ans = max(ans, sum_range)
    print(ans)