#include <bits/stdc++.h>
using namespace std;
char M[500][500];
int sx, sy;
int dx[] = {0, 1, 0, -1, 1, 1, -1, -1};
int dy[] = {1, 0, -1, 0, 1, -1, 1, -1};
int main() {
  int t;
  scanf("%d", &t);
  for (int I = 0; I < t; I++) {
    int ret = 0;
    memset(M, 0, sizeof M);
    scanf("%d%d", &sy, &sx);
    for (int i = 0; i < sy; i++) {
      scanf("%s", &M[i]);
    }
    for (int i = 0; i < sy; i++) {
      for (int j = 0; j < sx; j++) {
        if (M[i][j] == '1') {
          if ((!j || M[i][j - 1] == '0') && (!i || M[i - 1][j] == '0')) {
            int len = 0, x = j;
            while (x < sx && M[i][x] == '1') {
              len++;
              x++;
            }
            if (len == 1) goto bad;
            for (int y = i; y < i + len; y++) {
              if (y == sy || M[y][j] == '0' || M[y][j + len - 1] == '0')
                goto bad;
            }
            for (int x = j; x < j + len; x++) {
              if (M[i + len - 1][x] == '0') goto bad;
            }
            for (int x = j - 1; x <= j + len; x++) {
              if ((i && x >= 0 && x < sx && M[i - 1][x] == '1') ||
                  (i + len != sy && x >= 0 && x < sx && M[i + len][x] == '1'))
                goto bad;
            }
            for (int x = i - 1; x <= i + len; x++) {
              if ((j && x >= 0 && x < sy && M[x][j - 1] == '1') ||
                  (j + len != sx && x >= 0 && x < sy && M[x][j + len] == '1'))
                goto bad;
            }
            for (int x = j + 1; x < j + len - 1; x++) {
              if (M[i + 1][x] == '1' || M[i + len - 2][x] == '1') goto bad;
            }
            for (int x = i + 1; x < i + len - 1; x++) {
              if (M[x][j + 1] == '1' || M[x][j + len - 2] == '1') goto bad;
            }
            ret++;
          }
        bad:;
          if ((!j || M[i][j - 1] == '0') && (!i || M[i - 1][j] == '0')) {
            int x = j, y = i, len = 0;
            while (x != sx && y != sy && M[y][x] == '1') {
              for (int t = 0; t < 4; t++) {
                if (x + dx[t] != sx && y + dy[t] != sy && x + dx[t] != -1 &&
                    y + dy[t] != -1 && M[y + dy[t]][x + dx[t]] == '1')
                  goto fin;
              }
              x++;
              y++;
              len++;
            }
            if (len <= 1) goto fin;
            len--;
            for (int k = 0; k <= len; k++) {
              x = j + k;
              y = i + 2 * len - k;
              if (M[y][x] == '0') goto fin;
              if (y >= sy || x >= sx) goto fin;
              for (int t = 0; t < 4; t++) {
                if (x + dx[t] != sx && y + dy[t] != sy && x + dx[t] != -1 &&
                    y + dy[t] != -1 && M[y + dy[t]][x + dx[t]] == '1')
                  goto fin;
              }
              int t = 4;
              if (x + dx[t] != sx && y + dy[t] != sy && x + dx[t] != -1 &&
                  y + dy[t] != -1 && M[y + dy[t]][x + dx[t]] == '1')
                goto fin;
              t = 7;
              if (k && k != len && x + dx[t] != sx && y + dy[t] != sy &&
                  x + dx[t] != -1 && y + dy[t] != -1 &&
                  M[y + dy[t]][x + dx[t]] == '1')
                goto fin;
              x = j - k;
              y = i + k;
              if (M[y][x] == '0') goto fin;
              for (int t = 0; t < 4; t++) {
                if (x + dx[t] != sx && y + dy[t] != sy && x + dx[t] != -1 &&
                    y + dy[t] != -1 && M[y + dy[t]][x + dx[t]] == '1')
                  goto fin;
              }
              t = 7;
              if (x + dx[t] != sx && y + dy[t] != sy && x + dx[t] != -1 &&
                  y + dy[t] != -1 && M[y + dy[t]][x + dx[t]] == '1')
                goto fin;
              t = 4;
              if (k && k != len && x + dx[t] != sx && y + dy[t] != sy &&
                  x + dx[t] != -1 && y + dy[t] != -1 &&
                  M[y + dy[t]][x + dx[t]] == '1')
                goto fin;
              x = j - k;
              y = i + 2 * len - k;
              if (M[y][x] == '0') goto fin;
              for (int t = 0; t < 4; t++) {
                if (x + dx[t] != sx && y + dy[t] != sy && x + dx[t] != -1 &&
                    y + dy[t] != -1 && M[y + dy[t]][x + dx[t]] == '1')
                  goto fin;
              }
              if (x < 0) goto fin;
              t = 6;
              if (x + dx[t] != sx && y + dy[t] != sy && x + dx[t] != -1 &&
                  y + dy[t] != -1 && M[y + dy[t]][x + dx[t]] == '1')
                goto fin;
              t = 5;
              if (k && k != len && x + dx[t] != sx && y + dy[t] != sy &&
                  x + dx[t] != -1 && y + dy[t] != -1 &&
                  M[y + dy[t]][x + dx[t]] == '1')
                goto fin;
              x = j + k;
              y = i + k;
              if (M[y][x] == '0') goto fin;
              for (int t = 0; t < 4; t++) {
                if (x + dx[t] != sx && y + dy[t] != sy && x + dx[t] != -1 &&
                    y + dy[t] != -1 && M[y + dy[t]][x + dx[t]] == '1')
                  goto fin;
              }
              t = 5;
              if (x + dx[t] != sx && y + dy[t] != sy && x + dx[t] != -1 &&
                  y + dy[t] != -1 && M[y + dy[t]][x + dx[t]] == '1')
                goto fin;
              t = 6;
              if (k && k != len && x + dx[t] != sx && y + dy[t] != sy &&
                  x + dx[t] != -1 && y + dy[t] != -1 &&
                  M[y + dy[t]][x + dx[t]] == '1')
                goto fin;
            }
            ret++;
          }
        }
      fin:;
      }
    }
    printf("%d\n", ret);
  }
  return 0;
}
