#include <bits/stdc++.h>
#pragma comment(linker, "/STACK:64000000")
using namespace std;
void __never(int a) { printf("\nOPS %d", a); }
int dx[] = {-1, -1, 0, 1, 1, 1, 0, -1};
int dy[] = {0, 1, 1, 1, 0, -1, -1, -1};
const int INF = 1000000;
int T;
int n, m;
char s[256][256];
bool flag[256][256];
pair<int, int> ord[256 * 256];
int be, en;
void init() {
  freopen("input.txt", "r", stdin);
  freopen("output.txt", "w", stdout);
}
bool bfs(int x, int y) {
  ord[be = en = 1] = make_pair(x, y);
  flag[x][y] = true;
  int maX = -INF, maY = -INF, miX = INF, miY = INF;
  while (be <= en) {
    pair<int, int> cur = ord[be++];
    maX = max(maX, cur.first);
    maY = max(maY, cur.second);
    miX = min(miX, cur.first);
    miY = min(miY, cur.second);
    for (int(i) = (0); (i) <= (7); ++(i)) {
      int hx = cur.first + dx[i];
      int hy = cur.second + dy[i];
      if (hx >= 0 && hx < n && hy >= 0 && hy < m && !flag[hx][hy] &&
          s[hx][hy] == '1') {
        ord[++en] = make_pair(hx, hy);
        flag[hx][hy] = true;
      }
    }
  }
  if (en < 4) return false;
  if (maX - miX != maY - miY) return false;
  int sz = maX - miX + 1;
  if (sz < 2) return false;
  if (en == sz * sz - (sz - 2) * (sz - 2)) {
    bool ok = true;
    for (int(i) = (0); (i) <= (sz - 1); ++(i)) {
      if (s[miX][miY + i] != '1') ok = false;
      if (s[maX][miY + i] != '1') ok = false;
      if (s[miX + i][miY] != '1') ok = false;
      if (s[miX + i][maY] != '1') ok = false;
      if (!ok) break;
    }
    if (ok) return true;
  }
  if (sz & 1) {
    if (en == (sz - 1) * 2) {
      bool ok = true;
      int ly = (miY + maY) / 2;
      int ry = ly;
      for (int(i) = (0); (i) <= (sz - 1); ++(i)) {
        if (s[miX + i][ly] != '1') ok = false;
        if (s[miX + i][ry] != '1') ok = false;
        if (i < sz / 2) {
          ly--, ry++;
        } else {
          ly++, ry--;
        }
      }
      if (ok) return true;
    }
  }
  return false;
}
void sol() {
  cin >> T;
  for (int(t) = (1); (t) <= (T); ++(t)) {
    cin >> n >> m;
    gets(s[0]);
    for (int(i) = (0); (i) <= (n - 1); ++(i)) gets(s[i]);
    memset(flag, 0, sizeof(flag));
    int ans = 0;
    for (int(i) = (0); (i) <= (n - 1); ++(i))
      for (int(j) = (0); (j) <= (m - 1); ++(j))
        if (!flag[i][j] && s[i][j] == '1') {
          if (bfs(i, j)) ans++;
        }
    printf("%d\n", ans);
  }
}
int main() {
  sol();
  return 0;
}
