Buggy python submission (verdict: Time Limit Exceeded):

N,K = map(int,input().split())
S = input()

#N = 10 ** 5
#K = 3
#S = "01" * ((10 ** 5) // 2 )

lis = []

nowchar = -1
num = 0

for i in range(N):

        if nowchar == S[i]:

            num += 1

        elif nowchar != -1:

            lis.append([nowchar,num])

            nowchar = S[i]
            num = 1

        else:

            nowchar = S[i]
            num = 1

lis.append([nowchar,num])


nlis = []
zeronum = 0
summ = 0
ans = 0


while True:

    if zeronum <= K:

        ans = max(ans,summ)

        if len(lis) == 0:
            break

        new = lis.pop(0)
        nlis.append(new)

        if new[0] == "0":
            zeronum += 1

        summ += new[1]

    else:

        old = nlis.pop(0)

        if old[0] == "0":
            zeronum -= 1

        summ -= old[1]

print (ans)
        
    


Fixed submission:

N,K = map(int,input().split())
S = input()

#N = 10 ** 5
#K = 3
#S = "01" * ((10 ** 5) // 2 )

lis = []

nowchar = -1
num = 0

for i in range(N):

        if nowchar == S[i]:

            num += 1

        elif nowchar != -1:

            lis.append([nowchar,num])

            nowchar = S[i]
            num = 1

        else:

            nowchar = S[i]
            num = 1

lis.append([nowchar,num])


nlis = []
lisst = 0
lisla = -1
zeronum = 0
summ = 0
ans = 0


while True:

    if zeronum <= K:

        ans = max(ans,summ)

        if lisla == len(lis)-1:
            break

        new = lis[lisla+1]
        lisla += 1
        #nlis.append(new)

        if new[0] == "0":
            zeronum += 1

        summ += new[1]

    else:

        old = lis[lisst]
        lisst += 1

        if old[0] == "0":
            zeronum -= 1

        summ -= old[1]

print (ans)

Buggy python submission (verdict: Time Limit Exceeded):

N,K = map(int,input().split())
S = input()

lis = []

nowchar = -1
num = 0

for i in range(N):

        if nowchar == S[i]:

            num += 1

        elif nowchar != -1:

            lis.append([nowchar,num])

            nowchar = S[i]
            num = 1

        else:

            nowchar = S[i]
            num = 1

lis.append([nowchar,num])


nlis = []
zeronum = 0
summ = 0
ans = 0


while True:

    if zeronum <= K:

        ans = max(ans,summ)

        if len(lis) == 0:
            break

        new = lis.pop(0)
        nlis.append(new)

        if new[0] == "0":
            zeronum += 1

        summ += new[1]

    else:

        old = nlis.pop(0)

        if old[0] == "0":
            zeronum -= 1

        summ -= old[1]

print (ans)
        
    


Fixed submission:

N,K = map(int,input().split())
S = input()

#N = 10 ** 5
#K = 3
#S = "01" * ((10 ** 5) // 2 )

lis = []

nowchar = -1
num = 0

for i in range(N):

        if nowchar == S[i]:

            num += 1

        elif nowchar != -1:

            lis.append([nowchar,num])

            nowchar = S[i]
            num = 1

        else:

            nowchar = S[i]
            num = 1

lis.append([nowchar,num])


nlis = []
lisst = 0
lisla = -1
zeronum = 0
summ = 0
ans = 0


while True:

    if zeronum <= K:

        ans = max(ans,summ)

        if lisla == len(lis)-1:
            break

        new = lis[lisla+1]
        lisla += 1
        #nlis.append(new)

        if new[0] == "0":
            zeronum += 1

        summ += new[1]

    else:

        old = lis[lisst]
        lisst += 1

        if old[0] == "0":
            zeronum -= 1

        summ -= old[1]

print (ans)