#include <bits/stdc++.h>
#pragma GCC optimize("O500")
#pragma comment(linker, "/STACK:1677777216")
#pragma warning(default : 4)
using namespace std;
const double eps = 1e-12;
const int oo = 0x3F3F3F3F;
const long long ooLL = 0x3F3F3F3F3F3F3F3FLL;
const int MOD = 1000000007;
template <typename T>
T sqr(T x) {
  return x * x;
}
template <typename T>
void debpr(const T &);
template <typename T1, typename T2>
void debpr(const pair<T1, T2> &);
template <typename T1, typename T2>
void debpr(const vector<T1, T2> &);
template <typename T>
void debpr(const set<T> &);
template <typename T1, typename T2>
void debpr(const map<T1, T2> &);
template <typename T>
void prcont(T be, T en, const string &st, const string &fi, const string &mi) {
  debpr(st);
  bool ft = 0;
  while (be != en) {
    if (ft) debpr(mi);
    ft = 1;
    debpr(*be);
    ++be;
  }
  debpr(fi);
}
template <typename T>
void debpr(const T &a) {}
template <typename T1, typename T2>
void debpr(const pair<T1, T2> &p) {
  debpr("(");
  debpr(p.first);
  debpr(", ");
  debpr(p.second);
  debpr(")");
}
template <typename T1, typename T2>
void debpr(const vector<T1, T2> &a) {
  prcont(a.begin(), a.end(), "[", "]", ", ");
}
template <typename T>
void debpr(const set<T> &a) {
  prcont(a.begin(), a.end(), "{", "}", ", ");
}
template <typename T1, typename T2>
void debpr(const map<T1, T2> &a) {
  prcont(a.begin(), a.end(), "{", "}", ", ");
}
void deb(){};
template <typename T1>
void deb(const T1 &t1) {
  debpr(t1);
  debpr('\n');
}
template <typename T1, typename T2>
void deb(const T1 &t1, const T2 &t2) {
  debpr(t1);
  debpr(' ');
  debpr(t2);
  debpr('\n');
}
template <typename T1, typename T2, typename T3>
void deb(const T1 &t1, const T2 &t2, const T3 &t3) {
  debpr(t1);
  debpr(' ');
  debpr(t2);
  debpr(' ');
  debpr(t3);
  debpr('\n');
}
template <typename T1, typename T2, typename T3, typename T4>
void deb(const T1 &t1, const T2 &t2, const T3 &t3, const T4 &t4) {
  debpr(t1);
  debpr(' ');
  debpr(t2);
  debpr(' ');
  debpr(t3);
  debpr(' ');
  debpr(t4);
  debpr('\n');
}
const double PI = acos(-1.);
long long Round(double x) { return x < 0 ? x - .5 : x + .5; }
template <typename T>
void ass(bool v, const T &x, string m = "Fail") {
  if (!v) {
    deb(m);
    deb(x);
    throw;
  }
}
int main() {
  void run();
  run();
  return 0;
}
int n, m, T;
char s[1111][1111];
pair<int, int> p[1111 * 1111];
int u[1111][1111], c;
void go(int i, int j) {
  if (i < 0 || j < 0 || i >= n || j >= m || u[i][j] || s[i][j] != '1') return;
  u[i][j] = 1;
  p[c++] = {i, j};
  for (int dx = (-1), _b(2); dx < _b; ++dx)
    for (int dy = (-1), _b(2); dy < _b; ++dy) go(i + dx, j + dy);
}
bool t1() {
  if (c % 4) return 0;
  int l = 1 + c / 4;
  if (l < 2) return 0;
  for (int i = (0), _b(l); i < _b; ++i)
    if (p[i].first != p[0].first || p[i].second != p[0].second + i) return 0;
  for (int i = (0), _b(l - 2); i < _b; ++i) {
    int li = l + i + i, ri = li + 1;
    if (p[li].first != p[0].first + i + 1 || p[li].second != p[0].second)
      return 0;
    if (p[ri].first != p[0].first + i + 1 || p[ri].second != p[l - 1].second)
      return 0;
  }
  for (int i = (0), _b(l); i < _b; ++i)
    if (p[c - l + i].first != p[i].first + l - 1 ||
        p[c - l + i].second != p[i].second)
      return 0;
  return 1;
}
bool t2() {
  if (c % 4) return 0;
  int l = 1 + c / 4;
  for (int i = (0), _b(l); i < _b; ++i) {
    int ri = i + i, li = ri - 1;
    if (li < 0) li = 0;
    if (p[li].first != p[0].first + i || p[li].second != p[0].second - i)
      return 0;
    if (p[ri].first != p[0].first + i || p[ri].second != p[0].second + i)
      return 0;
  }
  for (int i = (0), _b(l); i < _b; ++i) {
    int li = c - i - i - 1, ri = li + 1;
    if (ri >= c) ri = c - 1;
    if (p[li].first != p[c - 1].first - i ||
        p[li].second != p[c - 1].second - i)
      return 0;
    if (p[ri].first != p[c - 1].first - i ||
        p[ri].second != p[c - 1].second + i)
      return 0;
  }
  return 1;
}
void run() {
  scanf("%d", &T);
  while (T--) {
    scanf("%d %d\n", &n, &m);
    for (int i = (0), _b(n); i < _b; ++i) gets(s[i]);
    memset(u, 0, sizeof(u));
    int r = 0;
    for (int i = (0), _b(n); i < _b; ++i)
      for (int j = (0), _b(m); j < _b; ++j)
        if (s[i][j] == '1' && !u[i][j]) {
          c = 0;
          go(i, j);
          sort(p, p + c);
          r += t1() || t2();
        }
    printf("%d\n", r);
  }
}
