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

typedef pair<int, int> P;
int w,h;

void dfs(int x, int y, vector<vector<bool>> &maze){
    queue<P> q;
    q.push({x, y});
    maze[x][y] = false;
    while(q.size() > 0){
        int nx = q.front().first;
        int ny = q.front().second;
        q.pop();
        for (int i = -1; i <= 1; i++){
            for (int j = -1; j <= 1; j++){
                if (0 <= nx+i && nx+i < h && 0 <= ny+j && ny+j < w && maze[nx+i][ny+j]){
                    maze[nx+i][ny+j] = false;
                    q.push({nx+i, ny+j});
                }
            }
        }
    }
}

int main(){
    int x;
    int ans;
    while(1){
        cin >> w >> h;
        if (w == 0 || h == 0) break;
        vector<vector<bool>> maze(h, vector<bool>(w));
        for (int i = 0; i < h; i++){
            for (int j = 0; j < w; j++){
                cin >> x;
                maze[i][j] = x;
            }
        }
        
        ans = 0;
        for (int i = 0; i < h; i++){
            for (int j = 0; j < w; j++){
                if (maze[i][j]){
                    dfs(i, j, maze);
                    ans++;
                }
            }
        }
        cout << ans << endl;
    }
}

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

using namespace std;


#define FOR(i,a,b) for (int i=(a);i<(b);i++)
#define RFOR(i,a,b) for (int i=(b)-1;i>=(a);i--)
#define REP(i,n) for (int i=0;i<(n);i++)
#define RREP(i,n) for (int i=(n)-1;i>=0;i--)

int h,w;

int c[52][52];

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


void dfs(int i,int j);



int main(){


	while(1){
		cin >> w >> ;

		if(h==0 && w == 0){
			break;
		}

		cnt=0;

		REP(i,h){
			REP(j,w){


				cin >> c[i][j] ;

			}
		}

		REP(i,h){
			REP(j,w){

				//cnt++??????????????\???????????????????????\???????????????
				if(c[i][j] == 1){
					cnt++;
					dfs(j,i);
				}
			}
		}

		cout << cnt << endl;
	}
	return 0;
}

void dfs(int i,int j){

	if(c[j][i] == 1){

		c[j][i] = 0;
		REP(k,8){
			//w???<=???????????£??????????????????
			if(0 <= i+dx[k] && i+dx[k] < w && 0 <= j+dy[k] && j+dy[k] < h){

				dfs(i+dx[k],j+dy[k]);
			}
		}
	}
}

C++: #include <iostream>
#include <cmath>
#include <string>
#include <utility>
#include <vector>
#include <set>
#include <map>
#include <stack>
#include <queue>
#include <bitset>
#include <algorithm>
#include <functional>
#include <numeric>
#define debug(x) cerr << #x << ':' << x << endl
using namespace std;
typedef long long ll;

int dx[8]={1,1,0,-1,-1,-1,0,1}, dy[8]={0,1,1,1,0,-1,-1,-1};
void dfs(int x, int y, int Map[51][51], int h, int w){
    if(!(0<=x && x <=h-1 && 0<=y && y<=w && Map[x][y]==1)) return;
    Map[x][y]=0;
    for(int i=0; i<8; i++)
        dfs(x+dx[i], y+dy[i], Map, h, w);
}

void disp(int Map[51][51], int h, int w){
    cout << "==========" << endl;
    for(int x=0; x<=h-1; x++){
        for(int y=0; y<=w-1; y++) cout << Map[x][y] << "";
        cout << endl;
    }
    cout << "==========" << endl;
}

int main(){
    int Map[51][51]={};
    int h, w;
    while(1){
        cin >> w >> h;
        if(h == 0 && w ==0) break;
        for(int x=0; x<=h-1; x++)
            for(int y=0; y<w; y++)
                cin >> Map[x][y];
        
        int cnt=0;
        for(int x=0; x<=h-1; x++)
            for(int y=0; y<w; y++){
                // cout << "before" << endl;
                // disp(Map, h, w);
                if(Map[x][y]==1) dfs(x, y, Map, h, w), cnt++;
                // cout << "after" << endl;
                // disp(Map, h, w);
            }
                
        
        cout << cnt << endl;
    }
    return 0;
}

C++: #include <iostream>
#include <fstream>
#include <set>
#include <map>
#include <vector>
#include <algorithm>
#include <cmath>
#include <chrono>
using namespace std;

void connection(vector<vector<bool>> &checked, int i, int j, int h, int w) {
    if (!checked.at(i).at(j)) return;
    checked.at(i).at(j) = false;
    for (int x = max(0, i - 1); x <= min(h - 1, i + 1); x++) {
        for (int y = max(0, j - 1); y <= min(w - 1, j + 1); y++) {
            if (x != i || y != j) connection(checked, x, y, h, w);
        }
    }
    return;
}

int main(int argc, const char * argv[]) {
    
    ios::sync_with_stdio(false);
    cin.tie(0);
    
    while(true) {
        int w, h;
        cin >> w >> h;
        if (h == 0 && w == 0) break;
        
        vector<vector<bool>> checked(h, vector<bool>(w, false));
        for (int i = 0; i < h; i++) {
            for (int j = 0; j < w; j++) {
                bool a;
                cin >> a;
                checked.at(i).at(j) = a;
            }
        }
        
        int ans = 0;
        for (int i = 0; i < h; i++) {
            for (int j = 0; j < w; j++) {
                if (checked.at(i).at(j)) {
                    connection(checked, i, j, h, w);
                    ans++;
                }
            }
        }
        
        cout << ans << endl;
    }
    
    return 0;
}