3D Space Exploration

No attempts yetTime limit1sMemory limit128 MB

Problem

Farmer John's cows have finally blasted off from Earth and are now drifting through space in their Moocraft. To reach their kin on Io, a moon of Jupiter, they must first cross a dangerous asteroid belt.

Bessie is piloting the craft through an $N \times N \times N$ ($1 \le N \le 100$) sector of space. Every asteroid in the sector is a group of $1 \times 1 \times 1$ blocks of space-rock joined face to face. Two blocks that touch only along an edge, or only at a single corner, belong to different asteroids.

Count the number of distinct asteroids in the sector.

The sector is given as $N$ square slices stacked one behind another (front to back). Each slice is an $N \times N$ grid in which * marks a block of space-rock and . marks empty space. A block at slice $s$, row $r$, column $c$ shares a face with the blocks at $(s\pm1, r, c)$, $(s, r\pm1, c)$, and $(s, r, c\pm1)$, and blocks joined this way belong to the same asteroid.

For example, consider a $3 \times 3 \times 3$ sector. Here M marks the Moocraft's start at $(1,1,1)$ and D marks the destination at $(3,3,3)$; these markers only help orient the map and are not needed to solve the problem.

   Close slice    Middle slice     Far Slice     Assembled with overlaps
     +---+            +---+          +---+                  +---+ Far
     |M..|            |..*|          |...|                  |...|
     |.*.|            |.*.|          |.*.|                +---+.|
     |...|            |*..|          |..D|                |..*|D|
     +---+            +---+          +---+              +---+.|-+
                                                        |M..|.|
                                                        |.*.|-+
                                                        |...|
                                                  Close +---+

Visual inspection shows three asteroids, including a long one running straight through the middle of the sector. The next figure labels each block with the number of the asteroid it belongs to:

                                 +---+ Far
                                 /|...|
                                / |.1.|
                               /  |...|
                              /   +---+
                             +---+   /
                            /|..3|  /
                           / |.1.| /
                          /  |2..|/
                         /   +---+
                        +---+   /
                        |...|  /
                        |.1.| /
                        |...|/
                  Close +---+

Input

  • The first line contains a single integer $N$ ($1 \le N \le 100$).
  • The next $N^2$ lines describe the $N$ slices in order. Each slice is given as $N$ lines of exactly $N$ characters, where each character is * (a block of space-rock) or . (empty space).
  • Counting after the first line, lines $s \cdot N + 1$ through $s \cdot N + N$ (for $s = 0, 1, \dots, N-1$) form slice $s$. Within a slice the lines are its rows and the characters within a line are its columns, so the same row and column of two adjacent slices are aligned and share a face.

Output

  • Print a single integer: the number of distinct asteroids in the sector.