#include <bits/stdc++.h>
using namespace std;
int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  cout.tie(NULL);
  long long int q, n, m;
  cin >> q;
  while (q--) {
    cin >> n >> m;
    string s;
    cin >> s;
    long long int a[n + 1], b[n + 1], zero = 0, one = 0;
    a[0] = 0;
    b[0] = 0;
    for (long long int i = 0; i < n; i++) {
      if (s[i] == '0') {
        zero++;
      } else {
        one++;
      }
      a[i + 1] = zero;
      b[i + 1] = one;
    }
    set<long long int> se;
    long long int f = 1;
    for (long long int i = 0; i < n; i++) {
      long long int a1 = m + i - 2 * a[i];
      long long int k = (zero - one);
      if (k == 0) {
        if (a1 == 0) f = 0;
      } else if (a1 % k == 0) {
        long long int a2 = a1 / k;
        long long int len = n * a2 + i;
        if (len >= 0) se.insert(len);
      }
    }
    if (f) {
      cout << se.size() << endl;
    } else {
      cout << -1 << endl;
    }
  }
}
