#include <bits/stdc++.h>
using namespace std;
#pragma comment(linker, "/STACK:200000000")
const double EPS = 1E-9;
const int INF = 1000000000;
const long long INF64 = (long long)1E18;
const double PI = 3.1415926535897932384626433832795;
const int dxy[8][2] = {{1, 0},  {-1, 0}, {0, 1},   {0, -1},
                       {1, -1}, {1, 1},  {-1, -1}, {-1, 1}};
char s[300][300];
int h[300], v[300], lx, rx, ly, ry, n, m;
bool u[300][300];
bool isin(int x, int y) { return 0 <= x && x < n && 0 <= y && y < m; }
void dfs(int x, int y) {
  u[x][y] = true;
  if (x < lx) {
    lx = x;
    h[x] = 0;
  }
  if (x > rx) {
    rx = x;
    h[x] = 0;
  }
  if (y < ly) {
    ly = y;
    v[y] = 0;
  }
  if (y > ry) {
    ry = y;
    v[y] = 0;
  }
  h[x]++;
  v[y]++;
  for (int i = 0; i < (int)(8); i++) {
    int nx = x + dxy[i][0];
    int ny = y + dxy[i][1];
    if (!isin(nx, ny) || s[nx][ny] == '0' || u[nx][ny]) continue;
    dfs(nx, ny);
  }
}
bool type1() {
  for (int t = lx + 1; t <= rx - 1; t++)
    if (!(h[t] == 2 && s[t][ly] == '1' && s[t][ry] == '1')) return false;
  for (int t = ly + 1; t <= ry - 1; t++)
    if (!(v[t] == 2 && s[lx][t] == '1' && s[rx][t] == '1')) return false;
  if (h[lx] != rx - lx + 1 || h[rx] != rx - lx + 1 || v[ly] != rx - lx + 1 ||
      v[ry] != rx - lx + 1)
    return false;
  return true;
}
bool type2() {
  if ((rx - lx) % 2 == 1) return false;
  for (int t = lx + 1; t <= rx - 1; t++)
    if (!(h[t] == 2 && s[t][ly + (rx - lx) / 2 - min(t - lx, rx - t)] == '1' &&
          s[t][ly + (rx - lx) / 2 + min(t - lx, rx - t)] == '1'))
      return false;
  for (int t = ly + 1; t <= ry - 1; t++)
    if (!(v[t] == 2 && s[lx + (rx - lx) / 2 - min(t - ly, ry - t)][t] == '1' &&
          s[lx + (rx - lx) / 2 + min(t - ly, ry - t)][t] == '1'))
      return false;
  if (!(h[lx] == 1 && s[lx][ly + (rx - lx) / 2] == '1' && h[rx] == 1 &&
        s[rx][ly + (rx - lx) / 2] == '1' && v[ly] == 1 &&
        s[lx + (rx - lx) / 2][ly] == '1' && v[ry] == 1 &&
        s[lx + (rx - lx) / 2][ry] == '1'))
    return false;
  return true;
}
int main() {
  int tt;
  scanf("%d", &tt);
  for (int ii = 0; ii < (int)(tt); ii++) {
    scanf("%d%d\n", &n, &m);
    for (int i = 0; i < (int)(n); i++) gets(s[i]);
    for (int i = 0; i < (int)(n); i++)
      for (int j = 0; j < (int)(m); j++) u[i][j] = false;
    int ans = 0;
    for (int i = 0; i < (int)(n); i++)
      for (int j = 0; j < (int)(m); j++)
        if (!u[i][j] && s[i][j] == '1') {
          lx = ly = INF;
          rx = ry = -INF;
          dfs(i, j);
          if (rx - lx < 1 || rx - lx != ry - ly) continue;
          bool t1 = type1(), t2 = type2();
          ans += (t1 || t2);
        }
    cout << ans << endl;
  }
  return 0;
}
