// Author: Shawn Das Shachin-->(shawn_das)
#include <bits/stdc++.h>
using namespace std;
using ll = long long;
using ld = long double;
#define pb push_back
#define mod 1000000007
#define srt(v) sort(v.begin(),v.end())
#define rsrt(v) sort(v.rbegin(),v.rend())
#define OPTIMIZE_IO ios::sync_with_stdio(false); cin.tie(nullptr);
void solve() {
int n, k;
cin >> n >> k;
string str;
cin >> str;
vector<int> counts;
int count = 0,tc=0;
for (char it : str) {
if (it == '1') {
count++;
tc++;
} else {
if (count > 0) {
counts.pb(count);
}
count = 0;
}
}
if (count > 0) {
counts.pb(count);
}
rsrt(counts);
if (counts.empty()) {
cout << 0 << endl;
return;
}
if (k == 0) {
cout << counts[0] << endl;
return;
}
int ans = counts[0];
for (int i = 1; i<=min(n,k); i++) {
ans += counts[i];
}
cout << min(tc,ans) << endl;
}
int main() {
OPTIMIZE_IO;
int t;
cin >> t;
while (t--) {
solve();
}
return 0;
}