[Question]
  [
A simple contest, inspired by [this stackoverflow question](https://stackoverflow.com/questions/11936073/can-counting-contiguous-regions-in-a-bitmap-be-solved-in-better-than-or-c/11936456):
> 
> 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.
  
[Answer]
# 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[#/.{"."->0,"*"->1}]&
```
---
## Example
```
Max@MorphologicalComponents[#/.{"."->0,"*"->1}]&[{{".", ".", ".", ".", ".", ".", ".", ".", ".", "*", "*"}, {"*", "*", ".", ".", ".", ".", ".", ".", "*", "*", "*"}, {".", ".", ".", ".", ".", ".", ".", ".", ".", ".", "."}, {".", ".", ".", "*", ".", ".", ".", ".", ".", ".", "."}, {"*", ".", ".", ".", ".", ".", ".", ".", ".", "*", "."}, {"*", ".", ".", ".", ".", ".", ".", ".", ".", ".", "*"}}]
```
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
```
/.{"."->0,"*"->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[{{".", ".", ".", ".", ".", ".", ".", ".", ".", "*", "*"}, {"*", "*", ".", ".", ".", ".", ".", ".", "*", "*", "*"}, {".", ".", ".", ".", ".", ".", ".", ".", ".", ".", "."}, {".", ".", ".", "*", ".", ".", ".", ".", ".", ".", "."}, {"*", ".", ".", ".", ".", ".", ".", ".", ".", "*", "."}, {"*", ".", ".", ".", ".", ".", ".", ".", ".", ".", "*"}} /. {"." -> 0, "*" -> 1}]
```
[![islands](https://i.stack.imgur.com/8Gp45.png)](https://i.stack.imgur.com/8Gp45.png)
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 `1`s with an integer corresponding to the cluster number of each cell.
![processing](https://i.stack.imgur.com/nyNg4.png)
`Max` returns the largest cluster number.
---
**Displaying the Islands**
`Colorize` will color each island uniquely.
![colorize](https://i.stack.imgur.com/rRhU6.png)
[Answer]
## Ruby 1.9 (~~134~~ ~~121~~ ~~113~~ 110)
Takes the map on stdin or the file name of the map as the first command-line argument, and prints the number of islands to stdout. Using a basic recursive flood-fill. Improvements welcome as always!
```
c=0
gets$!
c+=1while(f=->i{9.times{|o|$_[i]=?.;f[o]if$_[o=i+(o/3-1)*(~/$/+1)+o%3-1]==?*&&o>0}if i})[~/\*/]
p c
```
Similar to David's colorize, you can also get it to display the different islands by changing `$_[i]=?.` to `$_[i]=c.to_s` and `p c` to `puts$_`, which would give you something like this:
```
.........00
11......000
...........
...2.......
3........4.
3.........4
```
(at least until you run out of digits!)
Some test cases:
```
.........**
**......***
...........
...*.......
*........*.
*.........*
```
5
```
......*..**....*
**...*..***....*
....*..........*
...*.*.........*
*........***....
*.....*...***...
*.....*...*....*
****..........**
*.........*.....
```
9
```
*
```
1
```
****
****
....
****
```
2
```
**********
*........*
*.******.*
*.*....*.*
*.*.**.*.*
*.*.**.*.*
*.*....*.*
*.******.*
*........*
**********
```
3
[Answer]
## C, 169 chars
Reads map from stdin. Had no luck improving the recursive flood-fill function `r(j)` although it looks like it could be.
```
c,g,x,w;char m[9999];r(j){if(m[j]==42)m[j]=c,r(j+1),r(j+w-1),r(j+w),r(j+w+1),c+=j==g;}main(){while((m[x++]=g=getchar())+1)w=g<11*!w?x:w;for(;g++<x;)r(g);printf("%i",c);}
```
[Answer]
# Python 2, ~~223~~ 203 Bytes
Thank you to [Step Hen](https://codegolf.stackexchange.com/users/65836/step-hen) and [Arnold Palmer](https://codegolf.stackexchange.com/users/72137/arnold-palmer) for shaving off 20 characters of spaces and unnecessary parenthesis!
```
s=input()
c=[(s.index(l),i)for l in s for i,v in enumerate(l)if'*'==v]
n=[set([d for d in c if-2<d[0]-v[0]<2and-2<d[1]-v[1]<2])for v in c]
f=lambda x,p=0:p if x&n[p]else f(x,p+1)
print len(set(map(f,n)))
```
I thought that using list comprehensions might decrease the number of bytes, but it didn't provide any significant improvement.
[Try it here.](https://tio.run/##VY3NbsMgEITvfoo9hZ8SK/YxCk@COFADKhLeIEMs9@ldcJRE3cNqv9HMTvotP3cc9z3LgOlRKOsmqWjuA1q30chEYP6@QISAkKGdQawNHD5mt5jiqil4womUq@5QquwKVfaw2macIPjzeLPqos9rXbfRoD2EoQlDFfTRcbyddOdlNPO3NbCJJC/XVPOwnVAl7WJ24GnVvwbWpSVggeiQtsrZJOoFMsb2XZH@NZwTQTh/QaP@M0/iH@Lv3D/qOdF/)
I keep trying to trim it around the n (neighbors) list, but I haven't been successful. Maybe someone else will have some ideas for that section.
[Answer]
# [Perl 5](https://www.perl.org/), 100 bytes
98 bytes of code + 2 bytes for `-p0` flags.
```
/.*/;$@="@+"-1;$~="(.?.?.{$@})?";(s/X$~\*/X$1X/s||s/\*$~X/X$1X/s)&&redo;s/\*/X/&&++$\&&redo}{$\|=0
```
[Try it online!](https://tio.run/nexus/perl5#TYrBDoMgGIPvPsXG/hAtkV/PhMhjcOA4b0tm5Cjy6kyzsa1Nmq9Nb9dlXh@XfhkKa7AhZ4VToh8NZStaPR3eyO3dJEwb2VMOOHL0HFOKHEDZf3on5Trfn@Zc2bOUSlF4b/tGIdmhFF0FNEBFNPqnk1EZ3/8fa7wA "Perl 5 – TIO Nexus")
An adaptation (or rather a simplification) of my [answer](https://codegolf.stackexchange.com/a/113778/55508) to the challenge [How Many Holes?](https://codegolf.stackexchange.com/questions/113764/how-many-holes). You can find explanations of how this code works on this other answer (it's a bit long to explain, so I prefer not to retype the entire explanations).
[Answer]
## Python 2, 233 bytes
Too long, compared to other answers. Port of my answer to [this question](https://codegolf.stackexchange.com/questions/113764/how-many-holes).  
[Try it online](https://tio.run/nexus/python2#nU9BboMwELz7FZaiymuzUFBvUB8QX8iByPIhIkS1FCzUJqrpoV@npklbUqGGctjR7MzOrrbPpbHt6QicVDImpTzUFnIVax4mZHPuBrqr97SANU8JddjJNaFmT3PVaeX0I4tYeuHS86zZtlDgm2nBK@IhUI2x4IIES35utw5cmGA8tKg6HPzO@xuOg9d9eZyT1ydzqJlgxtKXUwM5Ks1TI795ZOyuduAneFYFMskKAHMXvpdo7j36De2zsUda9Sul2IrhqDT@KPRKoZMzF0X3flPEkM4GMWo1UqpG8r/gM64mts6F4Z1f4VsXr29Ppm/9/HdaTIzPuh0tvS2W/r0EtP4A)
```
A=input()
c=0
X=len(A[0])-1
Y=len(A)-1
def C(T):
 x,y=T
 if A[y][x]<'.':A[y][x]='.';map(C,zip([x]*3+[min(x+1,X)]*3+[max(x-1,0)]*3,[y,min(y+1,Y),max(y-1,0)]*3))
while'*'in sum(A,[]):i=sum(A,[]).index('*');c+=1;C((i%-~X,i/-~X))
print c
```
[Answer]
## JavaScript, 158 bytes
```
function f(s){w=s.search('\n');t=s.replace(RegExp('([*@])([^]{'+w+','+(w+2)+'})?(?!\\1)[*@]'),'@$2@');return t!=s?f(t):/\*/.test(s)?f(s.replace('*','@'))+1:0}
```
Noncompeting ES6 answer (language postdates challenge) for 132 bytes:
```
f=s=>s!=(s=s.replace(RegExp(`([*@])([^]{${w=s.search`
`},${w+2}})?(?!\\1)[*@]`),`@$2@`))?f(s):/\*/.test(s)?f(s.replace(`*`,`@`))+1:0
```
Port of my answer to [How Many Holes?](https://codegolf.stackexchange.com/questions/113764/how-many-holes) (yes I'm jumping on the bandwagon, now that I've seen two other people port their answers).
[Answer]
# [Python 2](https://docs.python.org/2/), 225 bytes
```
g=map(list,input())
q,w,t,r=len(g),len(g[0]),0,range
def l(i,j):
 if 0<=i<q and 0<=j<w and g[i][j]=='1':g[i][j]=0;l(i+1,j);l(i-1,j);l(i,j+1);l(i,j-1)
 return 1
print sum(l(i,j)if g[i][j]=='1'else 0 for j in r(w)for i in r(q))
```
[Try it online!](https://tio.run/##ZY3BjoMwDETv@QqLS2PVlew9dsmXIA5IDWwimkIIQvv1LIEiVVpf/Maj8Qy/6ecVvta1M89m0L2bErkwzEkjqpEWShRNb4PukPZVcY3EFJvQWfWwLfTakce7AtcCl8aVIzThkdGXy45d5erK18Zc5HI/BX9vwats0Qy3E8hf5Q03QQXRpjkGEDVEFxJM81MfhVvb51/bTxYY2lcEDy5A1Atm4Q4xIq5rVYgIs0hBxQGZ@JhM2@105b/Le7qo/wA "Python 2 – Try It Online")
]