#include <bits/stdc++.h>
using namespace std;
const long double EPS = 1E-9;
const int INF = (int)1E9;
const long long INF64 = (long long)1E18;
const long double PI = 2 * acos(.0);
const int dx[] = {0, 0, -1, 1, -1, -1, 1, 1};
const int dy[] = {-1, 1, 0, 0, -1, 1, -1, 1};
int n, m, k, X[100100], Y[100100];
char a[300][300];
bool used[300][300];
int cU = 1, cUsed[300][300];
inline bool valid(int x, int y) {
  return (0 <= x && x < n) && (0 <= y && y < m);
}
void DFS(int x, int y) {
  X[k] = x;
  Y[k] = y;
  k++;
  used[x][y] = true;
  for (int i = 0; i < (int)(8); i++) {
    int xx = x + dx[i], yy = y + dy[i];
    if (valid(xx, yy) && !used[xx][yy] && a[xx][yy] == '1') DFS(xx, yy);
  }
}
inline bool f1() {}
inline bool analyze() {
  int xMax = -1, yMax = -1, xMin = INF, yMin = INF;
  for (int i = 0; i < (int)(k); i++) {
    xMax = max(xMax, X[i]);
    xMin = min(xMin, X[i]);
    yMax = max(yMax, Y[i]);
    yMin = min(yMin, Y[i]);
  }
  if (xMax - xMin != yMax - yMin) return false;
  if (k < 4) return false;
  if (xMax - xMin + 1 < 2) return false;
  if (k == (xMax - xMin + 1) * 4 - 4) {
    cU++;
    for (int x = xMin; x <= xMax; x++) {
      cUsed[x][yMax] = cU;
      cUsed[x][yMin] = cU;
    }
    for (int y = yMin; y <= yMax; y++) {
      cUsed[xMax][y] = cU;
      cUsed[xMin][y] = cU;
    }
    bool correct = true;
    for (int i = 0; i < (int)(k); i++)
      if (cUsed[X[i]][Y[i]] != cU) {
        correct = false;
        break;
      }
    if (correct) return true;
  }
  if (k != (xMax - xMin + 1) * 2 - 2) return false;
  if ((xMax - xMin) % 2 != 0) return false;
  cU++;
  int l = (yMax + yMin) / 2, r = l;
  for (int i = 0; i < (int)((xMax - xMin) / 2 + 1); i++) {
    cUsed[xMin + i][l] = cUsed[xMin + i][r] = cU;
    cUsed[xMax - i][l] = cUsed[xMax - i][r] = cU;
    l--;
    r++;
  }
  for (int i = 0; i < (int)(k); i++)
    if (cUsed[X[i]][Y[i]] != cU) return false;
  return true;
}
inline void solve() {
  scanf("%d%d\n", &n, &m);
  for (int i = 0; i < (int)(n); i++) {
    gets(a[i]);
    for (int j = 0; j < (int)(m); j++) used[i][j] = false;
  }
  int ans = 0;
  for (int i = 0; i < (int)(n); i++)
    for (int j = 0; j < (int)(m); j++)
      if (a[i][j] == '1' && !used[i][j]) {
        k = 0;
        DFS(i, j);
        if (analyze()) ans++;
      }
  printf("%d\n", ans);
}
int main() {
  int tests;
  scanf("%d", &tests);
  for (int test = 0; test < (int)(tests); test++) solve();
  return 0;
}
