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<queue>
using namespace std;

struct mapst {
	int x, y;
};

#define func(x,y) (c[y][x]&&!f[y][x])

int main() {
	int w, h, ans=0;
	int c[52][52]={{0}}, f[52][52]={{1}};
	int dx[8] = {-1, -1, 0, 1, 1, 1, 0, -1};
	int dy[8] = {0, -1, -1, -1, 0, 1, 1, 1};
	queue<mapst> q;
	while(1) {
		cin >> w >> h;
		if(!w&&!h) break;
		ans = 0;
		for(int i=1; i<=h; ++i) for(int j=1; j<=w; ++j) {
			cin >> c[i][j];
			f[i][j] = 0;
		}
		for(int i=1; i<=h; ++i) for(int j=1; j<=w; ++j) {
			if(func(j,i)) {
				mapst m = {j, i}, ms;
				q.push(m);
				ans++;
				do {
					m = q.front();
					q.pop();
					f[m.y][m.x] = 1;
					for(int k=0; k<8; k++) {
						if(func(m.x+dx[k],m.y+dy[k])) {
							ms.x = m.x+dx[k]; ms.y = m.y+dy[k];
							q.push(ms);
						}
					}
				}while(!q.empty());
			}
		}
		cout << ans << endl;
	}
}

C++: #include "bits/stdc++.h"
#define rep(i,n) for(int i=0;i<n;i++)
using namespace std;

using ll = long long;

int w, h, c[52][52];

void dfs(int i, int j) {
    c[i][j] = 0;
    for (int k = i - 1; k < i + 2; k++) {
        for (int l = j - 1; l < j + 2; l++) {
            if (c[k][l] == 1) dfs(k, l);
        }
    }
}

int main() {
    cin >> w >> h;
    while (w) {
        rep(i, 52) rep(j, 52) c[i][j] = 0;
        rep(i, h) rep(j, w) cin >> c[i + 1][j + 1];
        int ans = 0;
        rep(i, h + 2) rep(j, w + 2) {
            if (c[i][j] == 1) {
                dfs(i, j);
                ans++;
            }
        }
        cout << ans << "\n";
        cin >> w >> h;
    }
}

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

using namespace std;

int W, H;
int C[50][50];

void DFS(int h, int w)
{
    if (w < 0 || w >= W) return;
    if (h < 0 || h >= H) return;
    if (C[h][w] == 0) return;
    C[h][w] = 0;

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

    for (int i = 0; i < 8; ++i) {
        DFS(h+dx[i], w+dy[i]);
    }
}

int main()
{
    while (1) {
        cin >> W >> H;
        if (W == 0 && H == 0) {
            break;
        }

        for (int h = 0; h < H; ++h) {
            for (int w = 0; w < W; ++w) {
                cin >> C[h][w];
            }
        }

        int Res = 0;
        for (int h = 0; h < H; ++h) {
            for (int w = 0; w < W; ++w) {
                if (C[h][w] == 1) {
                    DFS(h, w);
                    ++Res;
                }
            }
        }
        cout << Res << endl;
    }

    return 0;
}

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


int main(void){

	int w, h;

	while (cin >> w >> h, w, h){
		int c[100][100] = { 0 };
		vector<bool> island;
		island.push_back(false);

		for (int j = 1; j <= h; j++){
			for (int i = 1; i <= w; i++){
				cin >> c[j][i];
			}
		}

		int count = 1;
		for (int j = 1; j <= h; j++){
			for (int i = 1; i <= w; i++){
				if (c[j][i] != 0){

					if (c[j][i - 1] != 0) c[j][i] = c[j][i - 1];
					else if (c[j - 1][i + 1] != 0) c[j][i] = c[j - 1][i + 1];
					else if (c[j - 1][i] != 0) c[j][i] = c[j - 1][i];
					else if (c[j - 1][i - 1] != 0) c[j][i] = c[j - 1][i - 1];
					else{
						c[j][i] = count;
						count++;
						island.push_back(true);
					}

					bool buff = island[c[j][i]];

					island[c[j][i - 1]] = island[c[j - 1][i + 1]] = island[c[j - 1][i]] = island[c[j - 1][i - 1]] = false;
					island[c[j][i]] = buff && true;

				}
			}
		}

		int result = 0;
		vector<bool>::iterator it;
		for (it = island.begin(); it != island.end(); ++it) 	if (*it == true) result++;
		
		cout << result << endl;

	}

	return 0;
}

/*
for(i=0;i<N;i++){
cin >> X[i];
}


for(i=0;i<N;i++){
for(j=0;j<N;j++){
cin >> X[i][j];
}
}
*/

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

void exploreIsland( bool c[50][50], int h, int w, int y, int x )
{
    c[y][x] = 0;

    if( y - 1 >= 0 && c[y-1][x] == 1 )
        exploreIsland( c, h, w, y - 1, x ); 
    if( x - 1 >= 0 && y - 1 >= 0 && c[y-1][x-1] == 1 )
        exploreIsland( c, h, w, y - 1, x - 1 ); 
    if( x + 1 < w && y - 1 >= 0 && c[y-1][x+1] == 1 )
        exploreIsland( c, h, w, y - 1, x + 1 );
    if( x - 1 >= 0 && c[y][x-1] == 1 )
        exploreIsland( c, h, w, y, x - 1 ); 
    if( x + 1 < w && c[y][x+1] == 1 )
        exploreIsland( c, h, w, y, x + 1 );
    if( y + 1 < h && c[y+1][x] == 1 )
        exploreIsland( c, h, w, y + 1, x ); 
    if( x - 1 >= 0 && y + 1 < h && c[y+1][x-1] == 1 )
        exploreIsland( c, h, w, y + 1, x - 1 ); 
    if( x + 1 < w && y + 1 < h && c[y+1][x+1] == 1 )
        exploreIsland( c, h, w, y + 1, x + 1 ); 
}

int main()
{
    int h, w;
    bool c[50][50];

    while( cin >> w >> h )
    {
        if( h == 0 && w == 0 )
            break;
        
        for( int i = 0; i < h; i++ )
            for( int j = 0; j < w; j++ )
                cin >> c[i][j];
    
        int cnt = 0;
        for( int i = 0; i < h; i++ )
            for( int j = 0; j < w; j++ )
                if( c[i][j] == 1 )
                {
                    exploreIsland( c, h, w, i, j );
                    cnt++;
                }
        cout << cnt << endl;
    }
    return 0;
}

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

int w, h;
int field[60][60];

void dfs(int x, int y) {
  field[y][x] = 0;
  for(int i = -1; i <= 1; i++) {
    for(int j = -1; j <= 1; j++) {
      int tx = x+j, ty = y+i;
      if(0 <= tx && tx < w && 0 <= ty && ty < h && field[ty][tx] == 1) dfs(tx, ty);
    }
  }
}

int main() {
  while(cin >> w >> h, w+h) {
    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]) {
	  ans++;
	  dfs(j, i);
	}
      }
    }
    cout << ans << endl;
  }
}