#include <bits/stdc++.h>   using namespace std;   const int dx[8] = {-1, -1, -1, 0, 0, 1, 1, 1};   const int dy[8] = {-1, 0, 1, -1, 1, -1, 0, 1};   int t, ans, n, m;   int a[256][256], u[256][256];   char c;   void work(int x, int y) {     int tx, ty;     u[x][y] = 1;     for (int i = 0; i <= 7; i++) {       tx = x + dx[i];       ty = y + dy[i];       if ((a[tx][ty] == 1) and (u[tx][ty] == 0)) work(tx, ty);     }   }   bool check1(int x, int y) {     int d = x, r = y;     while (a[x][r + 1] == 1) r++;     while (a[d + 1][y] == 1) d++;     if (((r - y) != (d - x)) or ((r - y) == 0)) return false;     for (int j = y; j <= r; j++)       if (a[d][j] == 0) return false;     for (int i = x; i <= d; i++)       if (a[i][r] == 0) return false;     for (int j = y - 1; j <= r + 1; j++)       if ((a[x - 1][j] == 1) or (a[d + 1][j] == 1)) return false;     for (int i = x - 1; i <= d + 1; i++)       if ((a[i][y - 1] == 1) or (a[i][r + 1] == 1)) return false;     if ((r - y) == 1) return true;     for (int j = y + 1; j <= r - 1; j++)       if ((a[x + 1][j] == 1) or (a[d - 1][j] == 1)) return false;     for (int i = x + 1; i <= d - 1; i++)       if ((a[i][y + 1] == 1) or (a[i][r - 1] == 1)) return false;     return true;   }   bool check2(int x, int y) {     int li = x, lj = y;     int ri = x, rj = y;     int xx, yy;     while (a[li + 1][lj - 1] == 1) {       li++;       lj--;     }     while (a[ri + 1][rj + 1] == 1) {       ri++;       rj++;     }     if ((li != ri) or (li == x)) return false;     for (int i = 1; i <= li - x; i++)       if ((a[li + i][lj + i] == 0) or (a[ri + i][rj - i] == 0)) return false;     int d = x + 2 * (li - x);     for (int i = 0; i <= li - x; i++) {       xx = x + i;       yy = y - i;       if ((a[xx - 1][yy] == 1) or (a[xx - 1][yy - 1] == 1)) return false;       yy = y + i;       if ((a[xx - 1][yy] == 1) or (a[xx - 1][yy + 1] == 1)) return false;       xx = d - i;       yy = y - i;       if ((a[xx + 1][yy] == 1) or (a[xx + 1][yy - 1] == 1)) return false;       yy = y + i;       if ((a[xx + 1][yy] == 1) or (a[xx + 1][yy + 1] == 1)) return false;     }     if ((a[li][lj - 1] == 1) or (a[li][rj + 1] == 1)) return false;     if ((li - x) == 1)       if (a[x + 1][y] == 1)         return false;       else         return true;     for (int i = 1; i < li - x; i++) {       xx = x + i;       yy = y - i;       if ((a[xx + 1][yy] == 1) or (a[xx + 1][yy + 1] == 1)) return false;       yy = y + i;       if ((a[xx + 1][yy] == 1) or (a[xx + 1][yy - 1] == 1)) return false;       xx = d - i;       yy = y - i;       if ((a[xx - 1][yy] == 1) or (a[xx - 1][yy + 1] == 1)) return false;       yy = y + i;       if ((a[xx - 1][yy] == 1) or (a[xx - 1][yy - 1] == 1)) return false;     }     if ((a[x + 1][y] == 1) or (a[d - 1][y] == 1))       return false;     else       return true;   }   bool judge(int x, int y) {     work(x, y);     if ((check1(x, y)) or (check2(x, y)))       return true;     else       return false;   }   int main() {     cin >> t;     for (; t > 0; t--) {       cin >> n >> m;       memset(a, 0, sizeof(a));       memset(u, 0, sizeof(u));       for (int i = 1; i <= n; i++) {         for (int j = 1; j <= m; j++) {           cin >> c;           a[i][j] = (int)c - 48;         }         scanf( %*c );       }       ans = 0;       for (int i = 1; i <= n; i++)         for (int j = 1; j <= m; j++)           if ((a[i][j] == 1) and (u[i][j] == 0))             if (judge(i, j)) ans++;       cout << ans << endl;     }   }