#include <bits/stdc++.h>
using namespace std;
const int dx[] = {0, 0, 1, 1, 1, -1, -1, -1},
          dy[] = {1, -1, 0, 1, -1, 0, 1, -1};
const int maxn = 255;
int T, n, m, ans;
char a[maxn][maxn];
bool vis[maxn][maxn];
bool s[maxn][maxn];
queue<int> Q;
bool out(int x, int y) { return x <= 0 || y <= 0 || x > n || y > m; }
int main() {
  scanf("%d", &T);
  while (T--) {
    scanf("%d%d", &n, &m);
    for (int i = 1; i <= n; i++) scanf("%s", a[i] + 1);
    memset(vis, 0, sizeof(vis));
    ans = 0;
    for (int i = 1; i <= n; i++)
      for (int j = 1; j <= m; j++) {
        if (a[i][j] == '1' && !vis[i][j]) {
          int tot;
          memset(s, 0, sizeof(s));
          while (!Q.empty()) Q.pop();
          vis[i][j] = 1;
          Q.push(i * 1000 + j);
          s[i][j] = 1;
          tot = 1;
          while (!Q.empty()) {
            int x = Q.front() / 1000, y = Q.front() % 1000;
            Q.pop();
            for (int r = 0; r < 8; r++) {
              int ni = x + dx[r], nj = y + dy[r];
              if (!out(ni, nj) && a[ni][nj] == '1' && !vis[ni][nj]) {
                vis[ni][nj] = 1;
                Q.push(ni * 1000 + nj);
                s[ni][nj] = 1;
                tot++;
              }
            }
          }
          if (tot % 4 != 0) continue;
          int l = tot / 4 + 1;
          bool yes = 1;
          int cnt, x, y;
          for (x = 1; x <= n; x++) {
            cnt = 0;
            for (y = 1; y <= m; y++)
              if (s[x][y]) cnt++;
            if (cnt > 0) break;
          }
          if (cnt == 1) {
            for (y = 1; y <= m; y++)
              if (s[x][y]) break;
            int x1 = x, x2 = x1 + l - 1, x3 = x2 + l - 1;
            int y2 = y, y1 = y2 - l + 1, y3 = y2 + l - 1;
            for (int k = 0; k < l; k++)
              if (out(x1 + k, y2 - k) || !s[x1 + k][y2 - k]) yes = 0;
            for (int k = 0; k < l; k++)
              if (out(x1 + k, y2 + k) || !s[x1 + k][y2 + k]) yes = 0;
            for (int k = 0; k < l; k++)
              if (out(x3 - k, y2 - k) || !s[x3 - k][y2 - k]) yes = 0;
            for (int k = 0; k < l; k++)
              if (out(x3 - k, y2 + k) || !s[x3 - k][y2 + k]) yes = 0;
          } else {
            if (cnt != l) continue;
            for (y = 1; y <= m; y++)
              if (s[x][y]) break;
            int y1 = y, y2 = y1 + l - 1;
            int x1 = x, x2 = x1 + l - 1;
            for (int k = y1; k <= y2; k++)
              if (out(x1, k) || !s[x1][k]) yes = 0;
            for (int k = y1; k <= y2; k++)
              if (out(x2, k) || !s[x2][k]) yes = 0;
            for (int k = x1; k <= x2; k++)
              if (out(k, y1) || !s[k][y1]) yes = 0;
            for (int k = x1; k <= x2; k++)
              if (out(k, y2) || !s[k][y2]) yes = 0;
          }
          if (yes) ans++;
        }
      }
    printf("%d\n", ans);
  }
  return 0;
}
