#include <bits/stdc++.h>
char a[300][300];
int n, m, t, z, s;
const int X[8] = {0, 1, 0, -1, 1, 1, -1, -1},
          Y[8] = {1, 0, -1, 0, 1, -1, 1, -1};
void F(int x, int y, int &s) {
  if (x < 0 || x >= n || y < 0 || y >= m || a[x][y] != '1') return;
  a[x][y] = '2', s++;
  for (int i = 0; i < 8; i++) F(x + X[i], y + Y[i], s);
}
int C(int x, int y, int d, int l, int r) {
  for (int i = 0; i < d + 1; i++)
    for (int j = l; j < r; j++)
      if (x + X[j] * i < 0 || x + X[j] * i >= n || y + Y[j] * i < 0 ||
          y + Y[j] * i >= m || a[x + X[j] * i][y + Y[j] * i] != '2')
        return 0;
  return 1;
}
int main() {
  for (scanf("%d", &t); t; t--) {
    scanf("%d%d", &n, &m), z = 0;
    for (int i = 0; i < n; i++) scanf("%s", a[i]);
    for (int i = 0; i < n; i++)
      for (int j = 0; j < m; j++)
        if (a[i][j] == '1') {
          F(i, j, s = 0);
          if (s % 4 || s / 4 > 250) continue;
          z += C(i, j, s / 4, 0, 2) && C(i + s / 4, j + s / 4, s / 4, 2, 4);
          z += C(i, j, s / 4, 4, 6) && C(i + s / 2, j, s / 4, 6, 8);
        }
    printf("%d\n", z);
  }
  return 0;
}
