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 <bits/stdc++.h>
//#include <iostream>
using namespace std;

static const int MAX_H = 50;
static const int MAX_W = 50;

int H, W;
int grid[MAX_H][MAX_W + 1];
bool reached[MAX_H][MAX_W];

#define OCTAS
#ifdef OCTAS
static const int dh[]{-1, -1, -1, 0, 0, +1, +1, +1};
static const int dw[]{-1, 0, +1, -1, +1, -1, 0, +1};
#else
static const int dh[]{-1, 0, 0, +1};
static const int dw[]{0, -1, +1, 0};
#endif

void initialize() {
//  memset(reached, 0, sizeof(reached));
  fill_n(reached[0], MAX_H * MAX_W, false);
}

void dfs(int h, int w) {
  reached[h][w] = true;

  for (int i{}; i < sizeof(dh) / sizeof(int); ++i) {
    int nh = h + dh[i];
    if (nh < 0 || nh >= H)
      continue;
    int nw = w + dw[i];
    if (nw < 0 || nw >= W)
      continue;
    if (grid[nh][nw] == 0 || reached[nh][nw])
      continue;
    dfs(nh, nw);
  }
}

int main() {
  while (true) {
    scanf("%d%d", &W, &H);
    if (H == 0 && W == 0)
      break;
    initialize();
    for (int h = 0; h < H; ++h) {
      for (int w = 0; w < W; ++w) {
        scanf("%d", &grid[h][w]);
      }
    }

    int ans{};
    for (int h = 0; h < H; ++h) {
      for (int w = 0; w < W; ++w) {
        if (grid[h][w] == 1 && !reached[h][w]) {
          dfs(h, w);
          ++ans;
        }
      }
    }
    printf("%d\n", ans);
  }
  return 0;
}

C++: #include<bits//stdc++.h>
using namespace std;
int sum;
void solve(bool a[50][50],int h, int w, int i, int j){
	for(int z=-1;z<2;z++){
		for(int x=-1;x<2;x++){
			if(i+z>=0&&i+z<h&&j+x>=0&&x+z<w&&a[i+z][j+x]){
				a[i+z][j+x]=false;
				solve(a,h,w,i+z,j+x);
			}
		}
	}
}
int main(){
	int h,w;
	while(cin>>w>>h&&h!=0&&w!=0){
		bool a[50][50];
		for(int i=0;i<h;i++){
			for(int j=0;j<w;j++){
				int x;
				cin>>x;
				if(x==0) a[i][j]=false;
				else a[i][j]=true;
			}
		}
		sum=0;
		for(int i=0;i<h;i++){
			for(int j=0;j<w;j++){
				if(a[i][j]){
					sum++;
					solve(a,h,w,i,j);
				}
			}
		}
		cout<<sum<<endl;
	}
}

C++: #include <iostream>
#include <stdlib.h>

using namespace std;

int d[8][2] = {
	{-1, -1},
	{0, -1},
	{1, -1},
	{-1, 0},
	{1, 0},
	{-1, 1},
	{0, 1},
	{1, 1}
};

void func(int **a, int s, int t, int i, int j)
{
	if(a[i][j] == 0){
		return ;
	}
	a[i][j] = 0;
	
	for(int k = 0; k < 8; k++){
		if((i+d[k][0] >= 0 && i+d[k][0] < t) && (j+d[k][1] >= 0 && j+d[k][1] < s)){
			func(a, s, t, i+d[k][0], j+d[k][1]);
		}
	}
	
	return ;
}

int main(int argc, char **argv)
{
	int s, t, **a, c, t2, s2;
	
	while(1){
		cin >> s >> t;
		if(s == 0 && t == 0){
			break;
		} else if(s == 1 || t == 1){
			t2 = t+1;
			s2 = s+1;
		} else {
			t2 = t;
			s2 = s;
		}
		a = (int **)calloc(t2, sizeof(int));
		for(int i = 0; i < t; i++){
			a[i] = (int *)calloc(s2, sizeof(int));
			for(int j = 0; j < s; j++){
				cin >> a[i][j];
			}
		}
		c = 0;
		
		for(int i = 0; i < t; i++){
			for(int j = 0; j < s; j++){
				if(a[i][j] == 1){
					c++;
					func(a, s, t, i, j);
				}
			}
		}
		cout << c << endl;
		
		for(int i = 0; i < s2; i++){
			free(a[i]);
		}
		free(a);
	}
	
	return 0;
}

C++: #include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef unsigned long long ull;

int w, h;
int m[50][50];

void dfs(int y, int x){
    m[y][x] = 0;
    for(int dy = -1; dy <= 1; dy++){
        for(int dx = -1; dx <= 1; dx++){
            int ny = y + dy;
            int nx = x + dx;
            if(ny >= 0 && ny < h && nx >= 0 && nx < w &&
               m[ny][nx] == 1)
                dfs(ny, nx);
        }
    }
    return;
}

int main(){
    while(1){
        cin >> w >> h;
        int ans = 0;
        if(w == 0 && h == 0) break;

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

C++: #include <iostream>
using namespace std;
int w, h, m[50][50], dx[8] = {-1, 0, 1, -1, 1, -1, 0, 1}, dy[8] = {-1, -1, -1, 0, 0, 1, 1, 1};
void dfs(int x, int y)
{
	int i, nx, ny;
	m[y][x] = 0;
	for (i = 0; i < 8; i++)
	{
		if (0 <= (nx = x+dx[i]) && nx < w && 0 <= (ny = y+dy[i]) && ny < h)
			if (m[ny][nx])
				dfs(nx, ny);
	}
}
int main()
{
	int n, i, j;
	while (cin >> w >> h)
	{
		if (w == 0 && h == 0) break;
		for (i = 0; i < h; i++)
			for (j = 0; j < w; j++)
				cin >> m[i][j];
		for (i = n = 0; i < h; i++)
			for (j = 0; j < w; j++)
				if (m[i][j])
					n++, dfs(j, i);
		cout << n << endl;
	}
}

C++: #include <iostream>
using namespace std;


int w, h;
int ans;
int map[50][50];
int dx[8] = { 0, 0, 1, 1, 1, -1, -1, -1 };
int dy[8] = { 1, -1, 1, 0, -1, 1, 0, -1 };

void solve(int x, int y)
{
	map[x][y] = 0;
	for (int i = 0; i < 8; i++)
	{
		int X = x + dx[i];
		int Y = y + dy[i];
		if (0 <= X && X < w && 0 <= Y && Y < h && map[X][Y] == 1)
		{
			solve(X, Y);
		}
	}
}

int main()
{
	while (cin >> w >> h, w + h)
	{
		ans = 0;
		for (int y = 0; y < h; y++)
		{
			for (int x = 0; x < w; x++)
			{
				cin >> map[x][y];
			}
		}

		for (int y = 0; y < h; y++)
		{
			for (int x = 0; x < w; x++)
			{
				if (map[x][y] == 1)
				{
					ans++;
					solve(x, y);
				}
			}
		}

		cout << ans << endl;
	}

	return 0;
}