#include <bits/stdc++.h>
using namespace std;
int dx[8] = {0, 1, 1, 1, 0, -1, -1, -1};
int dy[8] = {1, 1, 0, -1, -1, -1, 0, 1};
char a[300][300];
int main() {
  int t;
  scanf("%d", &t);
  for (int l = 0; l < t; l++) {
    int n, m;
    scanf("%d %d", &n, &m);
    int i, j, k;
    for (i = 0; i < n + 2; i++) {
      for (j = 0; j < m + 2; j++) {
        a[i][j] = '0';
      }
    }
    for (i = 1; i <= n; i++) {
      char in[500];
      scanf("%s", in);
      for (j = 1; j <= m; j++) {
        a[i][j] = in[j - 1];
      }
      a[i][m + 1] = '0';
    }
    int ans = 0;
    for (i = 1; i <= n; i++) {
      for (j = 1; j <= m; j++) {
        if (a[i][j] == '1') {
          int c = 1;
          int x0 = i, y0 = j, x1 = i, y1 = j;
          queue<pair<int, int> > q;
          q.push(pair<int, int>(i, j));
          a[i][j] = '2';
          while (!q.empty()) {
            pair<int, int> tmp = q.front();
            q.pop();
            if (tmp.first > x1) {
              x1 = tmp.first;
              y1 = tmp.second;
            } else if (tmp.first == x1 && tmp.second > y1) {
              y1 = tmp.second;
            }
            for (k = 0; k < 8; k++) {
              if (a[tmp.first + dx[k]][tmp.second + dy[k]] == '1') {
                q.push(pair<int, int>(tmp.first + dx[k], tmp.second + dy[k]));
                c++;
                a[tmp.first + dx[k]][tmp.second + dy[k]] = '2';
              }
            }
          }
          if ((x1 - x0 == y1 - y0) && c >= 4 && c == 4 * (x1 - x0)) {
            int f = 0;
            for (k = 0; k <= x1 - x0; k++) {
              if (a[x0 + k][y0] != '2') f = 1;
              if (a[x0][y0 + k] != '2') f = 1;
              if (a[x1][y1 - k] != '2') f = 1;
              if (a[x1 - k][y1] != '2') f = 1;
            }
            if (f == 0) ans++;
          } else if (y0 == y1 && c >= 4 && c == 2 * (x1 - x0) && c % 4 == 0) {
            if (y0 - (x1 - x0) / 2 > 0 && y0 + (x1 - x0) / 2 <= m) {
              int f = 0;
              for (k = 0; k <= (x1 - x0) / 2; k++) {
                if (a[x0 + k][y0 + k] != '2') f = 1;
                if (a[x0 + k][y0 - k] != '2') f = 1;
                if (a[x1 - k][y1 + k] != '2') f = 1;
                if (a[x1 - k][y1 - k] != '2') f = 1;
              }
              if (f == 0) ans++;
            }
          }
        }
      }
    }
    printf("%d\n", ans);
  }
  return 0;
}
