d = []
with open('cellpicture.txt') as f:
m,n = map(int,f.readline().split())
for i in range(m):
r = list(map(int,f.readline()[:n]))
d.append(r)
#输出矩阵
for x in d:
print(x)
#探索细胞
def explorePixel(d,m,n,i,j):
q = [(i,j)]
while q:
#先删除队列第一个值,表示已探索
x,y =q.pop(0)
if d[x][y]<0:
continue
d[x][y] = 0-d[x][y]
if x>0 and d[x-1][y]>0:
q.append((x-1,y))
if x<m-1 and d[x+1][y]>0:
q.append((x+1,y))
if y>0 and d[x][y-1]>0:
q.append((x,y-1))
if y<n-1 and d[x][y+1]>0:
q.append((x,y+1))
#细胞计数
count = 0
for i in range(m):
for j in range(n):
if d[i][j]>0:
#未探索的位置调用函数,由于d是全局变量,函数可以更改d中的值
explorePixel(d,m,n,i,j)
count += 1
#输出结果
print(count)