def check_4(x, y, field):
return (field[x - 1][y - 1] == field[x - 1][y] ==
field[x][y - 1] == field[x][y] == "#")
def check_9(x, y, field):
arr = [(-1, 0), (-1, 1), (0, -1),(0, 0), (0, 1),(1, -1), (1, 0)]
for dx, dy in arr:
if not (0 <= x + dx < len(field) and 0 <= y + dy < len(field[0])):
return False
if field[x + dx][y + dy] != "#":
return False
return True
for _ in range(int(input())):
h, w = map(int, input().split())
s = [list(input()) for _ in range(h)]
ans = 0
for i in range(1, h):
for j in range(1, w):
if check_4(i, j, s) or check_9(i, j, s):
ans += 1
s[i][j] = "."
print(ans)
"""
.##...
###...
###...
....##
...###
...##.
"""