Go: package main

import (
	"bufio"
	"fmt"
	"os"
	"strconv"
)

func getScanner(fp *os.File) *bufio.Scanner {
	scanner := bufio.NewScanner(fp)
	scanner.Split(bufio.ScanWords)
	scanner.Buffer(make([]byte, 1000005), 1000005)
	return scanner
}

func getNextString(scanner *bufio.Scanner) string {
	scanner.Scan()
	return scanner.Text()
}

func getNextInt(scanner *bufio.Scanner) int {
	i, _ := strconv.Atoi(getNextString(scanner))
	return i
}

func getNextInt64(scanner *bufio.Scanner) int64 {
	i, _ := strconv.ParseInt(getNextString(scanner), 10, 64)
	return i
}

func getNextUint64(scanner *bufio.Scanner) uint64 {
	i, _ := strconv.ParseUint(getNextString(scanner), 10, 64)
	return i
}

func getNextFloat64(scanner *bufio.Scanner) float64 {
	i, _ := strconv.ParseFloat(getNextString(scanner), 64)
	return i
}

func main() {
	fp := os.Stdin
	wfp := os.Stdout
	cnt := 0

	if os.Getenv("MASPY") == "ますピ" {
		fp, _ = os.Open(os.Getenv("BEET_THE_HARMONY_OF_PERFECT"))
		cnt = 0
	}
	if os.Getenv("MASPYPY") == "ますピッピ" {
		wfp, _ = os.Create(os.Getenv("NGTKANA_IS_GENIUS10"))
	}

	scanner := getScanner(fp)
	writer := bufio.NewWriter(wfp)
	for solve(scanner, writer) {

	}
	for i := 0; i < cnt; i++ {
		fmt.Fprintln(writer, "-----------------------------------")
		solve(scanner, writer)
	}
	writer.Flush()
}

func solve(scanner *bufio.Scanner, writer *bufio.Writer) bool {
	sol := solver{}
	sol.w = getNextInt(scanner)
	sol.h = getNextInt(scanner)
	if sol.w == 0 && sol.h == 0 {
		return false
	}
	sol.land = makeInts(sol.h, sol.w)
	for y := 0; y < sol.h; y++ {
		for x := 0; x < sol.w; x++ {
			sol.land[y][x] = getNextInt(scanner)
		}
	}
	ans := 0
	for y := 0; y < sol.h; y++ {
		for x := 0; x < sol.w; x++ {
			if sol.land[y][x] == 0 {
				continue
			}
			ans++
			sol.dfs(y, x)
		}
	}
	fmt.Fprintln(writer, ans)
	return true
}

type solver struct {
	w, h int
	land [][]int
}

func (sol *solver) dfs(y, x int) {
	if y < 0 {
		return
	}
	if y >= sol.h {
		return
	}
	if x < 0 {
		return
	}
	if x >= sol.w {
		return
	}
	if sol.land[y][x] == 0 {
		return
	}
	sol.land[y][x] = 0
	dy := [8]int{-1, -1, -1, 0, 0, 1, 1, 1}
	dx := [8]int{-1, 0, 1, -1, 1, -1, 0, 1}
	for i := 0; i < 8; i++ {
		sol.dfs(y+dy[i], x+dx[i])
	}
}
func makeInts(h, w int) [][]int {
	index := make([][]int, h, h)
	data := make([]int, h*w, h*w)
	for i := 0; i < h; i++ {
		index[i] = data[i*w : (i+1)*w]
	}
	return index
}

C++: #include <iostream>
#include <vector>

using namespace std;

#define rep(i, n) for (int i = 0; i < n; i++)
#define vec vector

int dx[8] = {0, 0, 1, 1, 1,-1,-1,-1};
int dy[8] = {1,-1,-1, 0, 1,-1, 0, 1};

int dp[55][55];
int c[55][55];

int check(int x, int y)
{
	if (dp[x][y]) return 0;
	if (c[x][y] == 0) return 0;
	dp[x][y] = 1;
	
	rep(i, 8) {
		int xx = x + dx[i];
		int yy = y + dy[i];
		int take = check(xx, yy);
	}
	return 1;
}

int main(void)
{
	while(1) {
		int w, h;
		cin >> w >> h;
		if (w == 0) break;
		rep(i, 55) {
			rep(j, 55) c[i + 1][j + 1] = 0;
		}
		rep(i, h) rep (j, w) cin >> c[i + 1][j + 1];
		rep(i, 55) rep(j, 55) dp[i][j] = 0;
		rep(i, 55) {
			dp[0][i] = 1;
			dp[i][0] = 1;
			dp[h + 1][i] = 1;
			dp[i][w + 1] = 1;
		}

		int ans = 0;
		rep(i, h) rep(j, w) if (c[i + 1][j + 1]) ans += check(i + 1, j + 1);
		cout << ans << endl;
	}
	return 0;
}

C++: #include<iostream>
#include<queue>
#include<cstring>
using namespace std;
int x[90][90], col[90][90], h, w, c, i, j, k, l, p, q;
queue<int>Q, R;
int main() {
	while (true) {
		c = 0;
        memset(x,0,sizeof(x));
		memset(col, 0, sizeof(col));
		cin >> w >> h;
		if (h == 0) { break; }
		for (i = 0; i < h; i++) { for (j = 0; j < w; j++) { cin >> x[i][j]; } }
		for (i = 0; i < h; i++) {
			for (j = 0; j < w; j++) {
				if (x[i][j] == 1 && col[i][j] == 0) {
					c++;
					col[i][j] = c;
					Q.push(i * 100 + j);
					while (!Q.empty()) {
						while (!Q.empty()) {
							p = Q.front() / 100;
							q = Q.front() % 100;
							for (k = -1; k < 2; k++) {
								for (l = -1; l < 2; l++) {
									if (x[p + k][q + l] == 1 && col[p + k][q + l] == 0) {
										col[p + k][q + l] = c;
										R.push((p + k) * 100 + q + l);
									}
								}
							}
							Q.pop();
						}
						while (!R.empty()) {
							Q.push(R.front());
							R.pop();
						}
					}
				}
			}
		}
		cout << c << endl;
	}
}

C++: #include <iostream>
#include <vector>
using namespace std;
int d[3] = {0,1,-1};
void dfs(int x,int y,vector<vector<int> > &field){
  if((x < 0 || y < 0 || x >= field[0].size() || y >= field.size()) || field[y][x] == 0)return ;
  field[y][x] = 0;
  for(int i = -1;i <= 1;i++){
	for(int j = -1;j <= 1;j++){
	  if(i == 0 && j == 0)continue;
	  dfs(x+i,y+j,field);
	}
  }
  
}

int main(){
  int w,h;
  while(cin >> w >> h && w && h){
	vector<vector<int> > field(h,vector<int>(w,0));
	for(int i = 0;i < h;i++){
	  for(int j = 0;j < w;j++){
		cin >> field[i][j];
	  }
	}
	int ans = 0;
	for(int i = 0;i < h;i++){
	  for(int j =0;j < w;j++){
		if(field[i][j] == 1){
		  dfs(j,i,field);
		  ans++;
		}
	  }
	}
	cout << ans << endl;
  }
  return 0;
}

C++: #include<bits/stdc++.h>
using namespace std;

struct Graph{
	vector<vector<int>> fld;
	
	Graph(int h,int w) : fld( h,vector<int>(w) ){}
	
	void dfs(int x, int y){
		
		fld.at(x).at(y) = 2;
		for(int i=-1; i<=1; i++){
			for(int j=-1; j<=1; j++){
				int h = fld.size();
				int w = fld.at(0).size();
				if(x+i<0||h-1<x+i||y+j<0||w-1<y+j) continue;
				if(fld.at(x+i).at(y+j)!=1) continue;
				dfs(x+i,y+j);
			}
		}
		return;
	}
	
};

int main() {
	while(1){
		int w,h;
		cin >> w >> h;
		if(w==0&&h==0) break;
		
		Graph graph(h,w);
		for(int i=0; i<h; i++){
			for(int j=0; j<w; j++){
				int s;
				cin >> s;
				graph.fld.at(i).at(j) =  s;
			}
		}
		
		int cnt{};
		for(int i=0; i<h; i++){
			for(int j=0; j<w; j++){
				if(graph.fld.at(i).at(j)==1) {
					graph.dfs(i,j);
					cnt++;
				}
				
			}
			
		}
		cout << cnt << endl;
	}
	
}

C++: /*
AizuOnline A1160
How many islands
13/03/13 RUNTIME Error
*/
#include <stdio.h>
#include <stdlib.h>

int width = 10;
int height = 10;

char kajuen[100][101];

char search_next(int * x,int * y)
{
  int i,j;
/* printf("SEARCH =%d %d\n",*x,*y); */
  i = *x;j = *y;
  goto START;
  for(i=0;i<height;i++)
    for(j=0;j<width;j++)
      {
      START:
	//	printf("SEARCH =%d %d:",i,j);
	if(kajuen[i][j]=='1')
	  { *x=i;*y=j;
	    return(kajuen[i][j]);
	  }
      }
  return(0);
}

void paint(int p,char f,int x,int y)
{
  int i,j;

#ifdef DEBUG2
  printf("P=%d %d %c %c",x,y,p,f);
  printf("F=%c N=%c %c %c\n",kajuen[x+1][y],kajuen[x][y+1],owner[x+1][y],owner[x][y+1]);
#endif
  //owner[x][y]=p+'A';
  kajuen[x][y]='0';
  for(i=x-1;i<=x+1;i++)
    for(j=y-1;j<=y+1;j++)
      if((i >=0) && (i <height) && (j >= 0) && (j <width) && !(x==i&&y==j))
	{
	  //printf("p:%d %d %d %d",x,y,i,j);
	if(kajuen[i][j] == f)
	  paint(p,f,i,j);
	}
}

int devide_kajuen()
{
  int x,y;
  char person;
  char f;

  
  x=0;y=0;
  person = 0;
  while(f=search_next(&x,&y) != 0)
    {   
      f = kajuen[x][y];
      paint(person,f,x,y);
      person ++;

    }
  return(person);
}
void clear_table()
{
  int i,j;
  for(i=0;i<100;i++)
      for(j=0;j<101;j++)
      {
        kajuen[i][j]='\0';
      }   
}
main()
{
  int n;
  int i,j;


  while(EOF != scanf("%d %d",&width,&height) && (width || height))
    {
      clear_table();
      for(i=0;i<height;i++)
	for(j=0;j<width;j++)
	  {  scanf(" %c",&(kajuen[i][j])); 
	  }      

      n = devide_kajuen();
      printf("%d\n",n);      
    }

  return(0);
}