#include <bits/stdc++.h>
using namespace std;
using ll = long long;
template <typename T>
inline bool chmax(T& a, T b) {
  if (a < b) {
    a = b;
    return true;
  }
  return 0;
}
template <typename T>
inline bool chmin(T& a, T b) {
  if (a > b) {
    a = b;
    return true;
  }
  return 0;
}
void solve() {
  int N, X;
  string S;
  cin >> N >> X >> S;
  int cnt0 = 0, cnt1 = 0;
  for (int i = (int)0; i < (int)N; ++i) {
    if (S[i] == '0') {
      ++cnt0;
    } else {
      ++cnt1;
    }
  }
  if (cnt0 == cnt1) {
    cnt0 = cnt1 = 0;
    for (int i = (int)0; i < (int)N; ++i) {
      if (S[i] == '0') {
        ++cnt0;
      } else {
        ++cnt1;
      }
      if (cnt0 - cnt1 == X) {
        cout << -1 << endl;
        return;
      }
    }
    cout << 0 << endl;
  } else {
    if (cnt0 < cnt1) {
      X *= -1;
      for (int i = (int)0; i < (int)N; ++i) {
        if (S[i] == '0') {
          S[i] = '1';
        } else {
          S[i] = '0';
        }
      }
    }
    priority_queue<int> que;
    cnt0 = cnt1 = 0;
    for (int i = (int)0; i < (int)N; ++i) {
      if (S[i] == '0') {
        ++cnt0;
      } else {
        ++cnt1;
      }
      que.push(cnt0 - cnt1);
    }
    int diff = cnt0 - cnt1;
    int ans = 0;
    if (X == 0) {
      ++ans;
    }
    while (!que.empty()) {
      while (que.size() && que.top() >= X) {
        if (que.top() == X) {
          ++ans;
        }
        que.pop();
      }
      X -= ((X - que.top() + diff - 1) / diff) * diff;
    }
    cout << ans << endl;
  }
}
int main() {
  cin.tie(0);
  ios::sync_with_stdio(false);
  int T;
  cin >> T;
  for (int i = (int)0; i < (int)T; ++i) {
    solve();
  }
  return 0;
}
