Buggy python submission (verdict: Time Limit Exceeded):
N, K = map(int, input().split())
S = str(input())

import re

def nchars(s, n):
    """文字列 s に、同じ文字が n 個以上連続している部分文字列を見つける
    """
    assert n > 0
    reg = re.compile("(.)\\1{%d,}" % (n - 1))  # カンマを取ると n 個ちょうどになる
    while True:
        m = reg.search(s)
        if not m:
            break
        yield m.group(0)
        s = s[m.end():]

s_list = list(nchars(S, 1))
s_list_num = [len(s) for s in s_list]

#print(s_list)
#print(s_list_num)

ans = 0
for i in range(len(s_list)):
    count = 0
    s_len = 0
    for j in range(i, len(s_list)):
        if s_list[j][0] == '0':
            count += 1
            if count > K:
                break
        s_len += s_list_num[j]
        
    ans = max(ans, s_len)

print(ans)
        
        

Fixed submission:
N, K = map(int, input().split())
S = str(input())

import re

def nchars(s, n):
    """文字列 s に、同じ文字が n 個以上連続している部分文字列を見つける
    """
    assert n > 0
    reg = re.compile("(.)\\1{%d,}" % (n - 1))  # カンマを取ると n 個ちょうどになる
    while True:
        m = reg.search(s)
        if not m:
            break
        yield m.group(0)
        s = s[m.end():]

s_list = list(nchars(S, 1))
s_list_num = [len(s) for s in s_list]
if '1' in s_list[0]:
    s_list_num.insert(0,0)
else:
    s_list_num.insert(0,0)
    s_list_num.insert(0,0)


s_list_sum = []
tmp_sum = 0
for x in s_list_num:
    tmp_sum += x
    s_list_sum.append(tmp_sum)

#print(s_list)
#print(s_list_num)
#print(s_list_sum)

ans = 0
for i in range(1, len(s_list_sum),2):

    l_sum = s_list_sum[i-1]
    r_sum = s_list_sum[min(len(s_list_sum)-1, i+K*2)]
    #print(l_sum, r_sum)
    ans = max(ans, r_sum - l_sum)

print(ans)

Buggy python submission (verdict: Time Limit Exceeded):
N, K = map(int, input().split())
S = str(input())

def inversion_command(l, r, s):
    s_l = s[:l]
    s_c = s[l:r]
    s_r = s[r:]
    
    s_c = s_c.replace('0', 'X')
    s_c = s_c.replace('1', '0')
    s_c = s_c.replace('X', '1')
    return s_l + s_c + s_r
    
#print(inversion_command(0, 3, S))

s_list = set([S])

for _ in range(K):
    tmp_s_list = set()
    for l in range(N + 1):
        for r in range(l, N + 1):
            for s in s_list:
                tmp_s_list.add(inversion_command(l, r, s))
    s_list = tmp_s_list

ans = 0
for s in s_list:
    s_split = s.split('0')
    s_len = [len(s) for s in s_split]
    s_max = max(s_len)
    #print(s_len)
    #print(s_max)
    ans = max(ans, s_max)
print(ans)
    





Fixed submission:
N, K = map(int, input().split())
S = str(input())

import re

def nchars(s, n):
    """文字列 s に、同じ文字が n 個以上連続している部分文字列を見つける
    """
    assert n > 0
    reg = re.compile("(.)\\1{%d,}" % (n - 1))  # カンマを取ると n 個ちょうどになる
    while True:
        m = reg.search(s)
        if not m:
            break
        yield m.group(0)
        s = s[m.end():]

s_list = list(nchars(S, 1))
s_list_num = [len(s) for s in s_list]
if '1' in s_list[0]:
    s_list_num.insert(0,0)
else:
    s_list_num.insert(0,0)
    s_list_num.insert(0,0)


s_list_sum = []
tmp_sum = 0
for x in s_list_num:
    tmp_sum += x
    s_list_sum.append(tmp_sum)

#print(s_list)
#print(s_list_num)
#print(s_list_sum)

ans = 0
for i in range(1, len(s_list_sum),2):

    l_sum = s_list_sum[i-1]
    r_sum = s_list_sum[min(len(s_list_sum)-1, i+K*2)]
    #print(l_sum, r_sum)
    ans = max(ans, r_sum - l_sum)

print(ans)