#include <bits/stdc++.h>
const int X[8] = {0, 1, 0, -1, 1, 1, -1, -1},
          Y[8] = {1, 0, -1, 0, 1, -1, 1, -1};
int n, m, s;
char a[300][300];
void judge(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 <= 7; i++) judge(x + X[i], y + Y[i], s);
}
bool solve(int x, int y, int d, int l, int r) {
  for (int i = 0; i <= d; i++)
    for (int j = l; j <= r - 1; 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() {
  int test;
  for (scanf("%d", &test); test--;) {
    scanf("%d%d", &n, &m);
    for (int i = 0; i <= n - 1; i++) scanf("%s", a[i]);
    int ans = 0;
    for (int i = 0; i <= n - 1; i++)
      for (int j = 0; j <= m - 1; j++)
        if (a[i][j] == '1') {
          judge(i, j, s = 0);
          if (s % 4 || s / 4 > 250) continue;
          ans += solve(i, j, s / 4, 0, 2) &&
                 solve(i + s / 4, j + s / 4, s / 4, 2, 4);
          ans += solve(i, j, s / 4, 4, 6) && solve(i + s / 2, j, s / 4, 6, 8);
        }
    printf("%d\n", ans);
  }
}
