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>
#include <queue>
#include <stack>
#include <string>
#include <functional>
#include <algorithm>
using namespace std;

typedef long long ll;
typedef pair<int, int> P;

#define rep(i,a) for(int i=0;i<(a);++i)
#define debug(x) cerr << #x << " = " << (x) << " (L" << __LINE__ << ")" << endl;

int w, h;
int field[51][51];
int dx[] = { -1, -1, -1, 0, 1, 1, 1,  0};
int dy[] = { -1,  0,  1, 1, 1, 0, -1, -1};

bool infield(int x, int y)
{
    return x >= 1 && x <= w && y >= 1 && y <= h;
}

int bfs(int sx, int sy)
{
    queue<P> que;
    que.push(P(sx, sy));
    while(!que.empty()) {
        P p = que.front(); que.pop();
        int x = p.first, y = p.second;
        if(!infield(x, y) || field[y][x] == 0)
            continue;

        field[y][x] = 0;
        for(int i = 0; i < 8; i++) {
            int nx = x + dx[i], ny = y + dy[i];
            que.push(P(nx, ny));
        }
    }
}

int main()
{
    while(cin >> w >> h && w && h) {
        for(int i = 1; i <= h; i++) 
            for(int j = 1; j <= w; j++) 
                cin >> field[i][j];

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

        cout << ans << endl;
    }
}

C++: #include <iostream>
#include <string>
#include <algorithm>
#include <vector>
#include <cmath>
#include <cstdlib>
#include <tuple>
using namespace std;
using ll=long long;
using R=double;

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};
int field[50][50];

void dfs(int w, int h){
	field[h][w] = 0;

	for(int nh=h-1;nh<h+2;nh++){
	    for(int nw=w-1;nw<w+2;nw++){
	      if(field[nh][nw]==1&&0<=nh&&0<=nw&&nh<h&&nw<w)dfs(nw,nh);
	    }
	  }
	return;
}

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

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

		int count = 0;

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

C++: #include <iostream>
using namespace std;
int w,h;
int in[100][100];
bool seen[100][100];
int ans =0;
bool flag = false;

void solve(int i,int j){
    //cout << i << " " << j << endl;
    if(i < 0 || j < 0 || i >= h || j >= w){
        return;
    }
    if(seen[i][j]){
        return;
    }
    if(in[i][j] == 0){
        return;
    }
    seen[i][j] = true;
    solve(i-1,j-1);
    solve(i-1,j);
    solve(i-1,j+1);
    solve(i,j-1);
    solve(i,j+1);
    solve(i+1,j-1);
    solve(i+1,j);
    solve(i+1,j+1);

}
int main(){
    cin >> w >> h;
    for(int i=0;i<h;i++){
        for(int j=0;j<w;j++){
            cin >> in[i][j];
        }
    }
    for(int i=0;i<h;i++){
        for(int j=0;j<w;j++){
            if(seen[i][j] == true || in[i][j] == 0){
            }
            else{
                solve(i,j);
                ans++;
            }
            
        }
    }
    cout << ans << endl;
    return 0;
}

C++: #include <bits/stdc++.h>
using namespace std;
#define int long long
#define rep(i, n) for(int i = 0; i < n; i++)
#define all(a) a.begin(), a.end()
#define P pair<int, int>
#define F first
#define S second
#define prime 1000000007
#define INF 40000000000000000
int dx8[8] = {-1, 0, 1, 1, 1, 0, -1, -1};
int dy8[8] = {1, 1, 1, 0, -1, -1, -1, 0};
int dx4[4] = {-1, 0, 1, 0};
int dy4[4] = {0, -1, 0, 1};
bool Grid[51][51];
int w, h;
void dfs(int x, int y) {
    //cout << x << ':' << y << "HaHaHaHa" << endl;
    rep(i, 8) {
        int nx = x + dx8[i];
        int ny = y + dy8[i];
        //cout << x << ':' << dx8[i] << ' ' << y << ':' << dy8[i] << ' ' << nx << ':' << ny << endl;
        if(nx < 0 || nx >= h || ny < 0 || ny >= w) continue;
        if(Grid[nx][ny]) {
            Grid[nx][ny] = false;
            dfs(nx, ny);
        }
    }
}
signed main(void) {
    while(true) {
        cin >> w >> h;
        if(w == 0 && h == 0) return 0;
        rep(i, 51) rep(j, 51) Grid[i][j] = false;
        rep(i, h) rep(j, w) {
            int x;
            cin >> x;
            if(x == 1) Grid[i][j] = true;
        }
        int ans = 0;
        rep(i, h) rep(j, w) {
            if(Grid[i][j]) {
                ans++;
                Grid[i][j] = false;
                dfs(i, j);
            }
        }
        cout << ans << endl;
    }
}

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

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

void dfs(int y, int x)
{
	f[y][x] = 0;
	for(int i=0; i<8; i++)
	{
		int ny = y+dy[i];
		int nx = x+dx[i];
		if(f[ny][nx])
		{
			dfs(ny,nx);
		}
	}
}

int main()
{
	int w,h,c,x,y;
	while(cin >> w >> h, w||h)
	{
		memset(f,0,sizeof(f));

		for(y=1; y<=h; y++)
		for(x=1; x<=w; x++)
		{
			cin >> f[y][x];
		}

		c=0;
		for(y=1; y<=h; y++)
		for(x=1; x<=w; x++)
		{
			if(f[y][x])
			{
				c++;
				dfs(y,x);
			}
		}
		cout << c << endl;
	}
}