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>
#define rep(i,n) for(int i=0;i<n;i++)
#define Graph vector<vector<int>>
using namespace std;

void DFS(const Graph &G,int u,int v,vector<vector<bool>> &seen,vector<int> y, vector<int> x,int h,int w){//深さ優先探索。
//引数は順にグラフ、始点(注目点)、探索済みかどうか、y変位、x変位、y方向のグリッドサイズ、x方向のグリッドサイズ
  
  seen[u][v]=true;

  rep(k,8){
    int yd=u+y[k],xd=v+x[k];
    if(yd<0||h<=yd||xd<0||w<=xd) continue;//グラフの外側はスルー
    if(seen[yd][xd]) continue;//もう探索した場所はスルー
    if(G[yd][xd]==0) continue;//陸地でないならスルー
    DFS(G,yd,xd,seen,y,x,h,w);
  }
  return ;
}
 
int main() {

  while(1){

  int w,h;  cin >> w >> h ;

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

  Graph G(h,vector<int> (w));//Grid

  rep(i,h){
    rep(j,w){
    cin >> G[i][j];
    }
  }//入力終了

  vector<vector<bool>> seen(h,vector<bool>(w,false));//探索済み
  int ans=0;//島の数

  vector<int> x={1,-1,0,0,1,1,-1,-1};
  vector<int> y={0,0,1,-1,-1,1,1,-1};

  rep(i,h){
    rep(j,w){
      if(seen[i][j]) continue;
      if(G[i][j]==0) continue;
      DFS(G,i,j,seen,y,x,h,w);

      ans++;//DFS一つ終わるたびに島+1
    }
  }
    cout << ans << endl;
  
  }
  return 0;
}

C++: #include <bits/stdc++.h>
using namespace std;
???
using ll=long long;
#define int ll
???
#define FOR(i,a,b) for(int i=int(a);i<int(b);i++)
#define REP(i,b) FOR(i,0,b)
???
int read(){
	int i;
	scanf("%lld",&i);
	return i;
}
???
int land[100][100]={0};
int dy[]={-1,-1,-1,0,0,1,1,1};
int dx[]={-1,0,1,-1,1,-1,0,1};
int cnt=0;
???
void isLand(int i,int j){
    if(land[i][j]==1){
    	land[i][j] = 0;
		REP(k,8){
			int newx=j+dx[k];
			int newy=i+dy[k];
			isLand(newy,newx);
		}
    }
}
???
signed main(){
	int w,h;
	while( w=read(),h=read(),w||h){
		cnt=0;
		
		REP(i,100){
			REP(j,100){
				land[i][j]=0;
			}
		}
		FOR(i,1,w+1){
			FOR(j,1,h+1){
				land[i][j]=read();
			}
		}
		
		FOR(i,1,w+1){
			FOR(j,1,h+1){
				if(land[i][j]==1){
					cnt++;
					isLand(i,j);
				}
			}
		}
		
		cout << cnt << endl;
	}
	
}

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

// 深さ優先探索
bool seen[60][60];
int dx[8] = {1, 0, -1, 0, 1, 1, -1, -1};
int dy[9] = {0, 1, 0, -1, 1, -1, 1, -1};
int w,h;

void dfs(int current_h, int current_w){
    seen[current_h][current_w] = true;

    for(int dir=0; dir < 8; dir++){
        int next_w = current_w + dx[dir];
        int next_h = current_h + dy[dir];

        if(next_w < 0) continue;
        if(next_h < 0) continue;
        if(next_w >= w) continue;
        if(next_h >= h) continue;
        
        if(field[next_h][next_w] == 1 && !seen[next_h][next_w]){
            dfs(next_h, next_w);
        }
    }
}

int main(){
    while(true) {
        cin >> w >> h;
        memset(seen, 0, sizeof(seen));
        if( w==0 && h==0 ){
            break;
        }

        field.assign(h, vector<int>(w, 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 && !seen[i][j] ){
                    dfs(i, j);
                    count ++;
                }
            }
        }

        cout << count << endl;
    }
}

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

const int X[8] = {1, 1, 0, -1, -1, -1, 0, 1};
const int Y[8] = {0, 1, 1, 1, 0, -1, -1, -1};

int W, H;
vector<vector<int>> Field;

void dfs(int y, int x){
    Field[y][x] = 0;
    for(int i = 0; i < 8; i++){
        int NeX = x + X[i];
        int NeY = y + Y[i];
        if(NeX < 0 || W <= NeX || NeY < 0 || H <= NeY)continue;
        if(Field[NeY][NeX] == 0)continue;
        dfs(NeY, NeX);
    }
}

int main(){
    while(cin >> W >> H){
        if(W == 0 && H == 0)break;
        Field.assign(H, vector<int>(W, 0));
        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] == 0)continue;
                dfs(i, j);
                ans++;
            }
        }
        
        cout << ans << endl;
    }
    return 0;
}

C++: #include <iostream>
#include <string>
#include <vector>
#include <algorithm>
#include <stdio.h>
#include <cmath>
#include <map>
#include <queue>
using namespace std;
#define rep(i, n) for (int i = 0; i < (int)(n); i++)

int W,H;
vector<vector<int>> C;
void dfs(int v, int w) {
  C[v][w] = 0;
  for(int dh = -1; dh <= 1; dh++) {
    for(int dw = -1; dw <= 1; dw++) {
      if(v+dh < 0 || v+dh >= H || w+dw < 0 || w+dw >= W) {continue;}
      if(C[v+dh][w+dw] == 0) {continue;}
      dfs(v+dh, w+dw);
    }
  }
}


int main() {

  while(cin >> W >> H) {
    if(W == 0 && H == 0) {break;}
    C.assign(H, vector<int>(W, 0));
    rep(i,H) {
      rep(j, W) {
        //int a;
        cin >> C[i][j];
      }
    }
    int count = 0;
    rep(i,H) {
      rep(j, W) {
        if(C[i][j] == 0) {continue;}
        dfs(i,j);
        count++;
      }
    }
    cout << count << endl;
  }
}