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

int h,w;
bool grid[100][100];

void dfs(int y,int x){
  grid[y][x] = 0;
  int dx[] = {0,1,1,1,0,-1,-1,-1};
  int dy[] = {-1,-1,0,1,1,1,0,-1};

  for(int i=0;i<8;i++){
    int ty = y+dy[i], tx = x+dx[i];
    if(ty<0 || tx<0 || h<=ty || w<=tx)continue;
    if(grid[ty][tx])dfs(ty,tx);
  }
}

int main(){
  while(cin >> w >> h ,w||h){
    for(int i=0;i<h;i++)
      for(int j=0;j<w;j++)cin >> grid[i][j];

    int ans = 0;
    for(int i=0;i<h;i++)
      for(int j=0;j<w;j++)
	if(grid[i][j]){
	  dfs(i,j);
	  ans++;
	}

    cout << ans << endl;
  }
}

C++: #include <iostream>

using namespace std;

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

int sink(int x, int y) {
    if (map_[x][y] == 0) {
        return 0;
    }
    map_[x][y] = 0;
    for (int i=0; i<8; i++) {
        int x_new = x+dx[i];
        int y_new = y+dy[i];
        if (0 <= x_new && x_new < w && 0 <= y_new && y_new < h) {
            sink(x_new, y_new);
        }
    }
    return 1;
}

int main() {
    while (true) {
        int count = 0;
        cin >> w >> h;
        if (w == 0) {
            break;
        }
        for (int i=0; i<h; i++) {
            for (int j=0; j<w; j++) {
                cin >> map_[j][i];
            }
        }
        for (int i=0; i<h; i++) {
            for (int j=0; j<w; j++) {
                count += sink(j, i);
            }
        }
        cout << count << endl;
    }
    return 0;
}

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

int w,h;
int dx[8] = {0,1,1,1,0,-1,-1,-1};
int dy[8] = {1,1,0,-1,-1,-1,0,1};
vector<vector<int>> mp;

void dfs(int y, int x){
  mp[y][x] = 0;
  for (int i = 0; i < 8; i++)
  {
    if(x+dx[i]>=w || x+dx[i]<0 || y+dy[i]>=h || y+dy[i]<0) continue;
    if(mp[y+dy[i]][x+dx[i]]) dfs(y+dy[i], x+dx[i]);
  }
}


int main(){
  vector<int> anss;
  while(true){
    cin >> w >> h;
    if(w+h==0) break;
    mp = vector<vector<int>>(h,vector<int>(w));
    for (int i = 0; i < h; i++)
    {
      for (int j = 0; j < w; j++)
      {
        cin >> mp[i][j];
      }    
    }

    int ans = 0;
    for (int i = 0; i < h; i++)
    {
      for (int j = 0; j < w; j++)
      {
        if(mp[i][j]){
          ans++;
          dfs(i,j);
        }
      }
    }
    anss.push_back(ans);
  }
  for(int ans:anss) cout << ans << endl;
}

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

int w, h, ans, tmp;
vector<vector<int>> c(60, vector<int>(60,0));

void ume(int x, int y){
	if(c[x][y]==0) return;
	c[x][y]=0;
	ume(x-1,y);
	ume(x+1,y);
	ume(x-1,y-1);
	ume(x,y-1);
	ume(x+1,y-1);
	ume(x-1,y+1);
	ume(x,y+1);
	ume(x+1,y+1);
	return;
}

bool hantei(int x, int y){
	if(c[x][y]==1){
		ume(x,y);
		return 1;
	}
	else return 0;
}

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

C++: #include <iostream>
#include <math.h>
#include <algorithm>
#include <string>
#include <stack>
#include <queue>
#include <set>
#include <cstdio>
#include <string.h>
#include <cstring>
using namespace std;

int w,h;
int land[50][50];
int isLands;
int d1[] = {0,0,-1,1,-1,1,-1,1};
int d2[] = {-1,1,0,0,1,1,-1,-1};

void dfs(int i, int j,int h, int w){
    
    if(i >= h || j >= w) return;
    if(i < 0 || j < 0) return;
    if(land[i][j] == 0) return;
    if(land[i][j] == 1) {
        land[i][j] = 0;
        for(int k = 0; k < 8; k++){
            dfs(i + d1[k], j + d2[k], h , w);
        }
    }

}

int main() {
    while(true){
        cin >> w >> h;
        if(w == 0 || h == 0) break;
        isLands = 0;
        for(int i = 0;i < h; i++){
            for(int j = 0; j< w; j++){
                cin >> land[i][j];
            }
        }
        for(int i = 0;i < h; i++){
            for(int j = 0; j< w; j++){
                if(land[i][j] == 1){
                    isLands++;
                    dfs(i,j,h,w);
                }
            }
        }
        cout << isLands << endl;
    }
    return 0;
}

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

using namespace std;

int res, land[1080][1080];
int xs[] = {1, 1, 0, -1, -1, -1, 0, 1};
int ys[] = {0, 1, 1, 1, 0, -1, -1, -1};
void dfs(int x, int y){
	land[x][y] = 0;
	for(int i = 0;i < 8;i++){
		int tx = x + xs[i];
		int ty = y + ys[i];
		if(land[tx][ty])dfs(tx, ty);
	}
}

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