#include <bits/stdc++.h>
using namespace std;
const int INF = 2147483647;
const long long LLINF = 9223372036854775807LL;
const int maxn = 260;
int a[maxn][maxn];
int sumi[maxn][maxn] = {};
int sumj[maxn][maxn] = {};
int sumd1[maxn][maxn] = {};
int sumd2[maxn][maxn] = {};
pair<int, int> jumpi[maxn][maxn];
pair<int, int> jumpj[maxn][maxn];
pair<int, int> jumpd1[maxn][maxn];
pair<int, int> jumpd2[maxn][maxn];
int gd1(int i1, int j1, int i2, int j2) {
  return sumd1[i1][j1] - sumd1[i2 - 1][j2 - 1];
}
int gd2(int i1, int j1, int i2, int j2) {
  return sumd2[i1][j1] - sumd2[i2 - 1][j2 + 1];
}
int gsi(int i1, int j1, int i2, int j2) {
  return sumi[i1][j1] - sumi[i2][j2 - 1];
}
int gsj(int i1, int j1, int i2, int j2) {
  return sumj[i1][j1] - sumj[i2 - 1][j2];
}
int main() {
  int T;
  scanf("%d", &T);
  while (T--) {
    int n, m;
    scanf("%d%d", &n, &m);
    n += 4;
    m += 4;
    for (int i = 0; i < n; ++i)
      for (int j = 0; j < m; ++j) a[i][j] = 0;
    for (int i = 2; i < n - 2; ++i)
      for (int j = 2; j < m - 2; ++j) {
        char c = 0;
        while (c != '0' && c != '1') c = getchar();
        a[i][j] = c - '0';
      }
    for (int i = 1; i < n; ++i)
      for (int j = 1; j < m; ++j) {
        sumi[i][j] = a[i][j] + sumi[i][j - 1];
        sumj[i][j] = a[i][j] + sumj[i - 1][j];
        sumd1[i][j] = a[i][j] + sumd1[i - 1][j - 1];
        sumd2[i][j] = a[i][j] + sumd2[i - 1][j + 1];
        jumpi[i][j] = make_pair(i, j);
        if (a[i][j - 1]) jumpi[i][j] = jumpi[i][j - 1];
        jumpj[i][j] = make_pair(i, j);
        if (a[i - 1][j]) jumpj[i][j] = jumpj[i - 1][j];
        jumpd1[i][j] = make_pair(i, j);
        if (a[i - 1][j - 1]) jumpd1[i][j] = jumpd1[i - 1][j - 1];
        jumpd2[i][j] = make_pair(i, j);
        if (a[i - 1][j + 1]) jumpd2[i][j] = jumpd2[i - 1][j + 1];
      }
    int ans = 0;
    for (int i = 0; i < n; ++i)
      for (int j = 0; j < m; ++j) {
        if (a[i][j] == 0) continue;
        {
          pair<int, int> tt = jumpj[i][j];
          tt = jumpi[tt.first][tt.second];
          pair<int, int> rr = jumpi[i][j];
          rr = jumpj[rr.first][rr.second];
          if (rr != make_pair(i, j) && rr == tt &&
              i - rr.first == j - rr.second) {
            bool ok = true;
            int ri = rr.first;
            int rj = rr.second;
            ok &= gsi(i + 1, j + 1, i + 1, rj - 1) <= 0;
            ok &= gsi(ri - 1, j + 1, ri - 1, rj - 1) <= 0;
            ok &= gsi(i - 1, j - 1, i - 1, rj + 1) <= 0;
            ok &= gsi(ri + 1, j - 1, ri + 1, rj + 1) <= 0;
            ok &= gsj(i + 1, j + 1, ri - 1, j + 1) <= 0;
            ok &= gsj(i + 1, rj - 1, ri - 1, rj - 1) <= 0;
            ok &= gsj(i - 1, j - 1, ri + 1, j - 1) <= 0;
            ok &= gsj(i - 1, rj + 1, ri + 1, rj + 1) <= 0;
            if (ok) ans++;
          }
        }
        {
          pair<int, int> tt = jumpd1[i][j];
          int ti = tt.first;
          int Lj = tt.second;
          tt = jumpd2[tt.first][tt.second];
          pair<int, int> rr = jumpd2[i][j];
          int Rj = rr.second;
          rr = jumpd1[rr.first][rr.second];
          if (rr != make_pair(i, j) && rr == tt && j - rr.second == 0) {
            bool ok = true;
            ok &= gd2(i + 1, j, ti, Rj + 1) <= 0;
            ok &= gd2(i + 1, j + 1, ti + 1, Rj + 1) <= 0;
            ok &= gd2(ti, Lj - 1, rr.first - 1, j) <= 0;
            ok &= gd2(ti - 1, Lj - 1, rr.first - 1, j - 1) <= 0;
            ok &= gd1(ti - 1, Rj + 1, rr.first - 1, j + 1) <= 0;
            ok &= gd1(ti - 1, Rj, rr.first, j + 1) <= 0;
            ok &= gd1(i + 1, j - 1, ti + 1, Lj - 1) <= 0;
            ok &= gd1(i, j - 1, ti + 1, Lj) <= 0;
            ok &= gd2(i - 1, j, ti, Rj - 1) <= 0;
            ok &= gd2(i - 2, j, ti, Rj - 2) <= 0;
            ok &= gd2(ti, Lj + 1, rr.first + 1, j) <= 0;
            ok &= gd2(ti, Lj + 2, rr.first + 2, j) <= 0;
            ok &= gd1(i - 1, j, ti, Lj + 1) <= 0;
            ok &= gd1(i - 2, j, ti, Lj + 2) <= 0;
            ok &= gd1(ti, Rj - 1, rr.first + 1, j) <= 0;
            ok &= gd1(ti, Rj - 2, rr.first + 2, j) <= 0;
            if (ok) ans++;
          }
        }
      }
    printf("%d\n", ans);
  }
  return 0;
}
