import sys import numpy as np from scipy.sparse.csgraph import connected_components from scipy.sparse import csr_matrix buf = sys.stdin.buffer input = sys.stdin.buffer.readline def main(): H, W = map(int, input().split()) A = (np.frombuffer(buf.read(), dtype='S1')[::2] == b'1').reshape((H, W)) F = np.arange(H * W).reshape((H, W)) ct_0 = np.count_nonzero(~A) HC = A[1:] & A[:-1] ZW = np.zeros(W, dtype=bool).reshape(1, W) frm1 = F[np.concatenate([ZW, HC], 0)] to1 = F[np.concatenate([HC, ZW], 0)] del HC, ZW WC = A[:, 1:] & A[:, :-1] ZH = np.zeros(H, dtype=bool).reshape(H, 1) frm2 = F[np.concatenate([ZH, WC], 1)] to2 = F[np.concatenate([WC, ZH], 1)] del WC, ZH del A, F frm = np.hstack((frm1, frm2)) to = np.hstack((to1, to2)) L = len(frm) matr = csr_matrix(([1] * L, (frm, to)), shape=(H * W, H * W)) p = connected_components(matr, return_labels=False) ans = p - ct_0 return ans print(main())