#include <bits/stdc++.h>
using namespace std;
const int N = 300, dr[8] = {0, +1, +1, +1, 0, -1, -1, -1},
          dc[8] = {+1, +1, 0, -1, -1, -1, 0, +1};
int a[N][N], num[N][N], low[N][N], cnt, tc, n, m;
bool inside(int r, int c) { return (r >= 1 && r <= n && c >= 1 && c <= m); }
pair<int, int> visit(int r, int c, queue<pair<int, int> > &Q, int rpa, int cpa,
                     int kpa) {
  Q.push(pair<int, int>(r, c));
  low[r][c] = num[r][c] = ++cnt;
  int child = 0;
  pair<int, int> ans(0, 0);
  for (int k = kpa; k < 8; k++) {
    int nr = r + dr[k], nc = c + dc[k];
    if (nr == rpa && nc == cpa) continue;
    if (inside(nr, nc) && num[nr][nc] == 0 && a[nr][nc] == 1) {
      child++;
      pair<int, int> tmp = visit(nr, nc, Q, r, c, k);
      low[r][c] = min(low[r][c], low[nr][nc]);
      ans.first = max(ans.first, tmp.first);
      ans.second += tmp.second;
    } else if (inside(nr, nc) && a[nr][nc] == 1 && num[nr][nc] != 0) {
      if (low[r][c] > num[nr][nc]) {
        ans.second++;
        low[r][c] = num[nr][nc];
      }
    }
  }
  for (int k = 0; k < 8; k++) {
    int nr = r + dr[k], nc = c + dc[k];
    if (inside(nr, nc) && a[nr][nc] && num[nr][nc] == 0) {
      child++;
      pair<int, int> tmp = visit(nr, nc, Q, r, c, 0);
      ans.first = max(ans.first, tmp.first);
      ans.second += tmp.second;
    }
  }
  return pair<int, int>(max(ans.first, child), ans.second);
}
bool Loai1(vector<pair<int, int> > &diem, int canh) {
  int i = 0;
  pair<int, int> z = diem[0];
  for (int k = 0; k < 8; k += 2) {
    for (int j = 1; j <= canh; j++) {
      i = (i + 1) % (4 * canh);
      z.first += dr[k];
      z.second += dc[k];
      if (z != diem[i]) return 0;
    }
  }
  return 1;
}
bool Loai2(vector<pair<int, int> > &diem, int canh) {
  int i = 0;
  pair<int, int> z = diem[0];
  for (int k = 1; k < 8; k += 2) {
    for (int j = 1; j <= canh; j++) {
      i = (i + 1) % (4 * canh);
      z.first += dr[k];
      z.second += dc[k];
      if (z != diem[i]) return 0;
    }
  }
  return 1;
}
bool HinhVuong(queue<pair<int, int> > &Q) {
  vector<pair<int, int> > diem;
  while (!Q.empty()) {
    diem.push_back(Q.front());
    Q.pop();
  }
  int ndiem = diem.size();
  if (ndiem % 4 != 0) return 0;
  int canh = ndiem / 4;
  if (Loai1(diem, canh) || Loai2(diem, canh)) return 1;
  return 0;
}
int main() {
  scanf("%d\n", &tc);
  while (tc--) {
    scanf("%d %d\n", &n, &m);
    for (int i = 1; i <= n; i++) {
      for (int j = 1; j <= m; j++) scanf("%1d", &a[i][j]);
      scanf("\n");
    }
    for (int i = 1; i <= n; i++) {
      fill(num[i] + 1, num[i] + m + 1, 0);
      fill(low[i] + 1, low[i] + m + 1, 0);
    }
    queue<pair<int, int> > Q;
    int ans = 0;
    for (int i = 1; i <= n; i++)
      for (int j = 1; j <= m; j++)
        if (num[i][j] == 0 && a[i][j]) {
          pair<int, int> tmp = visit(i, j, Q, i, j, 0);
          if (tmp.second > 1 || tmp.first > 1) {
            while (!Q.empty()) Q.pop();
          } else {
            if (HinhVuong(Q)) ans++;
          }
        }
    printf("%d\n", ans);
    fflush(stdout);
  }
  return 0;
}
