#include <bits/stdc++.h>
#pragma GCC optimize("Ofast")
#pragma GCC optimize("unroll-loops")
using namespace std;
using ll = long long int;
using pii = pair<int, int>;
using pll = pair<ll, ll>;
using vi = vector<int>;
using vl = vector<ll>;
using vpii = vector<pii>;
using vpll = vector<pll>;
using vvi = vector<vi>;
using vvpii = vector<vpii>;
using vvpll = vector<vpll>;
using dd = double;
using ldd = long double;
using vd = vector<dd>;
const ll MOD = 1e9 + 7;
const int INF = INT_MAX;
const ll INF64 = LLONG_MAX;
const double PI = 3.1415926535897932;
struct T {
  pii topleft;
  int cnt;
};
ll N, M;
vector<vector<bool> > MAP;
vector<T> clist;
vvi cl;
const vpii offset = {{-1, -1}, {-1, 0}, {-1, 1}, {0, -1},
                     {0, 1},   {1, -1}, {1, 0},  {1, 1}};
void dfs(ll i, ll j, ll next) {
  if (!MAP[i][j] || cl[i][j]) return;
  cl[i][j] = next;
  clist[next].cnt++;
  for (ll f = (0); f < ((ll((offset).size()))); ++f) {
    dfs(i + offset[f].first, j + offset[f].second, next);
  }
}
bool check(ll x) {
  pii tl = clist[x].topleft;
  ll cnt = clist[x].cnt;
  if (cnt & 3) return false;
  cnt >>= 2;
  if (tl.first + cnt > N || tl.second + cnt > M) return false;
  bool t1 = true, t2 = true;
  if (tl.first + 2 * cnt > N || tl.second - cnt < 1) t2 = false;
  for (ll i = (0); i < (cnt); ++i) {
    if (!MAP[tl.first][tl.second + i] || !MAP[tl.first + i][tl.second + cnt] ||
        !MAP[tl.first + i + 1][tl.second] ||
        !MAP[tl.first + cnt][tl.second + i + 1]) {
      t1 = false;
      break;
    }
  }
  if (t1) return true;
  if (!t2) return false;
  for (ll i = (0); i < (cnt); ++i) {
    if (!MAP[tl.first + i][tl.second + i] ||
        !MAP[tl.first + cnt + i][tl.second + cnt - i] ||
        !MAP[tl.first + 2 * cnt - i][tl.second - i] ||
        !MAP[tl.first + cnt - i][tl.second - cnt + i]) {
      t2 = false;
      break;
    }
  }
  return t2;
}
void solve() {
  cin >> N >> M;
  MAP.assign(N + 2, vector<bool>(M + 2, 0));
  for (ll i = (1); i < (N + 1); ++i) {
    string s;
    cin >> s;
    for (ll j = (1); j < (M + 1); ++j) MAP[i][j] = s[j - 1] - '0';
  }
  cl.assign(N + 2, vi(M + 2, 0));
  clist.resize(1);
  for (ll i = (1); i < (N + 1); ++i) {
    for (ll j = (1); j < (M + 1); ++j) {
      if (MAP[i][j] && cl[i][j] == 0) {
        ll next = (ll((clist).size()));
        clist.push_back({{i, j}, 0});
        dfs(i, j, next);
      }
    }
  }
  ll ans = 0;
  for (ll i = (1); i < ((ll((clist).size()))); ++i) {
    if (check(i)) {
      ++ans;
    }
  }
  cout << ans << '\n';
}
int main() {
  fclose(stderr);
  ios::sync_with_stdio(false);
  cin.tie(0);
  cout.tie(0);
  cout << setiosflags(ios::fixed) << setprecision(15);
  cerr << setiosflags(ios::fixed) << setprecision(15);
  ll T;
  cin >> T;
  while (T--) {
    solve();
  };
  return 0;
}
