#include <bits/stdc++.h>
using namespace std;
char tab[260][260];
int ext[2][260][260][2];
int extd[2][260][260][2];
bool checksq(int i, int j, int sz, int num) {
  if (sz == 0) return true;
  assert(i + sz - 1 <= 253 && j + sz - 1 <= 253);
  return ext[num][i][j][0] >= sz && ext[num][i][j][1] >= sz &&
         ext[num][i + sz - 1][j][1] >= sz && ext[num][i][j + sz - 1][0] >= sz;
}
bool checkdiag(int i, int j, int sz, int num) {
  if (sz == 0) return true;
  assert(i + sz - 1 <= 263 && j + sz - 1 <= 253);
  return extd[num][i][j][0] >= sz && extd[num][i][j][1] >= sz &&
         extd[num][i + sz - 1][j - sz + 1][0] >= sz &&
         extd[num][i + sz - 1][j + sz - 1][1] >= sz;
}
int main() {
  int t, n, m;
  scanf("%d\n", &t);
  for (int z = 0; z < t; z++) {
    scanf("%d %d\n", &n, &m);
    for (int i = 0; i <= m + 3; i++) {
      tab[0][i] = tab[n + 2][i] = '0';
      tab[1][i] = tab[n + 3][i] = '0';
    }
    for (int i = 2; i <= n + 1; i++) {
      tab[i][0] = tab[i][1] = '0';
      cin >> tab[i] + 2;
      tab[i][m + 2] = tab[i][m + 3] = '0';
    }
    n += 4;
    m += 4;
    memset(ext, 0, sizeof ext);
    memset(extd, 0, sizeof extd);
    for (int i = n - 1; i >= 0; i--)
      for (int j = m - 1; j >= 0; j--)
        for (int k = 0; k < 2; k++) {
          if (tab[i][j] - '0' != k) {
            ext[k][i][j][0] = ext[k][i][j][1] = 0;
            continue;
          }
          if (i == n - 1)
            ext[k][i][j][0] = 1;
          else
            ext[k][i][j][0] = 1 + ext[k][i + 1][j][0];
          if (j == m - 1)
            ext[k][i][j][1] = 1;
          else
            ext[k][i][j][1] = 1 + ext[k][i][j + 1][1];
        }
    for (int i = n - 1; i >= 0; i--)
      for (int j = 0; j < m; j++)
        for (int k = 0; k < 2; k++) {
          if (tab[i][j] - '0' != k) {
            extd[k][i][j][0] = extd[k][i][j][1] = 0;
            continue;
          }
          if (i == n - 1 || j == m - 1)
            extd[k][i][j][0] = 1;
          else
            extd[k][i][j][0] = 1 + extd[k][i + 1][j + 1][0];
          if (i == n - 1 || j == 0)
            extd[k][i][j][1] = 1;
          else
            extd[k][i][j][1] = 1 + extd[k][i + 1][j - 1][1];
        }
    int ret = 0;
    for (int i = 0; i < n; i++)
      for (int j = 0; j < m; j++) {
        if (tab[i][j] == '0') continue;
        int sz = min(ext[1][i][j][0], ext[1][i][j][1]);
        if (sz < 2) continue;
        if (checksq(i, j, sz, 1) && checksq(i - 1, j - 1, sz + 2, 0) &&
            checksq(i + 1, j + 1, sz - 2, 0))
          ret++;
      }
    for (int i = 0; i < n; i++)
      for (int j = 0; j < m; j++) {
        if (tab[i][j] == '0') continue;
        int sz = min(extd[1][i][j][0], extd[1][i][j][1]);
        if (sz < 2) continue;
        if (checkdiag(i, j, sz, 1) && checkdiag(i - 1, j, sz + 1, 0) &&
            checkdiag(i + 1, j, sz - 1, 0) && checkdiag(i + 2, j, sz - 2, 0)) {
          if (extd[0][i - 1][j - 1][1] >= sz &&
              extd[0][i - 1][j + 1][0] >= sz &&
              extd[0][i + sz][j - sz][0] >= sz &&
              extd[0][i + sz][j + sz][1] >= sz)
            ret++;
        }
      }
    cout << ret << endl;
  }
}
