#include <bits/stdc++.h>
using namespace std;
#define int long long
constexpr int N = 57, M = 11;
constexpr int inf = 1E18, mod = 1e9 + 7;
int n, m, ans, a[N][N];
bool vis1[N][N], vis2[N][N];
int dd1[8][2] = {{-1, 0}, {1, 0}, {0, -1}, {0, 1}, {1, -1}, {1, 1}, {-1, 1}, {-1, -1}};
int dd2[4][2] = {{1, 0}, {-1, 0}, {0, 1}, {0, -1}};
inline void dfs1(int x, int y) {
for (int t = 0; t < 4; t ++) {
int xx = dd2[t][0] + x, yy = dd2[t][1] + y;
if(xx < 1 || xx > n || yy < 1 || yy > m || vis2[xx][yy] || a[xx][yy] != 1) continue;
vis2[xx][yy] = true;
dfs1(xx, yy);
}
}
inline void dfs(int x, int y) {
for (int t = 0; t < 8; t ++) {
int xx = dd1[t][0] + x, yy = dd1[t][1] + y;
if(xx < 0 || xx > n + 1 || yy < 0 || yy > m + 1 || vis1[xx][yy]) continue;
vis1[xx][yy] = true;
if(a[xx][yy] == 1 && !vis2[xx][yy]) {
++ ans;
vis2[xx][yy] = true;
dfs1(xx, yy);
}else if(a[xx][yy] == 0) dfs(xx, yy);
}
}
inline void Main() {
cin >> n >> m;
for (int i = 1; i <= n; i ++) {
for (int j = 1; j <= m; j ++) {
char c; cin >> c;
a[i][j] = c - '0';
}
}
dfs(0, 0);
cout << ans << '\n';
for (int i = 0; i <= n + 1; i ++) {
for (int j = 0; j <= m + 1; j ++) {
vis1[i][j] = false;
vis2[i][j] = false;
a[i][j] = 0;
}
}
ans = 0;
}
signed main() {
cin.tie(nullptr)->sync_with_stdio(false);
int T = 1; cin >> T;
while(T --) Main();
return 0;
}