#include <bits/stdc++.h>
using namespace std;
const int NMAX = 300;
char str[NMAX][NMAX];
int field[NMAX][NMAX];
int n, m;
int cur_lab = 2;
bool not_quad[NMAX * NMAX];
struct Vec2 {
  int x, y;
  Vec2(int x, int y) : x(x), y(y) {}
  void operator+=(const Vec2 &that) {
    x += that.x;
    y += that.y;
  }
  void operator-=(const Vec2 &that) {
    x -= that.x;
    y -= that.y;
  }
  int dist(const Vec2 &that) const { return abs(x - that.x) + abs(y - that.y); }
  bool ok() { return x >= 0 && x < n && y >= 0 && y < m; }
};
Vec2 go_dir(Vec2 pos, Vec2 dir, int lab) {
  for (;;) {
    if (!pos.ok() || (field[pos.x][pos.y] != 1 && field[pos.x][pos.y] != lab)) {
      pos -= dir;
      break;
    }
    field[pos.x][pos.y] = lab;
    pos += dir;
  }
  return pos;
}
void qu_prepare() {
  for (int i = 0; i < n; i++) {
    for (int j = 0; j < m - 1; j++) {
      if (field[i][j] == 1) {
        if (field[i][j + 1] == 1) {
          int lab = cur_lab++;
          Vec2 pos = Vec2(i, j);
          Vec2 pox(0, 0);
          int dist, d;
          pox = go_dir(pos, Vec2(0, 1), lab);
          dist = pox.dist(pos);
          if (dist < 1) not_quad[lab] = true;
          pos = pox;
          pox = go_dir(pos, Vec2(1, 0), lab);
          d = pox.dist(pos);
          if (d != dist) not_quad[lab] = true;
          pos = pox;
          pox = go_dir(pos, Vec2(0, -1), lab);
          d = pox.dist(pos);
          if (d != dist) not_quad[lab] = true;
          pos = pox;
          pox = go_dir(pos, Vec2(-1, 0), lab);
          d = pox.dist(pos);
          if (d != dist) not_quad[lab] = true;
        } else {
          int lab = cur_lab++;
          Vec2 pos = Vec2(i, j);
          Vec2 pox(0, 0);
          int dist, d;
          pox = go_dir(pos, Vec2(1, 1), lab);
          dist = pox.dist(pos);
          if (dist < 1) not_quad[lab] = true;
          pos = pox;
          pox = go_dir(pos, Vec2(1, -1), lab);
          d = pox.dist(pos);
          if (d != dist) not_quad[lab] = true;
          pos = pox;
          pox = go_dir(pos, Vec2(-1, -1), lab);
          d = pox.dist(pos);
          if (d != dist) not_quad[lab] = true;
          pos = pox;
          pox = go_dir(pos, Vec2(-1, 1), lab);
          d = pox.dist(pos);
          if (d != dist) not_quad[lab] = true;
        }
      }
    }
  }
}
int main() {
  int t;
  scanf("%d", &t);
  for (int it = 0; it < t; it++) {
    cur_lab = 2;
    for (int i = 0; i < NMAX * NMAX; i++) not_quad[i] = false;
    scanf("%d%d\n", &n, &m);
    for (int i = 0; i < n; i++) {
      gets(str[i]);
      for (int j = 0; j < m; j++) {
        field[i][j] = str[i][j] - '0';
      }
    }
    qu_prepare();
    for (int i = 0; i < n; i++) {
      for (int j = 0; j < m; j++) {
        if (field[i][j] != 0) {
          for (int ii = -1; ii <= 1; ii++) {
            for (int jj = -1; jj <= 1; jj++) {
              int x = i + ii;
              int y = j + jj;
              if (x >= 0 && x < n && y >= 0 && y < m) {
                if (field[x][y] != 0 && field[x][y] != field[i][j]) {
                  not_quad[field[x][y]] = true;
                  not_quad[field[i][j]] = true;
                }
              }
            }
          }
        }
      }
    }
    int num_quads = 0;
    for (int lab = 2; lab < cur_lab; lab++) {
      if (!not_quad[lab]) ++num_quads;
    }
    printf("%d\n", num_quads);
  }
  return 0;
}
