#include <bits/stdc++.h>
using namespace std;
const int MaxN = 252, MaxC = 0x3F3F3F3F, NA = -1, MaxD = 8;
const int dx[MaxD] = {-1, -1, -1, 0, 1, 1, 1, 0},
          dy[MaxD] = {-1, 0, 1, 1, 1, 0, -1, -1};
char a[MaxN][MaxN];
int m, n, s;
void mark(int x, int y, int p, int q) {
  int d;
  if (a[x][y] != p) return;
  a[x][y] = q;
  s++;
  for (d = 0; d < MaxD; d++) mark(x + dx[d], y + dy[d], p, q);
}
int main(void) {
  int test, tests;
  int i, j, k, l, res;
  scanf(" %d", &tests);
  for (test = 0; test < tests; test++) {
    scanf(" %d %d", &n, &m);
    for (i = 0; i <= n + 1; i++)
      for (j = 0; j <= m + 1; j++) a[i][j] = '0';
    for (i = 1; i <= n; i++) scanf(" %s", a[i] + 1);
    res = 0;
    for (i = 1; i <= n; i++)
      for (j = 1; j <= m; j++)
        if (a[i][j] == '1') {
          s = 0;
          mark(i, j, '1', '2');
          if (a[i + 1][j] == '2') {
            for (k = 1;; k++)
              if (a[i + k][j] != '2') break;
            l = k - 1;
            if (s != l * 4) continue;
            for (k = 1;; k++)
              if (a[i][j + k] != '2') break;
            if (l != k - 1) continue;
            for (k = 1;; k++)
              if (a[i + l][j + k] != '2') break;
            if (l != k - 1) continue;
            for (k = 1;; k++)
              if (a[i + k][j + l] != '2') break;
            if (l != k - 1) continue;
            res++;
          } else if (a[i + 1][j + 1] == '2') {
            for (k = 1;; k++)
              if (a[i + k][j + k] != '2') break;
            l = k - 1;
            if (s != l * 4) continue;
            for (k = 1;; k++)
              if (a[i + k][j - k] != '2') break;
            if (l != k - 1) continue;
            for (k = 1;; k++)
              if (a[i + l + k][j - l + k] != '2') break;
            if (l != k - 1) continue;
            for (k = 1;; k++)
              if (a[i + l + k][j + l - k] != '2') break;
            if (l != k - 1) continue;
            res++;
          }
          mark(i, j, '2', '3');
        }
    printf("%d\n", res);
  }
  return 0;
}
