#include <bits/stdc++.h>
using namespace std;
int n, m;
char a[260][260];
int cnt, col, ans;
int ex, ey;
int vis[260][260];
int dx[] = {1, -1, 0, 0, 1, 1, -1, -1}, dy[] = {0, 0, 1, -1, 1, -1, 1, -1};
bool bdj(int i, int j) {
  if (i >= 1 && i <= n && j >= 1 && j <= m) return true;
  return false;
}
void dfs(int i, int j) {
  vis[i][j] = col;
  cnt++;
  if (i > ex) {
    ex = i;
    ey = j;
  }
  if (i == ex && j > ey) {
    ey = j;
  }
  for (int k = 0; k <= 7; k++) {
    int x = i + dx[k];
    int y = j + dy[k];
    if (bdj(x, y) && vis[x][y] == 0 && a[x][y] == '1') {
      dfs(x, y);
    }
  }
}
void solve() {
  cin >> n >> m;
  col = 0;
  ans = 0;
  memset(vis, 0, sizeof(vis));
  for (int i = 1; i <= n; i++) {
    for (int j = 1; j <= m; j++) {
      cin >> a[i][j];
    }
  }
  for (int i = 1; i <= n; i++) {
    for (int j = 1; j <= m; j++) {
      if (vis[i][j] == 0 && a[i][j] == '1') {
        cnt = 0;
        col++;
        ex = 0;
        ey = 0;
        dfs(i, j);
        if (cnt % 4 != 0) continue;
        bool jdg1 = 1, jdg2 = 1;
        if (ex > cnt / 4 && ey > cnt / 4) {
          for (int k = ex; k >= ex - cnt / 4; k--) {
            if (vis[k][ey] != col) {
              jdg1 = 0;
              break;
            }
            if (vis[k][ey - cnt / 4] != col) {
              jdg1 = 0;
              break;
            }
          }
          for (int k = ey; k >= ey - cnt / 4; k--) {
            if (vis[ex][k] != col) {
              jdg1 = 0;
              break;
            }
            if (vis[ex - cnt / 4][k] != col) {
              jdg1 = 0;
              break;
            }
          }
        } else
          jdg1 = 0;
        if (ex > cnt / 2 && ey > cnt / 4 && ey <= m - cnt / 4) {
          for (int k = 0; k <= cnt / 4; k++) {
            if (vis[ex - k][ey + k] != col) {
              jdg2 = 0;
              break;
            }
            if (vis[ex - k][ey - k] != col) {
              jdg2 = 0;
              break;
            }
          }
          for (int k = 0; k <= cnt / 4; k++) {
            if (vis[ex - cnt / 2 + k][ey + k] != col) {
              jdg2 = 0;
              break;
            }
            if (vis[ex - cnt / 2 + k][ey - k] != col) {
              jdg2 = 0;
              break;
            }
          }
        } else
          jdg2 = 0;
        if (jdg1 || jdg2) {
          ans++;
        }
      }
    }
  }
  cout << ans << endl;
}
int main() {
  int t;
  cin >> t;
  while (t--) {
    solve();
  }
  return 0;
}
