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 <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] = {0};

void dfs(int x, int y){
	field[y][x] = 3;

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

int main(){
	while(1){
		cin >> w >> h;
		if(w==0 and h==0){
			return 0;
		}
		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){
					dfs(i,j);
					count++;
				}
			}
		}
		cout << count << endl;
	}
}

C++: #include <stdio.h>
#include <string.h>
#define rep(i, n) for (int i =  0; i < (int)(n); i++)

int n, m, f[64][64];

void rec(int x, int y) {
    if (f[x][y] == 0) return ;
    f[x][y] = 0;
    rep (i, 3) rep (j, 3) rec(x+i-1, y+j-1);
}

int main() {
    for (;;) {
        scanf("%d%d", &m, &n);
        if (n == 0) return 0;
        memset(f, 0, sizeof(f));
        rep (i, n) rep (j, m) scanf("%d", f[i+1]+j+1);
        int c = 0;
        rep (i, n) rep (j, m) if (f[i+1][j+1]) c++, rec(i+1, j+1);
        printf("%d\n", c);
    }
}

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

int H,W;

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

bool seen[51][51];
int board[51][51];

  void dfs(int h, int w) {
  seen[h][w] = true; 
  
  for (int dir = 0; dir < 8; ++dir) {
    int nw = w + dx[dir];
    int nh = h + dy[dir];  
    if (nw < 0 || nw >= W || nh < 0 || nh >= H) continue;
    if (seen[nh][nw]) continue; 
    if (board[nh][nw] == 1) dfs(nh,nw);
  }
} 

int main() {  
  cin >> W >> H;
  while (W >= 1 || H >= 1) {
    int ans =0;
    for (int i =0; i<H; i++) {
      for (int j =0; j<W; j++) {
        int tmp; cin >> tmp;
        board[i][j] = tmp;
      }
    }
    
    for (int i =0; i<H; i++) {
      for (int j =0; j<W; j++) {
        seen[i][j] = false;
      }
    }
  
    for (int i =0; i<H; i++) {
      for (int j =0; j<W; j++) {
        if (seen[i][j] == true || board[i][j] == 0) continue;
        dfs(i,j);
        ans++;
      }
    } 
    cout << ans << endl; 
    cin >> W >> H;
    for (int i =0; i<H; i++) {
      for (int j =0; j<W; j++) {
        board[i][j] = 0;
      }
    }
  }
}

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

int W, H;

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

void solve(int cx, int cy, vector<vector<int>>& v) {
    v[cx][cy] = 0;
    for(int i=0; i<8; ++i) {
        int nx = cx + dx[i];
        int ny = cy + dy[i];
        if(nx >= 0 && nx < W && ny >= 0 && ny < H && v[nx][ny] == 1) {
            solve(nx, ny, v);
        }
    }
}


int main() {
    while(cin >> W >> H, W) {
        vector<vector<int>> v(W, vector<int>(H));
        for(int i=0; i<H; ++i) {
            for(int j=0; j<W; ++j) {
                cin >> v[j][i]; // 0: sea, 1:island
            }
        }
        int ans=0;
        for(int i=0; i<W; ++i) {
            for(int j=0; j<H; ++j) {
                if(v[i][j] == 1) {
                    solve(i, j, v);
                    ++ans;
                }
            }
        }
        cout << ans << endl;
    }
}

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

//#define int long long
//struct Fast {Fast(){cin.tie(0);ios::sync_with_stdio(0);}} fast;
//const int MOD = 1e9 + 7;
using intpair = pair<int, int>;

#define rep(i, n) for (int i = 0; i < (int)(n); i++)
#define reps(i, n, m) for (int i = (int)(n); i <= (int)(m); i++)
#define ALL(obj) (obj).begin(), (obj).end()
#define rALL(obj) (obj).rbegin(), (obj).rend()
#define ceiv(a, b) ((int)((a) + (b) - 1)/(int)(b))

int H, W;
bool is_safe(int x, int y){
    return (0 <= x && x < W)&&(0 <= y && y < H);
}

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

void solve(){
    bool field[H][W], visited[H][W];
    rep(i,H)rep(j,W) visited[i][j] = false;
    rep(i,H)rep(j,W) cin >> field[i][j];
    
    int ans = 0;
    
    rep(y,H)rep(x,W){
        if(visited[y][x] || !field[y][x]) continue;
        stack<intpair> s;
        s.push({x,y});
        ans++;
        while(!s.empty()){
            int x2 = s.top().first, y2 = s.top().second;
            visited[y2][x2] = true;
            s.pop();
            rep(i,8){
                if(!is_safe(x2+dx[i], y2+dy[i])) continue;
                if(!field[y2+dy[i]][x2+dx[i]])   continue;
                if(visited[y2+dy[i]][x2+dx[i]])  continue;
                s.push({x2+dx[i],y2+dy[i]});
            }
        }
    }
    cout << ans << endl;
}

signed main(){
    while(true){
        cin >> W >> H;
        if(H==0 && W==0) break;
        solve();
    }
}