#include <bits/stdc++.h>
using namespace std;
int n, m;
char inp[260][260];
bool cov[260][260];
int mx, my, Mx, My;
vector<int> outai;
vector<int> outaj;
void dfs(int i, int j) {
  cov[i][j] = true;
  mx = min(mx, j);
  my = min(i, my);
  Mx = max(Mx, j);
  My = max(My, i);
  outai.push_back(i);
  outaj.push_back(j);
  for (int dx = -1; dx <= 1; dx++) {
    for (int dy = -1; dy <= 1; dy++) {
      int y = i + dy;
      int x = j + dx;
      if (y < 0 || y >= n || x < 0 || x >= m || inp[y][x] == '0' || cov[y][x])
        continue;
      dfs(y, x);
    }
  }
}
int ar[4];
bool firstType() {
  memset(ar, 0, sizeof(ar));
  for (int t = 0; t < outai.size(); t++) {
    bool ch = false;
    if (outai[t] == my && outaj[t] >= mx && outaj[t] <= Mx) ar[0]++, ch = true;
    if (outai[t] >= my && outai[t] <= My && outaj[t] == mx) ar[1]++, ch = true;
    if (outai[t] == My && outaj[t] >= mx && outaj[t] <= Mx) ar[2]++, ch = true;
    if (outai[t] >= my && outai[t] <= My && outaj[t] == Mx) ar[3]++, ch = true;
    if (!ch) return false;
  }
  if (ar[0] == ar[1] && ar[1] == ar[2] && ar[2] == ar[3]) {
    if (ar[0] > 1 && ar[0] == (Mx - mx + 1) && ar[0] == (My - my + 1))
      return true;
  }
  return false;
}
int getcord(int i, int j) { return i + j; }
bool secondType() {
  memset(ar, 0, sizeof(ar));
  mx = my = 1000;
  Mx = My = -1;
  for (int t = 0; t < outai.size(); t++) {
    int ty = outai[t];
    int tx = outaj[t];
    outai[t] = getcord(ty, tx);
    outaj[t] = getcord(ty, m - 1 - tx);
    mx = min(outaj[t], mx);
    Mx = max(outaj[t], Mx);
    My = max(outai[t], My);
    my = min(outai[t], my);
  }
  for (int t = 0; t < outai.size(); t++) {
    if ((outai[t] - my) % 2 != 0 || (outaj[t] - mx) % 2 != 0) return false;
  }
  for (int t = 0; t < outai.size(); t++) {
    bool ch = false;
    if (outai[t] == my && outaj[t] >= mx && outaj[t] <= Mx) ar[0]++, ch = true;
    if (outai[t] >= my && outai[t] <= My && outaj[t] == mx) ar[1]++, ch = true;
    if (outai[t] == My && outaj[t] >= mx && outaj[t] <= Mx) ar[2]++, ch = true;
    if (outai[t] >= my && outai[t] <= My && outaj[t] == Mx) ar[3]++, ch = true;
    if (!ch) return false;
  }
  if (ar[0] == ar[1] && ar[1] == ar[2] && ar[2] == ar[3]) {
    if (ar[0] > 1 && ar[0] == (1 + (Mx - mx + 1)) / 2 &&
        ar[0] == (1 + (My - my + 1)) / 2)
      return true;
  }
  return false;
}
int main() {
  int tc;
  scanf("%d", &tc);
  while (tc--) {
    memset(inp, 0, sizeof(inp));
    scanf("%d%d", &n, &m);
    int ret = 0;
    for (int i = 0; i < n; i++) scanf("%s", inp[i]);
    memset(cov, 0, sizeof(cov));
    for (int i = 0; i < n; i++) {
      for (int j = 0; j < m; j++) {
        if (inp[i][j] == '0') continue;
        if (cov[i][j]) continue;
        mx = my = 260;
        Mx = My = -1;
        outai.clear();
        outaj.clear();
        dfs(i, j);
        if (firstType()) ret++;
        if (secondType()) ret++;
      }
    }
    printf("%d\n", ret);
  }
  return 0;
}
