from collections import defaultdict
from sys import maxsize, stdin


NORTH = 0, -1
SOUTH = 0, 1
WEST = -1, 0
EAST = 1, 0

NORTHWEST = -1, -1
NORTHEAST = 1, -1
SOUTHWEST = -1, 1
SOUTHEAST = 1, 1

MAIN_DIRECTIONS = [NORTH, SOUTH, WEST, EAST]

ADJACENT_DIRECTIONS = {
    NORTH: [NORTH, NORTHEAST, NORTHWEST],
    SOUTH: [SOUTH, SOUTHEAST, SOUTHWEST],
    WEST: [WEST, NORTHWEST, SOUTHWEST],
    EAST: [EAST, NORTHEAST, SOUTHEAST],
}


def has_any_neighbor(elf, elves):
    x, y = elf

    for dx in range(-1, 2):
        for dy in range(-1, 2):
            if (dx or dy) and (x + dx, y + dy) in elves:
                return True

    return False


def is_valid_direction(elf, direction, elves):
    x, y = elf

    for dx, dy in ADJACENT_DIRECTIONS[direction]:
        if (x + dx, y + dy) in elves:
            return False

    return True


def add_vectors(a, b):
    ax, ay = a
    bx, by = b

    return ax + bx, ay + by


def main():
    elves = {(x, y) for y, l in enumerate(stdin) for x, c in enumerate(l) if c == "#"}

    for t in range(maxsize):
        proposed_moves = {}

        for elf in elves:
            if not has_any_neighbor(elf, elves):
                continue

            x, y = elf

            for i in range(4):
                direction = MAIN_DIRECTIONS[(t + i) % 4]

                if is_valid_direction(elf, direction, elves):
                    proposed_moves[elf] = add_vectors(elf, direction)
                    break

        move_counts = defaultdict(int)

        for move in proposed_moves.values():
            move_counts[move] += 1

        new_elves = set()

        for elf in elves:
            if elf in proposed_moves and move_counts[proposed_moves[elf]] == 1:
                new_elves.add(proposed_moves[elf])
            else:
                new_elves.add(elf)

        if new_elves == elves:
            print(t + 1)
            break

        elves = new_elves

        if t == 9:
            min_x = min(x for x, y in elves)
            min_y = min(y for x, y in elves)

            max_x = max(x for x, y in elves)
            max_y = max(y for x, y in elves)

            width = max_x - min_x + 1
            height = max_y - min_y + 1

            print(width * height - len(elves))


if __name__ == "__main__":
    main()
