Code-Golf: Count Islands A simple contest, inspired by this stackoverflow question:


  You are given an image of a surface photographed by a satellite.The image is a bitmap where water is marked by '.' and land is marked by '*'. Groups of adjacent '*'s form an island. (Two '*' are adjacent if they are horizontal, vertical or diagonal neighbours). Your task is to print the number of islands in the bitmap.


A single * also counts as an island.

Sample Input:

.........**
**......***
...........
...*.......
*........*.
*.........*


Sample Output:

5


Winner is entry with smallest number of bytes in the code.
Mathematica 188 185 170 115 130 46 48 chars
Explanation
In earlier versions, I made a graph of positions having a chessboard distance of 1 from each other.  GraphComponents then revealed the number of islands, one per component.
The present version uses MorphologicalComponents to find and number clusters of ones in the array--regions where 1's are physically contiguous. Because graphing is unnecessary, this results in a huge economy of code.

Code
Max@MorphologicalComponents[#/.{&quot;.&quot;-&gt;0,&quot;*&quot;-&gt;1}]&amp;


Example
Max@MorphologicalComponents[#/.{&quot;.&quot;-&gt;0,&quot;*&quot;-&gt;1}]&amp;[{{&quot;.&quot;, &quot;.&quot;, &quot;.&quot;, &quot;.&quot;, &quot;.&quot;, &quot;.&quot;, &quot;.&quot;, &quot;.&quot;, &quot;.&quot;, &quot;*&quot;, &quot;*&quot;}, {&quot;*&quot;, &quot;*&quot;, &quot;.&quot;, &quot;.&quot;, &quot;.&quot;, &quot;.&quot;, &quot;.&quot;, &quot;.&quot;, &quot;*&quot;, &quot;*&quot;, &quot;*&quot;}, {&quot;.&quot;, &quot;.&quot;, &quot;.&quot;, &quot;.&quot;, &quot;.&quot;, &quot;.&quot;, &quot;.&quot;, &quot;.&quot;, &quot;.&quot;, &quot;.&quot;, &quot;.&quot;}, {&quot;.&quot;, &quot;.&quot;, &quot;.&quot;, &quot;*&quot;, &quot;.&quot;, &quot;.&quot;, &quot;.&quot;, &quot;.&quot;, &quot;.&quot;, &quot;.&quot;, &quot;.&quot;}, {&quot;*&quot;, &quot;.&quot;, &quot;.&quot;, &quot;.&quot;, &quot;.&quot;, &quot;.&quot;, &quot;.&quot;, &quot;.&quot;, &quot;.&quot;, &quot;*&quot;, &quot;.&quot;}, {&quot;*&quot;, &quot;.&quot;, &quot;.&quot;, &quot;.&quot;, &quot;.&quot;, &quot;.&quot;, &quot;.&quot;, &quot;.&quot;, &quot;.&quot;, &quot;.&quot;, &quot;*&quot;}}]

5

How it works
Data are input as an array; in Mathematica, this is a list of lists.
In the input array, data are converted to 1's and 0's by the replacement
/.{&quot;.&quot;-&gt;0,&quot;*&quot;-&gt;1}

where /. is an infix form of ReplaceAll followed by replacement rules. This essentially converts the array into a black and white image.  All we need to do is apply the function, Image.
Image[{{&quot;.&quot;, &quot;.&quot;, &quot;.&quot;, &quot;.&quot;, &quot;.&quot;, &quot;.&quot;, &quot;.&quot;, &quot;.&quot;, &quot;.&quot;, &quot;*&quot;, &quot;*&quot;}, {&quot;*&quot;, &quot;*&quot;, &quot;.&quot;, &quot;.&quot;, &quot;.&quot;, &quot;.&quot;, &quot;.&quot;, &quot;.&quot;, &quot;*&quot;, &quot;*&quot;, &quot;*&quot;}, {&quot;.&quot;, &quot;.&quot;, &quot;.&quot;, &quot;.&quot;, &quot;.&quot;, &quot;.&quot;, &quot;.&quot;, &quot;.&quot;, &quot;.&quot;, &quot;.&quot;, &quot;.&quot;}, {&quot;.&quot;, &quot;.&quot;, &quot;.&quot;, &quot;*&quot;, &quot;.&quot;, &quot;.&quot;, &quot;.&quot;, &quot;.&quot;, &quot;.&quot;, &quot;.&quot;, &quot;.&quot;}, {&quot;*&quot;, &quot;.&quot;, &quot;.&quot;, &quot;.&quot;, &quot;.&quot;, &quot;.&quot;, &quot;.&quot;, &quot;.&quot;, &quot;.&quot;, &quot;*&quot;, &quot;.&quot;}, {&quot;*&quot;, &quot;.&quot;, &quot;.&quot;, &quot;.&quot;, &quot;.&quot;, &quot;.&quot;, &quot;.&quot;, &quot;.&quot;, &quot;.&quot;, &quot;.&quot;, &quot;*&quot;}} /. {&quot;.&quot; -&gt; 0, &quot;*&quot; -&gt; 1}]


The white squares correspond to the cells having the value, 1.
The picture below shows a some steps the approach uses. The input matrix contains only 1's and 0's.   The output matrix labels each morphological cluster with a number.  (I wrapped both the input and output matrices in MatrixForm to highlight their two dimensional structure.)
MorphologicalComponents replaces 1s with an integer corresponding to the cluster number of each cell.

Max returns the largest cluster number.

Displaying the Islands
Colorize will color each island uniquely.