#include<bits/stdc++.h>
#define ll long long
#define endl '\n'
using namespace std;
int32_t main() {
ios_base :: sync_with_stdio(0);
cin.tie(0); cout.tie(0);
int t;
cin >> t;
while (t--) {
ll n, k;
cin >> n >> k;
string s;
cin >> s;
int conseq_one = 0, one = 0, ans_conseq = 0;
/* for (int i = 0; i < n; i++) {
if (s[i] == '1') one++;
}
*/
for (int i = 0; i < n; i++) {
conseq_one = 0;
while (s[i] == '1' && i < n) {
conseq_one++;
one++;
i++;
}
ans_conseq = max(ans_conseq, conseq_one);
}
int non_conseq = abs(one - ans_conseq);
if (non_conseq <= k) cout << non_conseq + ans_conseq << endl;
else {
if (non_conseq >= k) cout << ans_conseq + k << endl;
else cout << ans_conseq + non_conseq << endl;
}
}
return 0;
}