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 <stdio.h>

int w, h;
int island[50][50], map[50][50], ans;

void dfs(int x, int y, int flg) {
		int i, j;
	if(x < 0 || w <= x || y < 0 || h <= y || map[x][y] == 1) {
		return;
	}
	else if(map[x][y] == 0) {
		if(island[y][x] == 0) {
			if(flg == 0) {
			 	map[x][y] = 1;
		
				for(i = -1; i <= 1; i ++) {
					for(j = -1; j <= 1; j ++) {
						dfs(x + i, y + j, 0);
					}
				}
			}
			else if(flg == 1) {
				return;
			}
		}
		else if(island[y][x] == 1) {
			map[x][y] = 1;  
					
			if(flg == 0) {
				ans ++; 
			}
			for(i = -1; i <= 1; i ++) {
				for(j = -1; j <= 1; j ++) {
					dfs(x + i, y + j, 1);
				}
			}
		}
	}
	return;
}

int main() {
	
	int i, j;
	while(scanf("%d %d", &w, &h)) {
		if(w == 0 && h == 0) { break; }
		
		ans = 0;
		for(i = 0; i < 50; i ++) {
			for(j = 0; j < 50; j ++) {
				map[i][j] = -1;
				island[i][j] = 0;
			}
		}
	
		for(i = 0; i < h; i ++) {
			for(j = 0; j < w; j ++) {
				scanf("%d", &island[i][j]);
				map[i][j] = 0;
			}
		}
		
		for(i = 0; i < h; i ++) {
			for(j = 0; j < w; j ++) {
				dfs(i, j, 0);
			}
		}
		printf("%d\n", ans);
	}

	return 0;
}

C++: #include <iostream>
#include <ios>
#include <iomanip>
#include <algorithm>
#include <string>
#include <vector>
#include <map>
#include <set>
#include <cstdlib>
#include <cstring>
#include <cmath>
using namespace std;

#define MOD 1e9+7
#define INF 1e9+7
#define rep(i, n) for(int i=0; i<n; ++i)
typedef long long ll;

#define MAX_N 100000
#define MAX_V 100
#define MAX_E 100

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

int ddx[] = {-1, 0, 1, 0, -1, 1, -1, 1};
int ddy[] = {0, -1, 0, 1, 1, 1, -1, -1};

//--------------------------

void dfs(int x, int y);

int w, h, cnt;

int c[50][50];

int main() {

  while(true) {
    cin >> w >> h;

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

    cnt = 0;

    rep(i, h) {
      rep(j, w) cin >> c[i][j];
    }

    rep(i, h) {
      rep(j, w) {
        if(c[i][j]==1) {
          dfs(i, j);
          ++cnt;
        }
      }
    }

    cout << cnt << endl;
  }

  return 0;
}

void dfs(int x, int y) {

  if(x<0||x>=h||y<0||y>=w||c[x][y]==0) {
    return;
  }

  c[x][y] = 0;

  rep(i, 8) {
    dfs(x+ddx[i], y+ddy[i]);
  }
}

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

int is[52][52];
int w, h, c;

void search(int x, int y){
	int i, j;
	if(!is[x][y]) return;
	is[x][y] = 0;
	for(i = -1; i <= 1; i++){
		for(j = -1; j <= 1; j++){
			search(x + i , y + j);
		}
	}
	return;
}

int main(void){
	int i, j;
	while(cin >> h >> w, w){
		memset(is, 0, sizeof(is));
		c = 0;
		for(i = 1; i <= w; i++)
			for(j = 1; j <= h; j++)
				cin >> is[i][j]; 

		while(1){
			for(i = 1; i <= w; i++){
				for(j = 1; j <= h; j++){
					if(is[i][j]) break;
				}
				if(is[i][j]) break;
			}
			if(i == w+1) break;
			search(i, j);
			c++;
		}
		cout << c << endl;
	}
	return 0;
}

C++: #include<queue>
#include<cstdio>

#define	mp	make_pair

using namespace std;

typedef	pair<int,int>	pii;

const int dx[]={1,1,0,-1,-1,-1,0,1},dy[]={0,-1,-1,-1,0,1,1,1};
int field[50][50];
bool visited[50][50];

int main(){
	for(int w,h;scanf("%d%d",&w,&h),w;){
		for(int i=0;i<h;i++)for(int j=0;j<w;j++){
			scanf("%d",field[i]+j);
			visited[i][j]=false;
		}

		int cnt=0;
		for(int i=0;i<h;i++)for(int j=0;j<w;j++){
			if(field[i][j]==1 && !visited[i][j]){
				visited[i][j]=true;
				queue<pii> qu;	qu.push(mp(i,j));
				while(!qu.empty()){
					pii a=qu.front();	qu.pop();
					for(int k=0;k<8;k++){
						int xx=a.second+dx[k],yy=a.first+dy[k];
						if(0<=xx && xx<w && 0<=yy && yy<h
						&& field[yy][xx]==1 && !visited[yy][xx]){
							qu.push(mp(yy,xx));
							visited[yy][xx]=true;
						}
					}
				}
				cnt++;
			}
		}

		printf("%d\n",cnt);
	}

	return 0;
}

C++: #include<iostream>
using namespace std;
int w,h,i,j,c[50][50];

void visit(int i,int j){

  if(i<0||j<0)return;
  if(i>=50||j>=50)return;

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

C++: #include "bits/stdc++.h"
using namespace std;
typedef pair<int, int> P;
int dx[8] = { -1, -1, 0, 1, 1, 1, 0, -1 };
int dy[8] = { 0, -1, -1, -1, 0, 1, 1, 1 };
void DFS(vector<vector<int>>&A, P p, int H, int W) {
	A.at(p.first).at(p.second) = 0;
	for (int i = -1; i <= 1; i++) {
		for (int j = -1; j <= 1; j++) {
			if (i == 0 && j == 0) continue;
			int nx = p.second + i;
			int ny = p.first + j;
			if (0 <= nx && nx < W && 0 <= ny && ny < H && A.at(ny).at(nx) == 1) DFS(A, make_pair(ny, nx), H, W);
		}
	}
	return;
}
int main() {
	int W, H;
	cin >> W >> H;
	while (W != 0 && H != 0) {
		vector<vector<int>>A(H, vector<int>(W));
		for (int i = 0; i < H; i++) {
			for (int j = 0; j < W; j++) {
				cin >> A.at(i).at(j);
			}
		}
		vector<vector<bool>>reach(H, vector<bool>(W, false));
		int ans = 0;
		for (int i = 0; i < H; i++) {
			for (int j = 0; j < W; j++) {
				if (A.at(i).at(j) == 1) {
					DFS(A, make_pair(i, j), H, W);
					ans++;
				}
			}
		}
		cout << ans << endl;
		cin >> W >> H;
	}
}