#include <bits/stdc++.h>
int x, y;
char in[300][300];
int cnt;
void nul(int a, int b, char from, char to) {
  int i, j;
  if (a < 0 || b < 0 || a >= x || b >= y || in[a][b] != from) return;
  cnt++;
  in[a][b] = to;
  for (i = -1; i < 2; i++)
    for (j = -1; j < 2; j++) nul(a + i, b + j, from, to);
}
int is_ok(int i, int j) {
  if (i < 0 || j < 0 || i >= x || j >= y || in[i][j] != '2') return 0;
  return 1;
}
int main() {
  int i, j, k, l, m, n, len, ok, res;
  int size;
  scanf("%d", &size);
  while (size--) {
    scanf("%d%d", &x, &y);
    for (i = 0; i < x; i++) scanf("%s", in[i]);
    res = 0;
    for (i = 0; i < x; i++)
      for (j = 0; j < y; j++)
        if (in[i][j] == '1') {
          cnt = 0;
          nul(i, j, '1', '2');
          if (cnt % 4 == 0) {
            len = cnt / 4 + 1;
            ok = 1;
            for (k = 0; k < len; k++)
              if (!is_ok(i + k, j)) ok = 0;
            for (k = 0; k < len; k++)
              if (!is_ok(i, j + k)) ok = 0;
            for (k = 0; k < len; k++)
              if (!is_ok(i + len - 1, j + k)) ok = 0;
            for (k = 0; k < len; k++)
              if (!is_ok(i + k, j + len - 1)) ok = 0;
            res += ok;
            ok = 1;
            for (k = 0; k < len; k++)
              if (!is_ok(i + k, j + k)) ok = 0;
            for (k = 0; k < len; k++)
              if (!is_ok(i + k, j - k)) ok = 0;
            for (k = 0; k < len; k++)
              if (!is_ok(i + 2 * len - 2 - k, j + k)) ok = 0;
            for (k = 0; k < len; k++)
              if (!is_ok(i + 2 * len - 2 - k, j - k)) ok = 0;
            res += ok;
          }
          nul(i, j, '2', '0');
        }
    printf("%d\n", res);
  }
  return 0;
}
