#include <bits/stdc++.h>
using namespace std;
int main() {
  int t;
  cin >> t;
  while (t--) {
    long long int n, x;
    cin >> n >> x;
    long long int a[n];
    a[0] = 0;
    string s;
    cin >> s;
    for (int i = 0; i < n; i++) {
      char ch;
      ch = s[i];
      if (i == 0) {
        if (ch == '1')
          a[i]--;
        else
          a[i]++;
      } else {
        if (ch == '1')
          a[i] = a[i - 1] - 1;
        else
          a[i] = a[i - 1] + 1;
      }
    }
    long long int as = 0;
    for (int i = 0; i < n; i++) {
      long long int k = (x - a[i]) * a[n - 1];
      if (k >= 0) {
        if (abs(x - a[i]) == 0 ||
            (a[n - 1] != 0 && abs(x - a[i]) % abs(a[n - 1]) == 0)) {
          as++;
        }
      }
    }
    long long int count = 0;
    for (int i = 0; i < n; i++) {
      if (a[i] == x) {
        count++;
      }
    }
    as = max(as, count);
    if (as != 0 && a[n - 1] == 0) {
      cout << -1 << "\n";
    } else {
      if (x == 0) as++;
      cout << as << "\n";
    }
  }
}
