#include <bits/stdc++.h>
using namespace std;
inline void read(register int *n) {
  register char c;
  *n = 0;
  do {
    c = getchar();
  } while (c < '0' || c > '9');
  do {
    *n = c - '0' + *n * 10;
    c = getchar();
  } while (c >= '0' && c <= '9');
}
char b[300][300];
int r, c;
vector<pair<int, int> > V;
void flood_fill(int i, int j) {
  if (i < 0 || j < 0 || i >= r || j >= c) return;
  if (b[i][j] == '0') return;
  b[i][j] = '0';
  V.push_back(pair<int, int>(i, j));
  flood_fill(i - 1, j);
  flood_fill(i + 1, j);
  flood_fill(i, j - 1);
  flood_fill(i, j + 1);
  flood_fill(i - 1, j - 1);
  flood_fill(i + 1, j + 1);
  flood_fill(i + 1, j - 1);
  flood_fill(i - 1, j + 1);
}
vector<vector<pair<int, int> > > cache[300 * 4];
void normalize(vector<pair<int, int> > &V) {
  sort((V).begin(), (V).end());
  for (register int i = (1); i < (int)(((int)(V).size())); ++i)
    V[i].first -= V[0].first, V[i].second -= V[0].second;
  V[0] = pair<int, int>(0, 0);
}
int main() {
  ios::sync_with_stdio(false);
  for (register int i = (1); i < (int)(300); ++i) {
    vector<pair<int, int> > tmp;
    tmp.push_back(pair<int, int>(0, 0));
    tmp.push_back(pair<int, int>(i, 0));
    tmp.push_back(pair<int, int>(0, i));
    tmp.push_back(pair<int, int>(i, i));
    for (register int j = (1); j < (int)(i); ++j)
      tmp.push_back(pair<int, int>(j, 0)), tmp.push_back(pair<int, int>(0, j)),
          tmp.push_back(pair<int, int>(i, j)),
          tmp.push_back(pair<int, int>(j, i));
    normalize(tmp);
    cache[((int)(tmp).size())].push_back(tmp);
    tmp.clear();
    for (register int j = (0); j < (int)(i); ++j)
      tmp.push_back(pair<int, int>(i - j, j)),
          tmp.push_back(pair<int, int>(-j, i - j)),
          tmp.push_back(pair<int, int>(j - i, -j)),
          tmp.push_back(pair<int, int>(j, j - i));
    normalize(tmp);
    cache[((int)(tmp).size())].push_back(tmp);
  }
  int t;
  cin >> t;
  while (t--) {
    cin >> r >> c;
    for (register int i = (0); i < (int)(r); ++i) cin >> b[i];
    int tot = 0;
    for (register int i = (0); i < (int)(r); ++i)
      for (register int j = (0); j < (int)(c); ++j)
        if (b[i][j] == '1') {
          V.clear();
          flood_fill(i, j);
          normalize(V);
          bool ok = false;
          if (((int)(V).size()) < 4 * 300)
            for (__typeof((cache[((int)(V).size())]).begin()) it =
                     (cache[((int)(V).size())]).begin();
                 it != (cache[((int)(V).size())]).end(); ++it)
              if (*it == V) {
                ok = true;
                break;
              }
          if (ok) tot++;
        }
    cout << tot << "\n";
  }
  return 0;
}
