#include <bits/stdc++.h>
using namespace std;
int t, n, m, ans;
vector<vector<int>> gh, used;
int dx[8] = {1, 1, 1, 0, 0, -1, -1, -1};
int dy[8] = {1, 0, -1, 1, -1, 1, 0, -1};
bool onseg(double x1, double y1, double x2, double y2, double x, double y) {
  double d1 = sqrt((x - x1) * (x - x1) + (y - y1) * (y - y1));
  double d2 = sqrt((x - x2) * (x - x2) + (y - y2) * (y - y2));
  double d3 = sqrt((x1 - x2) * (x1 - x2) + (y1 - y2) * (y1 - y2));
  return abs(d3 - d2 - d1) < 1e-9;
}
void bfs(int y, int x) {
  if (gh[y][x] == 0) return;
  if (used[y][x]) return;
  used[y][x] = true;
  vector<pair<int, int>> todo, temp, bunch;
  int cx, cy, nx, ny;
  todo.push_back({y, x});
  bunch.push_back({y, x});
  while (true) {
    if (todo.empty()) break;
    for (int i = 0; i < todo.size(); i++) {
      cy = todo[i].first, cx = todo[i].second;
      for (int j = 0; j < 8; j++) {
        ny = cy + dy[j];
        nx = cx + dx[j];
        if (ny >= 0 && ny < n && nx >= 0 && nx < m) {
          if (used[ny][nx] == 0) {
            temp.push_back({ny, nx});
            bunch.push_back({ny, nx});
            used[ny][nx] = 1;
          }
        }
      }
    }
    todo = temp;
    temp.clear();
  }
  if (bunch.size() % 4) return;
  int len = bunch.size() / 4;
  int topx, topy, btx, bty;
  topx = bunch[0].second, topy = bunch[0].first;
  btx = topx + len, bty = topy + len;
  bool sq = true;
  for (int j = 1; j < bunch.size(); j++) {
    int nx = bunch[j].second, ny = bunch[j].first;
    if (!onseg(topx, topy, btx, topy, nx, ny) &&
        !onseg(topx, topy, topx, bty, nx, ny) &&
        !onseg(btx, bty, btx, topy, nx, ny) &&
        !onseg(btx, bty, topx, bty, nx, ny)) {
      sq = false;
      break;
    }
  }
  if (sq) {
    ans++;
    return;
  }
  sq = true;
  topx = bunch[0].second, topy = bunch[0].first;
  btx = topx, bty = topy + 2 * len;
  for (int j = 1; j < bunch.size(); j++) {
    int nx = bunch[j].second, ny = bunch[j].first;
    if (!onseg(topx, topy, topx - len, topy + len, nx, ny) &&
        !onseg(topx, topy, topx + len, topy + len, nx, ny) &&
        !onseg(btx, bty, topx - len, topy + len, nx, ny) &&
        !onseg(btx, bty, topx + len, topy + len, nx, ny)) {
      sq = false;
      break;
    }
  }
  if (sq) {
    ans++;
  }
}
void solve() {
  cin >> n >> m;
  gh = used = vector<vector<int>>(n, vector<int>(m));
  ans = 0;
  char ch;
  int val;
  for (int i = 0; i < n; i++) {
    for (int j = 0; j < m; j++) {
      cin >> ch;
      val = ch - '0';
      if (val == 0) used[i][j] = 1;
      gh[i][j] = val;
    }
  }
  for (int i = 0; i < n; i++) {
    for (int j = 0; j < m; j++)
      if (used[i][j] == 0) bfs(i, j);
  }
  cout << ans << endl;
}
int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(0);
  cin >> t;
  while (t--) solve();
  return 0;
}
