#include <bits/stdc++.h>
using namespace std;
long long a[100010], b[101010] = {0};
char s[520][520] = {0};
int t, n, m, X[9] = {0, 1, 1, 1, 0, 0, -1, -1, -1},
             Y[9] = {0, 1, 0, -1, 1, -1, 1, 0, -1};
int dfs(int x, int y, int &t) {
  int i;
  if (s[x][y] != '1') return 0;
  s[x][y] = '2';
  t++;
  for (i = 1; i <= 8; i++)
    if (x + X[i] >= 0 && y + Y[i] >= 0 && x + X[i] < n && y + Y[i] < m)
      dfs(x + X[i], y + Y[i], t);
  return 0;
}
int check1(int x, int y, int l) {
  int i;
  if (x + l >= n || y + l >= m) return 0;
  for (i = 1; i <= l; i++) {
    if (s[x][y + i] != '2') return 0;
    if (s[x + l][y + i] != '2') return 0;
    if (s[x + i][y] != '2') return 0;
    if (s[x + i][y + l] != '2') return 0;
  }
  return 1;
}
int check2(int x, int y, int l) {
  int i;
  if (y + l >= m || x + 2 * l >= n || y - l < 0 || s[x + 2 * l][y] != '2')
    return 0;
  for (i = 1; i <= l; i++) {
    if (s[x + i][y + i] != '2') return 0;
    if (s[x + i][y - i] != '2') return 0;
    if (s[x + 2 * l - i][y - i] != '2') return 0;
    if (s[x + 2 * l - i][y + i] != '2') return 0;
  }
  return 1;
}
int main() {
  int num[11] = {1, 2, 4, 8, 16, 32, 64, 128, 256, 512, 1024};
  int i, j, ii, y, k, p, q, x, fi, T;
  long long ans;
  const int pp = 1000000007;
  char s1[10000], s2[10000], c1;
  bool tt;
  scanf("%d", &T);
  for (ii = 1; ii <= T; ii++) {
    scanf("%d%d", &n, &m);
    ans = 0;
    for (i = 0; i <= n - 1; i++) scanf("%s", s[i]);
    for (i = 0; i <= n - 1; i++)
      for (j = 0; j <= m - 1; j++)
        if (s[i][j] == '1') {
          t = 0;
          dfs(i, j, t);
          if (t % 4 == 0 && t <= 4 * min(m, n)) {
            ans = ans + check1(i, j, t / 4);
            ans = ans + check2(i, j, t / 4);
          }
        }
    printf("%I64d\n", ans);
  }
}
