#include <bits/stdc++.h>
using namespace std;
char s[300][300];
int vis[300][300];
struct node {
  int x, y;
};
queue<node> q;
int f[8][2] = {0, 1, 1, 0, 0, -1, -1, 0, 1, 1, -1, 1, -1, -1, 1, -1};
int bfs(int x, int y, int n, int m) {
  node e, t;
  e.x = x, e.y = y;
  vis[x][y] = 1;
  while (!q.empty()) q.pop();
  q.push(e);
  int num = 1;
  while (!q.empty()) {
    e = q.front();
    q.pop();
    for (int i = 0; i < 8; i++) {
      t.x = e.x + f[i][0];
      t.y = e.y + f[i][1];
      if (t.x < 1 || t.x > n || t.y < 1 || t.y > m) continue;
      if (s[t.x][t.y] == '0') continue;
      if (vis[t.x][t.y]) continue;
      vis[t.x][t.y] = 1;
      num++;
      q.push(t);
    }
  }
  return num;
}
bool solve1(int x, int y, int n, int m, int k) {
  int xi = x, yi = y;
  while (yi <= m && s[xi][yi] == '1') yi++;
  int num = yi - y, len = yi - y;
  while (xi <= n && s[xi][y] == '1') xi++;
  if (xi - x != len) return false;
  xi = x;
  while (xi <= n && s[xi][yi - 1] == '1') xi++;
  if (xi - x != len) return false;
  yi = y;
  while (yi <= m && s[xi - 1][yi] == '1') yi++;
  if (yi - y != len) return false;
  num = len * 4 - 4;
  if (num != k) return false;
  return true;
}
bool solve2(int x, int y, int n, int m, int k) {
  int xi, yi, num, len;
  xi = x, yi = y;
  while (yi >= 1 && xi <= n && s[xi][yi] == '1') xi++, yi--;
  len = xi - x;
  xi--, yi++;
  int g = xi;
  while (yi <= m && xi <= n && s[xi][yi] == '1') xi++, yi++;
  if (xi - g != len) return false;
  xi = x, yi = y;
  while (yi <= m && xi <= n && s[xi][yi] == '1') xi++, yi++;
  if (xi - x != len) return false;
  xi--, yi--;
  while (yi >= 1 && xi <= n && s[xi][yi] == '1') xi++, yi--;
  if (xi - g != len) return false;
  num = len * 4 - 4;
  if (num != k) return false;
  return true;
}
int main() {
  int t;
  scanf("%d", &t);
  while (t--) {
    int n, m;
    scanf("%d%d", &n, &m);
    for (int i = 1; i <= n; i++) {
      scanf("%s", s[i] + 1);
    }
    int ans = 0;
    memset(vis, 0, sizeof(vis));
    for (int i = 1; i <= n; i++) {
      for (int j = 1; j <= m; j++) {
        if (s[i][j] == '1' && vis[i][j] == 0) {
          int k = bfs(i, j, n, m);
          if (k % 4) continue;
          if (s[i][j + 1] == '1' && solve1(i, j, n, m, k))
            ans++;
          else if (solve2(i, j, n, m, k))
            ans++;
        }
      }
    }
    printf("%d\n", ans);
  }
  return 0;
}
