def solve(H, W, data):
DOT = 100
BLOCK = 101
def paint(pos, color):
data[pos] = color
for d in [-WIDTH, -1, +1, +WIDTH]:
if data[pos + d] == DOT:
paint(pos + d, color)
color = 0
for pos in allPosition():
if data[pos] == DOT:
paint(pos, color)
color += 1
if color >= 5:
return 0
ret = 0
for pos in allPosition():
if data[pos] != BLOCK:
continue
color_exists = [0] * 4
for d in [-WIDTH, -1, +1, +WIDTH]:
c = data[pos + d]
if c < 4:
color_exists[c] = 1
if sum(color_exists) == color:
ret += 1
return ret