#include <bits/stdc++.h>
using namespace std;
char a[300][300];
int n, m;
bool isSquare(vector<pair<int, int> > &v) {
  int minI = 1000, minJ = 1000, maxI = 0, maxJ = 0;
  for (int i = 0; i < (int)v.size(); ++i) {
    minI = min(minI, v[i].first);
    maxI = max(maxI, v[i].first);
    minJ = min(minJ, v[i].second);
    maxJ = max(maxJ, v[i].second);
  }
  int a = maxI - minI, b = maxJ - minJ;
  if (a != b || a < 1) return false;
  bool ok = v.size() == a * 4;
  for (int i = 0; ok && i < (int)v.size(); ++i) {
    if (v[i].first != minI && v[i].first != maxI && v[i].second != minJ &&
        v[i].second != maxJ) {
      ok = false;
    }
  }
  if (ok) return true;
  if (a % 2 != 0 && b % 2 != 0) return false;
  int len = a / 2 + 1;
  if (v.size() != (len - 1) * 4) return false;
  for (int i = 0; i < (int)v.size(); ++i) {
    v[i].first -= len - 1 + minI;
    v[i].second -= len - 1 + minJ;
    if (labs(v[i].first) + labs(v[i].second) != len - 1) return false;
  }
  return true;
}
bool flood(int si, int sj) {
  queue<pair<int, int> > q;
  q.push(make_pair(si, sj));
  vector<pair<int, int> > res;
  res.push_back(make_pair(si, sj));
  a[si][sj] = '0';
  while (!q.empty()) {
    pair<int, int> cur = q.front();
    q.pop();
    for (int di = -1; di <= 1; ++di)
      for (int dj = -1; dj <= 1; ++dj) {
        int ni = cur.first + di, nj = cur.second + dj;
        if (0 <= ni && ni < n && 0 <= nj && nj < m && a[ni][nj] == '1') {
          q.push(make_pair(ni, nj));
          res.push_back(make_pair(ni, nj));
          a[ni][nj] = '0';
        }
      }
  }
  return isSquare(res);
}
int main() {
  int T;
  cin >> T;
  while (T--) {
    scanf("%d%d\n", &n, &m);
    memset(a, 0, sizeof a);
    for (int i = 0; i < n; ++i) gets(a[i]);
    int res = 0;
    for (int i = 0; i < n; ++i)
      for (int j = 0; j < m; ++j)
        if (a[i][j] == '1') {
          if (flood(i, j)) ++res;
        }
    cout << res << endl;
  }
  return 0;
}
