Space Exploration

No attempts yetTime limit1sMemory limit128 MB

Problem

Farmer John's cows have finally blasted off from Earth and are now floating through space in their Moocraft. The cows want to reach their fiery kin on Jupiter's moon Io, but to get there they must first navigate the dangerous asteroid belt.

Bessie is piloting the craft through this treacherous $N \times N$ ($1 \le N \le 1000$) sector of space. The asteroids in this sector are made up of some number of $1 \times 1$ squares of space rock connected along their edges; squares that touch only at a corner count as two distinct asteroids. Help Bessie navigate the field by counting the number of distinct asteroids in the entire sector.

Consider the $10 \times 10$ sector shown on the left below. Each * is a chunk of asteroid rock and each . is empty space. The diagram on the right assigns an arbitrary number to each asteroid.

...**.....    ...11.....
.*........    .2........
......*...    ......3...
...*..*...    ...3..3...
..*****...    ..33333...
...*......    ...3......
....***...    ....444...
.*..***...    .5..444...
.....*...*    .....4...6
..*.......    ..7.......

You can see there are 7 distinct asteroids in this sector.

Input

  • Line 1: a single integer $N$.
  • Lines 2 to $N+1$: line $i+1$ contains row $i$ of the asteroid field as $N$ characters.

Output

  • Line 1: a single integer, the number of distinct asteroids in the field.