Go: // AOJ 1160: How Many Islands? http://judge.u-aizu.ac.jp/onlinejudge/description.jsp?id=1160

package main

import (
	"bufio"
	"container/list"
	"fmt"
	"os"
	"strconv"
)

var SEA = 0
var LAND = 1

func nextInt(sc *bufio.Scanner) int {
	sc.Scan()
	v, _ := strconv.Atoi(sc.Text())
	return v
}

func discoverIslands(M, V [][]int, H, W int) int {
	// 島を探す
	queue := list.New()
	islands := 0
	for i := 1; i <= H; i++ {
		for j := 1; j <= W; j++ {
			// 訪問済みの土地ならスキップ
			if V[i][j] == 1 {
				continue
			}

			// 訪問中の土地が海なら訪問済みにしてスキップ
			if M[i][j] == SEA {
				V[i][j] = 1
				continue
			}

			// 未訪問の陸地が見つかったので
			// 島の数を1つ増やし、この陸地から辿れる陸地を訪問していく
			islands++
			queue.PushBack([]int{i, j})

			for queue.Len() > 0 {
				cur := queue.Remove(queue.Front()).([]int)
				h, w := cur[0], cur[1]
				if V[h][w] == 1 {
					continue
				}
				V[h][w] = 1

				// 全方向に未訪問の陸地がないか探索
				for v := -1; v <= 1; v++ {
					for l := -1; l <= 1; l++ {
						dh := h + v
						dw := w + l
						if M[dh][dw] == LAND && V[dh][dw] == 0 {
							queue.PushBack([]int{dh, dw})
						}
					}
				}
				/*
					if M[h-1][w] == LAND {
						queue.PushBack([]int{h - 1, w})
					}
					if M[h-1][w+1] == LAND {
						queue.PushBack([]int{h - 1, w + 1})
					}
					if M[h][w+1] == LAND {
						queue.PushBack([]int{h, w + 1})
					}
					if M[h+1][w+1] == LAND {
						queue.PushBack([]int{h + 1, w + 1})
					}
					if M[h+1][w] == LAND {
						queue.PushBack([]int{h + 1, w})
					}
					if M[h+1][w-1] == LAND {
						queue.PushBack([]int{h + 1, w - 1})
					}
					if M[h][w-1] == LAND {
						queue.PushBack([]int{h, w - 1})
					}
					if M[h-1][w-1] == LAND {
						queue.PushBack([]int{h - 1, w - 1})
					}
				*/
			}
		}
	}
	return islands
}

func main() {
	sc := bufio.NewScanner(os.Stdin)
	sc.Split(bufio.ScanWords)

	for {
		W, H := nextInt(sc), nextInt(sc)

		if W == 0 && H == 0 {
			break
		}
		// Initialize map and visted list
		M := make([][]int, H+2)
		V := make([][]int, H+2)
		for i := 0; i < H+2; i++ {
			M[i] = make([]int, W+2)
			V[i] = make([]int, W+2)
		}
		for i := 1; i <= H; i++ {
			for j := 1; j <= W; j++ {
				M[i][j] = nextInt(sc)
			}
		}

		islands := discoverIslands(M, V, H, W)

		fmt.Println(islands)
	}
}

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);
}