import sys
from collections import deque


directions = [(x + y * 1j) for y in [-1, 0, 1] for x in [-1, 0, 1] if y or x]


def main():
    octopuses = {
        (x + y * 1j): int(c)
        for y, line in enumerate(sys.stdin)
        for x, c in enumerate(line.strip())
    }

    flash_counts = []

    while True:
        flashing = set()
        queue = deque()

        for a in octopuses:
            octopuses[a] += 1

            if octopuses[a] > 9:
                flashing.add(a)
                queue.append(a)

        while queue:
            a = queue.popleft()

            for direction in directions:
                b = a + direction

                if b in octopuses:
                    octopuses[b] += 1

                    if b not in flashing and octopuses[b] > 9:
                        flashing.add(b)
                        queue.append(b)

        for a in flashing:
            octopuses[a] = 0

        flash_counts.append(len(flashing))

        if len(flash_counts) == 100:
            print(sum(flash_counts))

        if len(flashing) == len(octopuses):
            break

    print(len(flash_counts))


if __name__ == "__main__":
    main()
