Under construction

Still being built. Things may change

Space Exploration

Interview

Time limit1sMemory limit128 MB

Summary
Count the connected components of asterisk cells in an N x N grid, where cells connect only along shared edges, not corners.
Level

Medium4 of 10

Topics
Graph, DFS, BFS, Matrix
Solved
No attempts yet

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×NN \times N (1≤N≤10001 \le N \le 1000) sector of space. The asteroids in this sector are made up of some number of 1×11 \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×1010 \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 NN.
  • Lines 2 to N+1N+1: line i+1i+1 contains row ii of the asteroid field as NN characters.

Output

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

Examples1

  1. Example 1

    Input
    10
    ...**.....
    .*........
    ......*...
    ...*..*...
    ..*****...
    ...*......
    ....***...
    .*..***...
    .....*...*
    ..*.......
    
    Expected output
    7