#include <bits/stdc++.h>
using namespace std;
char table[251][251];
vector<pair<int, int> > pos;
int n, m;
void go(int x, int y) {
  pos.push_back(make_pair(x, y));
  table[x][y] = '0';
  for (int dx = -1; dx <= 1; ++dx)
    for (int dy = -1; dy <= 1; ++dy) {
      if (x + dx < 0 || x + dx == n) continue;
      if (y + dy < 0 || y + dy == m) continue;
      if (table[x + dx][y + dy] == '1') {
        go(x + dx, y + dy);
      }
    }
}
int main() {
  int T;
  scanf("%d", &T);
  while (T--) {
    scanf("%d%d", &n, &m);
    for (int i = 0; i < n; ++i)
      for (int j = 0; j < m; ++j) scanf(" %c", &table[i][j]);
    int ret = 0;
    for (int i = 0; i < n; ++i)
      for (int j = 0; j < m; ++j) {
        if (table[i][j] == '1') {
          pos.clear();
          go(i, j);
          if (pos.size() < 4 || pos.size() % 4 != 0) continue;
          int isok = 1;
          sort(pos.begin(), pos.end());
          int sx = pos.front().first, sy = pos.front().second;
          int ex = pos.back().first, ey = pos.back().second;
          for (int k = 0; k < pos.size(); ++k) {
            if ((sx == pos[k].first || ex == pos[k].first) ||
                (sy == pos[k].second || ey == pos[k].second))
              ;
            else
              isok = 0;
            if (pos[k].first < sx || pos[k].first > ex) isok = 0;
            if (pos[k].second < sy || pos[k].second > ey) isok = 0;
          }
          if (isok && ey - sy == ex - sx && pos.size() == 4 * (ey - sy)) {
            ret++;
            continue;
          }
          isok = 1;
          int s1 = sx + sy, s2 = ex + ey;
          int m1 = sx - sy, m2 = ex - ey;
          for (int k = 0; k < pos.size(); ++k) {
            int xx = pos[k].first;
            int yy = pos[k].second;
            if ((s1 == xx + yy || s2 == xx + yy) ||
                (m1 == xx - yy || m2 == xx - yy))
              ;
            else
              isok = 0;
            if (xx + yy < s1 || xx + yy > s2) isok = 0;
            if (xx - yy < m1 || xx - yy > m2) isok = 0;
          }
          if (isok && sy == ey && pos.size() == 2 * (ex - sx)) {
            ret++;
            continue;
          }
        }
      }
    printf("%d\n", ret);
  }
}
