#include <bits/stdc++.h>
using namespace std;
int bitct(long long r) { return r == 0 ? 0 : (bitct(r >> 1) + (r & 1)); }
long long gcd(long long x, long long y) { return x ? gcd(y % x, x) : y; }
template <typename T>
ostream &operator<<(ostream &o, vector<T> v) {
  o << "{";
  int i = 0, s = v.size();
  for (; i + 1 < s; i++) o << v[i] << ", ";
  if (s) o << v[i];
  o << "}";
  return o;
}
long long choose(long long n, long long q) {
  if (n == 0 || q == 0) return 1;
  if (q == 1)
    return n;
  else
    return (choose(n, q - 1) * (n - q + 1) / q);
}
int dx[8] = {0, 1, 1, 1, 0, -1, -1, -1};
int dy[8] = {1, 1, 0, -1, -1, -1, 0, 1};
int fixboard(vector<string> &board, int x, int y, int din, int len) {
  int nx = x;
  int ny = y;
  for (int dd = 0; dd < 4; dd++) {
    int d = (2 * dd + din) % 8;
    for (int i = 0; i < len; i++) {
      if (!(nx >= 0 && nx < board[0].size() && ny >= 0 && ny < board.size()))
        return 0;
      if (board[ny][nx] == '2') board[ny][nx] = '1';
      if (i < len - 1) {
        nx += dx[d];
        ny += dy[d];
      }
    }
  }
  return 0;
}
int testpos(vector<string> &board, int x, int y, int din) {
  int nx = x;
  int ny = y;
  int len = 0;
  while (nx >= 0 && nx < board[0].size() && ny >= 0 && ny < board.size()) {
    if (board[ny][nx] == '0') break;
    len++;
    nx += dx[din];
    ny += dy[din];
  }
  nx = x;
  ny = y;
  if (len < 2) return 0;
  for (int dd = 0; dd < 4; dd++) {
    int d = (2 * dd + din) % 8;
    for (int i = 0; i < len; i++) {
      if (!(nx >= 0 && nx < board[0].size() && ny >= 0 && ny < board.size())) {
        fixboard(board, x, y, din, len);
        return 0;
      }
      if (board[ny][nx] == '0') {
        fixboard(board, x, y, din, len);
        return 0;
      }
      board[ny][nx] = '2';
      if (i < len - 1) {
        nx += dx[d];
        ny += dy[d];
      }
    }
  }
  for (int dd = 0; dd < 4; dd++) {
    int d = (2 * dd + din) % 8;
    for (int i = 0; i < len; i++) {
      for (int dt = 0; dt < 8; dt++) {
        int nnx = nx + dx[dt];
        int nny = ny + dy[dt];
        if (!(nnx >= 0 && nnx < board[0].size() && nny >= 0 &&
              nny < board.size()))
          continue;
        if (board[nny][nnx] == '1') {
          fixboard(board, x, y, din, len);
          return 0;
        }
      }
      if (i < len - 1) {
        nx += dx[d];
        ny += dy[d];
      }
    }
  }
  return 1;
}
int main() {
  int t;
  cin >> t;
  for (int tti = 0; tti < t; tti++) {
    int n, m;
    cin >> n >> m;
    vector<string> board;
    for (int i = 0; i < n; i++) {
      string r;
      cin >> r;
      board.push_back(r);
    }
    int count = 0;
    for (int x = 0; x < m - 1; x++)
      for (int y = 0; y < n - 1; y++) {
        if (board[y][x] == '0') continue;
        if (x > 0 && board[y][x - 1] == '1') continue;
        if (y > 0 && board[y - 1][x] == '1') continue;
        if (y > 0 && x > 0 && board[y - 1][x - 1] == '1') continue;
        if (testpos(board, x, y, 0) || testpos(board, x, y, 1)) count++;
      }
    cout << count << endl;
  }
}
