#include <bits/stdc++.h>
using namespace std;
char in[255][255];
int X, Y;
int try1(int y, int x) {
  int length = 0;
  for (length = 1;; length++) {
    if (y + length - 1 >= Y || x + length - 1 >= X ||
        in[y + length - 1][x] != '1')
      break;
  }
  length--;
  bool OK = 1;
  for (int yy = y; yy <= y + length - 1 && OK; yy++) {
    if (in[yy][x] != '1' || in[yy][x + length - 1] != '1') OK = 0;
  }
  for (int xx = x; xx <= x + length - 1 && OK; xx++)
    if (in[y][xx] != '1' || in[y + length - 1][xx] != '1') OK = 0;
  if (OK)
    return 4 * length - 4;
  else
    return -1;
}
int try2(int y, int x) {
  int length = 0;
  for (length = 1;; length++)
    if (y + length - 1 >= Y || x + length - 1 >= X ||
        in[y + length - 1][x + length - 1] != '1')
      break;
  length--;
  bool OK = 1;
  int x1 = x, x2 = x;
  for (int yy = y; yy <= y + length - 1 && OK; yy++) {
    if (x1 < 0 || x1 >= X || x2 < 0 || x2 >= X) {
      OK = 0;
      break;
    }
    if (in[yy][x1] != '1' || in[yy][x2] != '1') OK = 0;
    x1--;
    x2++;
  }
  x1++;
  x2--;
  for (int yy = y + length; yy < y + length * 2 - 1 && OK; yy++) {
    x1++;
    x2--;
    if (x1 < 0 || x1 >= X || x2 < 0 || x2 >= X) {
      OK = 0;
      break;
    }
    if (in[yy][x1] != '1' || in[yy][x2] != '1') OK = 0;
  }
  int rrr = 4 * length - 4;
  ;
  if (OK)
    return rrr;
  else
    return -1;
}
int cleanup(int y, int x) {
  queue<pair<int, int> > qu;
  qu.push(make_pair(y, x));
  pair<int, int> now;
  int ret = 0;
  while (!qu.empty()) {
    ret++;
    now = qu.front();
    qu.pop();
    in[now.first][now.second] = '0';
    for (int i1 = -1; i1 <= 1; i1++)
      for (int i2 = -1; i2 <= 1; i2++) {
        int xx = now.second + i1;
        int yy = now.first + i2;
        if (xx >= 0 && xx < X && yy >= 0 && yy < Y && in[yy][xx] == '1') {
          in[yy][xx] = '0';
          qu.push(make_pair(yy, xx));
        }
      }
  }
  return ret;
}
int k;
int main() {
  scanf("%d", &k);
  for (int cas = 1; cas <= k; cas++) {
    scanf("%d%d", &Y, &X);
    int res = 0;
    for (int i = 0; i < Y; i++)
      for (int j = 0; j < X; j++) {
        scanf(" %c ", &in[i][j]);
      }
    for (int i = 0; i < Y; i++)
      for (int j = 0; j < X; j++)
        if (in[i][j] == '1') {
          int aa1 = try1(i, j);
          int aa2 = try2(i, j);
          int rr = cleanup(i, j);
          if (rr == aa1 || rr == aa2) res++;
        }
    cout << res << endl;
  }
  return 0;
}
