#include <bits/stdc++.h>
using namespace std;
char a[(1 << 8)][(1 << 8)];
int TESTCASE;
int n, m;
int DFS;
int Ans;
const int dx[] = {0, 1, 0, -1, 1, 1, -1, -1};
const int dy[] = {1, 0, -1, 0, 1, -1, 1, -1};
void dfs(int x, int y) {
  if (x < 0 || x >= n || y < 0 || y >= m || a[x][y] != '1') {
    return;
  }
  a[x][y] = '2';
  DFS++;
  for (int i = 0; i < 8; i++) {
    dfs(x + dx[i], y + dy[i]);
  }
}
bool Chk(int x, int y, int d, int lft, int rgh) {
  for (int i = 0; i <= d; i++) {
    for (int j = lft; j < rgh; j++) {
      if (x + dx[j] * i < 0 || x + dx[j] * i >= n || y + dy[j] * i < 0 ||
          y + dy[j] * i >= m || a[x + dx[j] * i][y + dy[j] * i] != '2') {
        return false;
      }
    }
  }
  return true;
}
int main() {
  scanf("%d", &TESTCASE);
  while (TESTCASE--) {
    Ans = 0;
    scanf("%d%d", &n, &m);
    for (int i = 0; i < n; i++) {
      scanf("%s", a[i]);
    }
    for (int i = 0; i < n; i++) {
      for (int j = 0; j < m; j++) {
        if (a[i][j] == '1') {
          DFS = 0;
          dfs(i, j);
          int r = DFS;
          if (r % 4 != 0 || r / 4 > 250) {
            continue;
          }
          Ans += (Chk(i, j, r / 4, 0, 2) &&
                  Chk(i + r / 4, j + r / 4, r / 4, 2, 4));
          Ans += (Chk(i, j, r / 4, 4, 6) && Chk(i + r / 2, j, r / 4, 6, 8));
        }
      }
    }
    printf("%d\n", Ans);
  }
  return 0;
}
