#include <bits/stdc++.h>
using namespace std;
int n, m, ans, flag, cnt, tot, rx, ry, edge;
char mp[305][305];
bool vis[305][305];
int dx[] = {-1, 1, 0, 0, -1, -1, 1, 1};
int dy[] = {0, 0, -1, 1, -1, 1, -1, 1};
struct node {
  int x, y;
} q[90005];
bool isok(int x, int y) {
  if (x < 1 || x > n || y < 1 || y > m || mp[x][y] == '0') return false;
  return true;
}
void dfs(int x, int y) {
  int i, nx, ny;
  for (i = 0; i < 8; i++) {
    nx = x + dx[i];
    ny = y + dy[i];
    if (!isok(nx, ny) || vis[nx][ny]) continue;
    if (rx < nx || rx == nx && ry < ny) {
      rx = nx;
      ry = ny;
    }
    q[++cnt].x = nx;
    q[cnt].y = ny;
    vis[nx][ny] = 1;
    dfs(nx, ny);
  }
}
void dfs1(int x, int y) {
  int i, nx, ny;
  for (i = 0; i < 8; i++) {
    nx = x + dx[i];
    ny = y + dy[i];
    if (!isok(nx, ny) || vis[nx][ny]) continue;
    if (rx < nx || rx == nx && ry < ny) {
      rx = nx;
      ry = ny;
    }
    q[++cnt].x = nx;
    q[cnt].y = ny;
    vis[nx][ny] = 1;
    dfs1(nx, ny);
  }
}
bool yes1(int x, int y, int nx, int ny) {
  if (x < nx) return false;
  if (abs(nx - x) != abs(ny - y) || abs(nx - x) + 1 > edge) return false;
  return true;
}
bool yes2(int x, int y, int nx, int ny) {
  if (x > nx) return false;
  if (abs(nx - x) != abs(ny - y) || abs(nx - x) + 1 > edge) return false;
  return true;
}
void solve() {
  int i, j, t;
  ans = 0;
  memset(vis, 0, sizeof(vis));
  for (i = 1; i <= n; i++) {
    for (j = 1; j <= m; j++) {
      if (mp[i][j] == '0' || vis[i][j]) continue;
      vis[i][j] = 1;
      rx = i;
      ry = j;
      cnt = 0;
      q[++cnt].x = i;
      q[cnt].y = j;
      dfs(i, j);
      if (rx == i && ry == j) continue;
      if (rx - i != ry - j || cnt != (rx - i + 1) * 4 - 4) continue;
      flag = 1;
      for (int k = 1; k <= cnt; k++) {
        if (q[k].x == i && q[k].y >= j && q[k].y <= ry ||
            q[k].x == rx && q[k].y >= j && q[k].y <= ry ||
            q[k].y == j && q[k].x >= i && q[k].x <= rx ||
            q[k].y == ry && q[k].x >= i && q[k].x <= rx)
          ;
        else {
          flag = 0;
          break;
        }
      }
      ans += flag;
    }
  }
  memset(vis, 0, sizeof(vis));
  for (i = 1; i <= n; i++) {
    for (j = 1; j <= m; j++) {
      if (mp[i][j] == '0' || vis[i][j]) continue;
      vis[i][j] = 1;
      rx = i;
      ry = j;
      cnt = 0;
      q[++cnt].x = i;
      q[cnt].y = j;
      dfs1(i, j);
      if (rx == i && ry == j) continue;
      if (ry != j) continue;
      if ((rx - i + 1) % 2 == 0) continue;
      edge = (rx - i + 1) / 2 + 1;
      if (edge * 4 - 4 != cnt) continue;
      flag = 1;
      for (int k = 1; k <= cnt; k++) {
        if (yes1(q[k].x, q[k].y, i, j) || yes2(q[k].x, q[k].y, rx, ry))
          ;
        else {
          flag = 0;
          break;
        }
      }
      ans += flag;
    }
  }
  printf("%d\n", ans);
}
int main() {
  int i, j, t;
  scanf("%d", &t);
  while (t--) {
    scanf("%d%d", &n, &m);
    for (i = 1; i <= n; i++) {
      scanf("%s", mp[i] + 1);
    }
    solve();
  }
  return 0;
}
