# Aizu Problem 0067: The Number of Islands
#
import sys, math, os

# read input:
PYDEV = os.environ.get('PYDEV')
if PYDEV=="True":
    sys.stdin = open("sample-input.txt", "rt")


class UndirectedGraph():
    def __init__(self, grid):
        offsets = [[0, 1], [0, -1], [1, 0], [-1, 0]]
        N = len(grid)
        M = len(grid[0])
        self.edges = {}
        self.nodes = []
        for row in range(N):
            for col in range(M):
                if grid[row][col] == '1':
                    self.AddNode((row, col))
                    for row_off, col_off in offsets:
                        row2 = row + row_off
                        col2 = col + col_off
                        if 0 <= row2 < N and 0 <= col2 < M and  grid[row2][col2] == '1':
                                self.AddEdge((row, col), (row2, col2))
        
    def AddNode(self, node):
        if node not in self.nodes:
            self.nodes.append(node)
        if node not in self.edges:
            self.edges[node] = []

    def GetNodes(self):
        return self.nodes

    def GetEdges(self):
        return self.edges

    def AddEdge(self, node1, node2):
        self.AddNode(node1)
        self.AddNode(node2)
        if node1 not in self.edges[node2]:
            self.edges[node2].append(node1)
        if node2 not in self.edges[node1]:
            self.edges[node1].append(node2)

    def Degree(self, node):
        return len(self.edges[node])

    def GetNeighbors(self, node):
        return self.edges.get(node, [])
            
    def GetPath(self, source, target):
        # find path by BFS:
        predecessor = {node: -1 for node in self.GetNodes()}
        predecessor[source] = 0
        visited = {node: False for node in self.GetNodes()}
        queue = [source]
        while len(queue) > 0:
            current = queue.pop(0)
            visited[current] = True
            for neighbor in self.GetNeighbors(current):
                if not visited[neighbor] and neighbor not in queue:
                    queue.append(neighbor)
                    predecessor[neighbor] = current
                if neighbor == target:
                    queue = []
                    break
        # build path from predecessor info:
        if predecessor[target] == -1:
            return []
        path = [target]
        while path[0] != source:
            path = [predecessor[path[0]]] + path[:]
        return path

    def GetConnectedComponents(self):
        # identify connected components by BFS
        nodes = self.GetNodes()[:]
        connected_components = []
        while len(nodes) > 0:
            queue = [nodes.pop()]
            component = [queue[0]]
            while len(queue) > 0:
                current = queue.pop(0)
                for neighbor in self.GetNeighbors(current):
                    if neighbor in nodes and neighbor not in queue:
                        queue.append(neighbor)
                        nodes.remove(neighbor)
                        component.append(neighbor)
            connected_components.append(component)
        return connected_components


finished = False
while True:
    grid = []
    if finished:
        break
    while True:
        try:
            inp = input().strip()
        except EOFError:
            finished = True
            break
        if len(inp) == 0:
            break
        grid.append([_ for _ in inp])
    G = UndirectedGraph(grid)
    print(len(G.GetConnectedComponents()))

Measured memory_bytes: 7696