#include <bits/stdc++.h>
using namespace std;
char s[1000005];
int main() {
  int T, n, m;
  scanf("%d", &T);
  while (T--) {
    scanf("%d%d", &n, &m);
    scanf("%s", s + 1);
    map<int, int> b;
    int c0 = 0, c1 = 0;
    for (int i = 1; i <= n; i++) {
      c0 += (s[i] == '0');
      c1 += (s[i] == '1');
      b[c0 - c1]++;
    }
    int d = c0 - c1;
    long long ans = 0;
    if (!d) {
      if (b[m])
        puts("-1");
      else
        printf("%d\n", !m);
    } else {
      for (auto f : b)
        if ((m - f.first) % d == 0) {
          int t = (m - f.first) / d;
          if (t >= 0) ans += f.second;
        }
      printf("%lld\n", !m + ans);
    }
  }
  return 0;
}
