#include <bits/stdc++.h>
using namespace std;
char mm[256][256];
vector<pair<int, int> > pts;
int n, m;
int dx[8] = {-1, -1, -1, 0, 1, 1, 1, 0};
int dy[8] = {-1, 0, 1, 1, 1, 0, -1, -1};
void dfs(int x, int y) {
  pts.push_back(make_pair(x, y));
  mm[x][y] = '0';
  for (int i = 0; i < 8; ++i) {
    int newx = x + dx[i];
    int newy = y + dy[i];
    if (newx >= 0 && newx < n && newy >= 0 && newy < m && mm[newx][newy] == '1')
      dfs(newx, newy);
  }
}
bool isRect() {
  int le = 1000000000, ri = -1, up = 1000000000, dn = -1;
  int sz = pts.size();
  if (pts.size() % 4 != 0) return false;
  for (int i = 0; i < pts.size(); ++i) {
    le = min(le, pts[i].second);
    ri = max(ri, pts[i].second);
    up = min(up, pts[i].first);
    dn = max(dn, pts[i].first);
  }
  if (ri - le != dn - up) return false;
  int lcnt = 0, rcnt = 0, ucnt = 0, dcnt = 0;
  for (int i = 0; i < pts.size(); ++i) {
    if (pts[i].first == up && pts[i].second >= le && pts[i].second <= ri)
      ucnt++;
    if (pts[i].first == dn && pts[i].second >= le && pts[i].second <= ri)
      dcnt++;
    if (pts[i].second == le && pts[i].first >= up && pts[i].first <= dn) lcnt++;
    if (pts[i].second == ri && pts[i].first >= up && pts[i].first <= dn) rcnt++;
  }
  if (lcnt == rcnt && rcnt == ucnt && ucnt == dcnt && dcnt == sz / 4 + 1)
    return true;
  if (ucnt != 1) return false;
  le = 1000000000, ri = -1000000000, up = 1000000000, dn = -1000000000;
  for (int i = 0; i < pts.size(); ++i) {
    le = min(le, pts[i].first - pts[i].second);
    ri = max(ri, pts[i].first - pts[i].second);
    up = min(up, pts[i].first + pts[i].second);
    dn = max(dn, pts[i].first + pts[i].second);
  }
  if (ri - le != dn - up) return false;
  lcnt = 0, rcnt = 0, ucnt = 0, dcnt = 0;
  for (int i = 0; i < pts.size(); ++i) {
    if (pts[i].first - pts[i].second >= le &&
        pts[i].first - pts[i].second <= ri) {
      if (pts[i].first + pts[i].second == up) ucnt++;
      if (pts[i].first + pts[i].second == dn) dcnt++;
    }
    if (pts[i].first + pts[i].second >= up &&
        pts[i].first + pts[i].second <= dn) {
      if (pts[i].first - pts[i].second == le) lcnt++;
      if (pts[i].first - pts[i].second == ri) rcnt++;
    }
  }
  if (lcnt == rcnt && rcnt == ucnt && ucnt == dcnt && dcnt == sz / 4 + 1)
    return true;
  else
    return false;
}
int main() {
  int t;
  scanf("%d", &t);
  while (t--) {
    scanf("%d%d", &n, &m);
    for (int i = 0; i < n; ++i) scanf("%s", mm[i]);
    int count = 0;
    for (int i = 0; i < n; ++i) {
      for (int j = 0; j < m; ++j) {
        if (mm[i][j] == '0') continue;
        pts.clear();
        dfs(i, j);
        if (isRect()) count++;
      }
    }
    printf("%d\n", count);
  }
  return 0;
}
