#include <bits/stdc++.h>
#pragma comment(linker, "/STACK:32000000")
void initf() {}
using namespace std;
const int inf = (1 << 29);
char a[261][261];
int n, m;
int sum1[261][261];
int sum2[261][261];
int sum3[511][511];
int sum4[511][511];
inline int get1(int x, int y, int s) {
  if (y == 0) return (sum1[x][y + s - 1]);
  return (sum1[x][y + s - 1] - sum1[x][y - 1]);
}
inline int get2(int x, int y, int s) {
  if (x == 0) return (sum2[x + s - 1][y]);
  return (sum2[x + s - 1][y] - sum2[x - 1][y]);
}
inline int get3(int x, int y, int s) {
  int mi = min(x, y);
  int fx = x + s - 1;
  int fy = y + s - 1;
  if (mi == 0) return (sum3[fx][fy]);
  return (sum3[fx][fy] - sum3[x - 1][y - 1]);
}
inline int get4(int x, int y, int s) {
  int mi = min(x, m - 1 - y);
  int fx = x + s - 1;
  int fy = y - s + 1;
  if (mi == 0) return (sum4[fx][fy]);
  return (sum4[fx][fy] - sum4[x - 1][y + 1]);
}
bool mark[265][265];
int cc;
int dx[] = {1, 0, -1, 0, 1, 1, -1, -1};
int dy[] = {0, 1, 0, -1, 1, -1, 1, -1};
bool valid(int x, int y) { return (x >= 0 && y >= 0 && x < n && y < m); }
void dfs(int x, int y) {
  if (!valid(x, y)) return;
  if (a[x][y] == '0') return;
  if (mark[x][y]) return;
  mark[x][y] = true;
  ++cc;
  for (int i = 0; i < (8); ++i) dfs(x + dx[i], y + dy[i]);
}
void solve() {
  memset(((mark)), (0), sizeof((mark)));
  memset(((sum1)), (0), sizeof((sum1)));
  memset(((sum2)), (0), sizeof((sum2)));
  memset(((sum3)), (0), sizeof((sum3)));
  memset(((sum4)), (0), sizeof((sum4)));
  scanf("%d%d\n", &n, &m);
  for (int i = 0; i < (n); ++i) scanf("%s\n", a[i]);
  for (int i = 0; i < (n); ++i)
    for (int j = 0; j < (m); ++j) {
      if (j == 0)
        sum1[i][j] = (a[i][j] == '1');
      else
        sum1[i][j] = sum1[i][j - 1] + (a[i][j] == '1');
    }
  for (int j = 0; j < (m); ++j)
    for (int i = 0; i < (n); ++i) {
      if (i == 0)
        sum2[i][j] = (a[i][j] == '1');
      else
        sum2[i][j] = sum2[i - 1][j] + (a[i][j] == '1');
    }
  for (int i = 0; i < (n + m); ++i) {
    int stx = 0, sty = 0;
    if (i < n)
      stx = n - i - 1;
    else
      sty = i - n;
    sum3[stx][sty] = (a[stx][sty] == '1');
    ++stx;
    ++sty;
    while (stx < n && sty < m) {
      sum3[stx][sty] = sum3[stx - 1][sty - 1] + (a[stx][sty] == '1');
      ++stx;
      ++sty;
    }
  }
  for (int i = 0; i < (n + m); ++i) {
    int stx = 0, sty = m - 1;
    if (i < m)
      sty = i;
    else
      stx = i - m;
    sum4[stx][sty] = (a[stx][sty] == '1');
    ++stx;
    --sty;
    while (stx < n && sty >= 0) {
      sum4[stx][sty] = sum4[stx - 1][sty + 1] + (a[stx][sty] == '1');
      ++stx;
      --sty;
    }
  }
  int ret = 0;
  int cnt = 0;
  for (int i = 0; i < (n); ++i)
    for (int j = 0; j < (m); ++j) {
      if (!mark[i][j]) {
        cc = 0;
        dfs(i, j);
        if (cc % 4 == 0) {
          cc /= 4;
          int sz = 1 + cc;
          if (!(i + (sz * 2 - 1) - 1 >= n))
            if (!(j - sz + 1 < 0 || j + sz - 1 >= m))
              if (get3(i, j, sz) == sz && get4(i, j, sz) == sz &&
                  get3(i + sz - 1, j - sz + 1, sz) == sz &&
                  get4(i + sz - 1, j + sz - 1, sz) == sz) {
                ++ret;
                continue;
              }
          if (!(i + sz - 1 >= n || j + sz - 1 >= m))
            if (get1(i, j, sz) == sz && get2(i, j, sz) == sz &&
                get1(i + sz - 1, j, sz) == sz &&
                get2(i, j + sz - 1, sz) == sz) {
              ++ret;
              continue;
            }
        }
      }
    }
  printf("%d\n", ret);
}
int main() {
  initf();
  int t;
  scanf("%d", &t);
  for (int i = 0; i < (t); ++i) solve();
  return (0);
}
