Buggy python submission (verdict: Time Limit Exceeded):
n,k=map(int,input().split())
S = input()
binary = []
one = []
zero = []
tmp_z = 0
tmp_o = 0
zeroflag = (S[0]=="0")
saigozero = (S[-1]=="0")
for i,s in enumerate(S):
    if s=="0":
        tmp_z += 1
        if not zeroflag:
            one.append(tmp_o)
            tmp_o=0
            zeroflag=True
    if s =="1":
        tmp_o+=1
        if zeroflag:
            zero.append(tmp_z)
            tmp_z=0
            zeroflag=False
if tmp_z!=0:
    zero.append(tmp_z)
if tmp_o!=0:
    one.append(tmp_o)

zeroflag = (S[0]=="0")
saigozero = (S[-1]=="0")

if zeroflag:
    one = [1] + one
if saigozero:
    one.append(1)

if len(zero)<=k:
    print(len(S))
else:
    ans = 0
    
    for i in range(len(zero)-k + 1):
        if zeroflag and i==0:
            tmp = sum(zero[i:i+k])+sum(one[i:i+k+1]) -1
        elif saigozero and i==len(zero)-k:
            tmp = sum(zero[i:i+k])+sum(one[i:i+k+1]) -1
        else:
            tmp = sum(zero[i:i+k])+sum(one[i:i+k+1])
        ans = max(ans,tmp)
    print(ans)

Fixed submission:
n,k=map(int,input().split())
S = input()
binary = []
one = []
zero = []
tmp_z = 0
tmp_o = 0
zeroflag = (S[0]=="0")
saigozero = (S[-1]=="0")
for i,s in enumerate(S):
    if s=="0":
        tmp_z += 1
        if not zeroflag:
            one.append(tmp_o)
            tmp_o=0
            zeroflag=True
    if s =="1":
        tmp_o+=1
        if zeroflag:
            zero.append(tmp_z)
            tmp_z=0
            zeroflag=False
if tmp_z!=0:
    zero.append(tmp_z)
if tmp_o!=0:
    one.append(tmp_o)

zeroflag = (S[0]=="0")
saigozero = (S[-1]=="0")

if zeroflag:
    one = [1] + one
if saigozero:
    one.append(1)
    
one_sum = [0]*(len(one)+1)
zero_sum = [0]*(len(zero)+1)
for i in range(len(one)):
    one_sum[i+1] = one_sum[i]+one[i]
for i in range(len(zero)):
    zero_sum[i+1]=zero_sum[i]+zero[i]
    


if len(zero)<=k:
    print(len(S))
else:
    ans = 0
    
    for i in range(len(zero)-k + 1):
        if zeroflag and i==0:
#             tmp = sum(zero[i:i+k])+sum(one[i:i+k+1]) -1
            tmp = zero_sum[i+k]-zero_sum[i]+one_sum[i+k+1]-one_sum[i]-1
        elif saigozero and i==len(zero)-k:
            tmp = zero_sum[i+k]-zero_sum[i]+one_sum[i+k+1]-one_sum[i]-1
        else:
            tmp = zero_sum[i+k]-zero_sum[i]+one_sum[i+k+1]-one_sum[i]
        ans = max(ans,tmp)
    print(ans)

Buggy python submission (verdict: Time Limit Exceeded):
n,k=map(int,input().split())
S = input()
binary = []
one = []
zero = []
tmp_z = 0
tmp_o = 0
zeroflag = (S[0]=="0")
saigozero = (S[-1]=="0")
for i,s in enumerate(S):
    if s=="0":
        tmp_z += 1
        if not zeroflag:
            one.append(tmp_o)
            tmp_o=0
            zeroflag=True
    if s =="1":
        tmp_o+=1
        if zeroflag:
            zero.append(tmp_z)
            tmp_z=0
            zeroflag=False
if tmp_z!=0:
    zero.append(tmp_z)
if tmp_o!=0:
    one.append(tmp_o)

zeroflag = (S[0]=="0")
saigozero = (S[-1]=="0")

if zeroflag:
    one = [1] + one
if saigozero:
    one.append(1)

if len(zero)<=k:
    print(len(S))
else:
    ans = 0
    
    for i in range(len(zero)-k + 1):
        if zeroflag and i==0:
            tmp = sum(zero[i:i+k])+sum(one[i:i+k+1]) -1
        elif saigozero and i==len(zero)-k:
            tmp = sum(zero[i:i+k])+sum(one[i:i+k+1]) -1
        else:
            tmp = sum(zero[i:i+k])+sum(one[i:i+k+1])
        ans = max(ans,tmp)
    print(ans)

Fixed submission:
n,k=map(int,input().split())
S = input()
binary = []
one = []
zero = []
tmp_z = 0
tmp_o = 0
zeroflag = (S[0]=="0")
saigozero = (S[-1]=="0")
for i,s in enumerate(S):
    if s=="0":
        tmp_z += 1
        if not zeroflag:
            one.append(tmp_o)
            tmp_o=0
            zeroflag=True
    if s =="1":
        tmp_o+=1
        if zeroflag:
            zero.append(tmp_z)
            tmp_z=0
            zeroflag=False
if tmp_z!=0:
    zero.append(tmp_z)
if tmp_o!=0:
    one.append(tmp_o)

zeroflag = (S[0]=="0")
saigozero = (S[-1]=="0")

if zeroflag:
    one = [1] + one
if saigozero:
    one.append(1)
    
one_sum = [0]*(len(one)+1)
zero_sum = [0]*(len(zero)+1)
for i in range(len(one)):
    one_sum[i+1] = one_sum[i]+one[i]
for i in range(len(zero)):
    zero_sum[i+1]=zero_sum[i]+zero[i]
    


if len(zero)<=k:
    print(len(S))
else:
    ans = 0
    
    for i in range(len(zero)-k + 1):
        if zeroflag and i==0:
#             tmp = sum(zero[i:i+k])+sum(one[i:i+k+1]) -1
            tmp = zero_sum[i+k]-zero_sum[i]+one_sum[i+k+1]-one_sum[i]-1
        elif saigozero and i==len(zero)-k:
            tmp = zero_sum[i+k]-zero_sum[i]+one_sum[i+k+1]-one_sum[i]-1
        else:
            tmp = zero_sum[i+k]-zero_sum[i]+one_sum[i+k+1]-one_sum[i]
        ans = max(ans,tmp)
    print(ans)