Buggy python submission (verdict: Time Limit Exceeded):
import math
import copy
from copy import deepcopy
import sys
import fractions
# import numpy as np
from functools import reduce
# import statistics
import heapq
sys.setrecursionlimit(100001)

# input = sys.stdin.readline
# sys.setrecursionlimit(10**6)

# ===FUNCTION===

def getInputInt():
    inputNum = int(input())
    return inputNum


def getInputListInt():
    outputData = []
    inputData = input().split()
    outputData = [int(n) for n in inputData]

    return outputData


def getSomeInputInt(n):
    outputDataList = []
    for i in range(n):
        inputData = int(input())
        outputDataList.append(inputData)

    return outputDataList


def getSomeInputListInt(n):
    inputDataList = []
    outputDataList = []
    for i in range(n):
        inputData = input().split()
        inputDataList = [int(n) for n in inputData]
        outputDataList.append(inputDataList)

    return outputDataList


# ===CODE===

n, k = map(int,input().split())
d = str(input())
tmp = list(d)
data = [int(s) for s in tmp]

cnt = 0
ans_cnt = 0
ans = 0

cnt_0 = []
cnt_1 = []

cnt0 = 0
cnt1 = 0

cnt_1.append(0)
for i in range(n):
    if i == 0:
        tmp = 0
    elif data[i-1] == 1 and data[i] == 0:
        cnt_1.append(cnt1)
        cnt1 = 0
    elif data[i - 1] == 0 and data[i] == 1:
        cnt_0.append(cnt0)
        cnt0 = 0

    if data[i] == 0:
        cnt0 += 1
    else:
        cnt1 += 1

    if i == n-1:
        if cnt0 != 0:
            cnt_0.append(cnt0)
        if cnt1 != 0:
            cnt_1.append(cnt1)
cnt_1.append(0)

change = min(k, len(cnt_0))
if data[0] == 0:
    start_1 = 0
else:
    start_1 = 1

ans = 0
for i in range(len(cnt_0)):
    tmp_ans = 0
    for j in range(i, min(i+change, len(cnt_0)), 1):
       tmp_ans += cnt_0[j]+cnt_1[j+start_1]
    tmp_ans += cnt_1[j+start_1+1]
    ans = max(ans, tmp_ans)

if len(cnt_0) == 0:
    ans = max(ans, cnt_1[1])

print(ans)



Fixed submission:
import math
import copy
from copy import deepcopy
import sys
import fractions
# import numpy as np
from functools import reduce
# import statistics
import heapq
sys.setrecursionlimit(100001)

# input = sys.stdin.readline
# sys.setrecursionlimit(10**6)

# ===FUNCTION===

def getInputInt():
    inputNum = int(input())
    return inputNum


def getInputListInt():
    outputData = []
    inputData = input().split()
    outputData = [int(n) for n in inputData]

    return outputData


def getSomeInputInt(n):
    outputDataList = []
    for i in range(n):
        inputData = int(input())
        outputDataList.append(inputData)

    return outputDataList


def getSomeInputListInt(n):
    inputDataList = []
    outputDataList = []
    for i in range(n):
        inputData = input().split()
        inputDataList = [int(n) for n in inputData]
        outputDataList.append(inputDataList)

    return outputDataList


# ===CODE===

n, k = map(int, input().split())
d = str(input())
tmp = list(d)
data = [int(s) for s in tmp]

ans = 0

cnt_list = []

tmp = 1
cnt = 0
for i in range(n):
    if data[i] == tmp:
       cnt += 1
    else:
        cnt_list.append(cnt)
        cnt = 1
        tmp = 1 - tmp
if cnt != 0:
    cnt_list.append(cnt)

if len(cnt_list) % 2 == 0:
    cnt_list.append(0)

sum_list = [0]
sum_num = 0
for d in cnt_list:
    sum_num += d
    sum_list.append(sum_num)

tmp_ans = 0
for i in range(0, len(sum_list), 2):
    left = i
    right = min(i + 2*k+1, len(sum_list)-1)
    tmp_ans = sum_list[right] - sum_list[left]
    ans = max(ans, tmp_ans)

print(ans)

Buggy python submission (verdict: Time Limit Exceeded):
import math
import copy
from copy import deepcopy
import sys
import fractions
import numpy as np
from functools import reduce
# import statistics
import heapq
sys.setrecursionlimit(100001)

# input = sys.stdin.readline
# sys.setrecursionlimit(10**6)

# ===FUNCTION===

def getInputInt():
    inputNum = int(input())
    return inputNum


def getInputListInt():
    outputData = []
    inputData = input().split()
    outputData = [int(n) for n in inputData]

    return outputData


def getSomeInputInt(n):
    outputDataList = []
    for i in range(n):
        inputData = int(input())
        outputDataList.append(inputData)

    return outputDataList


def getSomeInputListInt(n):
    inputDataList = []
    outputDataList = []
    for i in range(n):
        inputData = input().split()
        inputDataList = [int(n) for n in inputData]
        outputDataList.append(inputDataList)

    return outputDataList


# ===CODE===

n, k = map(int,input().split())
d = str(input())
tmp = list(d)
data = [int(s) for s in tmp]

cnt = 0
ans_cnt = 0
ans = 0

cnt_0 = []
cnt_1 = []

cnt0 = 0
cnt1 = 0

cnt_1.append(0)
for i in range(n):
    if i == 0:
        tmp = 0
    elif data[i-1] == 1 and data[i] == 0:
        cnt_1.append(cnt1)
        cnt1 = 0
    elif data[i - 1] == 0 and data[i] == 1:
        cnt_0.append(cnt0)
        cnt0 = 0

    if data[i] == 0:
        cnt0 += 1
    else:
        cnt1 += 1

    if i == n-1:
        if cnt0 != 0:
            cnt_0.append(cnt0)
        if cnt1 != 0:
            cnt_1.append(cnt1)
cnt_1.append(0)

change = min(k, len(cnt_0))
if data[0] == 0:
    start_1 = 0
else:
    start_1 = 1

ans = 0
for i in range(len(cnt_0)):
    tmp_ans = 0
    for j in range(i, min(i+change, len(cnt_0)), 1):
       tmp_ans += cnt_0[j]+cnt_1[j+start_1]
    tmp_ans += cnt_1[j+start_1+1]
    ans = max(ans, tmp_ans)

if len(cnt_0) == 0:
    ans = max(ans, cnt_1[1])

print(ans)



Fixed submission:
import math
import copy
from copy import deepcopy
import sys
import fractions
# import numpy as np
from functools import reduce
# import statistics
import heapq
sys.setrecursionlimit(100001)

# input = sys.stdin.readline
# sys.setrecursionlimit(10**6)

# ===FUNCTION===

def getInputInt():
    inputNum = int(input())
    return inputNum


def getInputListInt():
    outputData = []
    inputData = input().split()
    outputData = [int(n) for n in inputData]

    return outputData


def getSomeInputInt(n):
    outputDataList = []
    for i in range(n):
        inputData = int(input())
        outputDataList.append(inputData)

    return outputDataList


def getSomeInputListInt(n):
    inputDataList = []
    outputDataList = []
    for i in range(n):
        inputData = input().split()
        inputDataList = [int(n) for n in inputData]
        outputDataList.append(inputDataList)

    return outputDataList


# ===CODE===

n, k = map(int, input().split())
d = str(input())
tmp = list(d)
data = [int(s) for s in tmp]

ans = 0

cnt_list = []

tmp = 1
cnt = 0
for i in range(n):
    if data[i] == tmp:
       cnt += 1
    else:
        cnt_list.append(cnt)
        cnt = 1
        tmp = 1 - tmp
if cnt != 0:
    cnt_list.append(cnt)

if len(cnt_list) % 2 == 0:
    cnt_list.append(0)

sum_list = [0]
sum_num = 0
for d in cnt_list:
    sum_num += d
    sum_list.append(sum_num)

tmp_ans = 0
for i in range(0, len(sum_list), 2):
    left = i
    right = min(i + 2*k+1, len(sum_list)-1)
    tmp_ans = sum_list[right] - sum_list[left]
    ans = max(ans, tmp_ans)

print(ans)