#include <bits/stdc++.h>
using namespace std;
const int X[8] = {-1, 1, 0, 0, -1, -1, 1, 1},
          Y[8] = {0, 0, -1, 1, -1, 1, 1, -1};
char ar[256][256];
int n, m, t, cnt, ans;
void dfs(int x, int y) {
  if (x < 0 || x >= n || y < 0 || y >= m || ar[x][y] != '1') return;
  ar[x][y] = '2';
  ++cnt;
  for (int i = 0; i < 8; ++i) dfs(x + X[i], y + Y[i]);
}
bool check(int x, int y, int a, int b) {
  int d = cnt / 4;
  for (int i = 0; i <= d; ++i)
    for (int j : {a, b})
      if (x + X[j] * i < 0 || y + Y[j] * i < 0 || x + X[j] * i >= n ||
          y + Y[j] * i >= m || ar[x + X[j] * i][y + Y[j] * i] != '2')
        return false;
  return true;
}
int main() {
  ios::sync_with_stdio(false);
  cin.tie(0);
  cout.tie(0);
  for (cin >> t; t; --t) {
    cin >> n >> m;
    ans = 0;
    for (int i = 0; i < n; ++i) cin >> ar[i];
    for (int i = 0; i < n; ++i)
      for (int j = 0; j < m; ++j)
        if (ar[i][j] == '1') {
          cnt = 0;
          dfs(i, j);
          if (cnt % 4 || cnt / 4 > min(n, m)) continue;
          ans += check(i, j, 1, 3) && check(i + cnt / 4, j + cnt / 4, 0, 2);
          ans += check(i, j, 6, 7) && check(i + cnt / 2, j, 4, 5);
        }
    cout << ans << '\n';
  }
}
