import itertools, collections class UnionFind(object): def __init__(self, h, w, field): self.parents = {(x, y) : () for x, y in itertools.product(range(h), range(w)) if field[x][y] == 1} def find(self, x, y): child = (x, y) while True: parent = self.parents[child] if parent: child = parent else: return child def union(self, x1, y1, x2, y2): parent1 = self.find(x1, y1) parent2 = self.find(x2, y2) if parent1 != parent2: self.parents[parent1] = parent2 def count(self): count = 0 for parent in self.parents.values(): if not parent: count += 1 return count if __name__ == '__main__': h, w = map(int, input().split()) field = tuple(tuple(map(int, input().split())) for _ in range(h)) uf = UnionFind(h, w, field) diffs = ((1, 0), (0, 1)) for x1, y1 in itertools.product(range(h), range(w)): if field[x1][y1] == 0: continue for dx, dy in diffs: x2, y2 = x1 + dx, y1 + dy print(x1, y1, x2, y2) if 0 <= x2 < h and 0 <= y2 < w and field[x2][y2] == 1: uf.union(x1, y1, x2, y2) print(uf.count())