#include <bits/stdc++.h>
using namespace std;
int dd[3] = {-1, 0, 1};
void extract(vector<string> &board, int i, int j,
             set<pair<int, int> > &coords) {
  if (board[i][j] != '1') return;
  board[i][j] = '0';
  coords.insert(pair<int, int>(i, j));
  for (int k = 0; k < 3; k++) {
    for (int l = 0; l < 3; l++) {
      if (i + dd[k] < 0 || i + dd[k] >= board.size()) continue;
      if (j + dd[l] < 0 || j + dd[l] >= board[0].size()) continue;
      if (board[i + dd[k]][j + dd[l]] == '0') continue;
      extract(board, i + dd[k], j + dd[l], coords);
    }
  }
  return;
}
int main() {
  int t;
  cin >> t;
  for (int k = 0; k < t; k++) {
    int n, m;
    cin >> n >> m;
    vector<string> board(n);
    for (int i = 0; i < n; i++) cin >> board[i];
    int count = 0;
    for (int i = 0; i < n; i++) {
      for (int j = 0; j < m; j++) {
        if (board[i][j] == '0') continue;
        set<pair<int, int> > coords;
        extract(board, i, j, coords);
        if (coords.size() < 4) continue;
        __typeof(coords.begin()) iter1 = coords.begin(), iter2 = coords.end();
        iter2--;
        pair<int, int> start = *iter1, end = *iter2;
        int i0 = start.first, i1 = end.first, j0 = start.second,
            j1 = end.second;
        if (j0 == j1 && (i1 - i0) % 2 == 0) {
          int length = (i1 - i0) / 2;
          set<pair<int, int> > dup;
          for (int d = 0; d <= length; d++) {
            dup.insert(pair<int, int>(i0 + d, j0 + d));
            dup.insert(pair<int, int>(i0 + d, j0 - d));
            dup.insert(pair<int, int>(i1 - d, j1 + d));
            dup.insert(pair<int, int>(i1 - d, j1 - d));
          }
          if (dup == coords) count++;
        }
        if (i1 - i0 == j1 - j0) {
          int length = i1 - i0;
          set<pair<int, int> > dup;
          for (int d = 0; d <= length; d++) {
            dup.insert(pair<int, int>(i0 + d, j0));
            dup.insert(pair<int, int>(i0, j0 + d));
            dup.insert(pair<int, int>(i1 - d, j1));
            dup.insert(pair<int, int>(i1, j1 - d));
          }
          if (dup == coords) count++;
        }
      }
    }
    cout << count << endl;
  }
}
