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 loop(i,x,n) for(int i=(x);i<(n);i++)
#define all(x) (x).begin(),(x).end()
#define pb push_back
#define mp make_pair
#define fir first
#define sec second
#define ub upper_bound
#define lb lower_bound
#define int long long
using namespace std;
const int MOD=1000000007;
const int INF=1000000009;

int dx[8]={1,0,-1,0,1,-1,-1,1};
int dy[8]={0,1,0,-1,1,1,-1,-1};
int h,w;
int MAP[100][100];

int bfs(int y,int x){
  rep(i,8){
    int ny=y+dy[i];
    int nx=x+dx[i];
    if(nx>=0&&ny>=0&&nx<w&&ny<h&&MAP[ny][nx]==1){
      MAP[ny][nx]=0;
      bfs(ny,nx);
    }
  }
  return 1;
}

signed main(){
  while(cin>>w>>h,w){
    rep(i,h)rep(j,w)cin>>MAP[i][j];
    int ans=0;
    rep(i,h)rep(j,w){
      if(MAP[i][j]==1)ans+=bfs(i,j);
    }
    cout<<ans<<endl;
  }
  return 0;
}

C++: #include<iostream>
#include<cstdio>
using namespace std;
int field[52][52]={};
void umeru(int y,int x){
	field[y][x]=0;
	for(int i=-1;i<=1;i++){
		for(int j=-1;j<=1;j++){
			if(field[y+i][x+j])	umeru(y+i,x+j);
		}
	}
}
int main(){
	while(1){
		int w,h,cnt=0;
		cin >> w >> h;
		if(!w&&!h)	break;
		for(int i=1;i<=h;i++){
			for(int j=1;j<=w;j++){
				cin >> field[i][j];
			}
		}
		for(int i=1;i<=h;i++){
			for(int j=1;j<=w;j++){
				if(field[i][j]){
					cnt++;
					umeru(i,j);
				}
			}
		}
		cout << cnt << endl;
	}
}

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

using namespace std;

#define vec vector
#define rep(i, n) for (int i = 0; i < (int)n; i++)

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

int dp[55][55];
int c[55][55];

int check(int x, int y)
{
	if (dp[x][y]) return 0;
	if (c[x][y] == 0) return 0;
	dp[x][y] = 1;
	
	rep(i, 8) {
		int xx = x + dx[i];
		int yy = y + dy[i];
		int take = check(xx, yy);
	}
	return 1;
}

int main(void)
{
	while(1) {
		int w, h;
		cin >> w >> h;
		if (w == 0) break;
		rep(i, 55) rep(j, 55) c[i + 1][j + 1] = 0;
		rep(i, h) rep (j, w) cin >> c[i + 1][j + 1];
		rep(i, 55) rep(j, 55) dp[i][j] = 0;
		rep(i, 55) {
			dp[0][i] = 1;
			dp[i][0] = 1;
			dp[h + 1][i] = 1;
			dp[i][w + 1] = 1;
		}

		int ans = 0;
		rep(i, h) rep(j, w) if (c[i + 1][j + 1]) ans += check(i + 1, j + 1);
		cout << ans << endl;
	}
	return 0;
}

C++: #include <algorithm>
#include <climits>
#include <cmath>
#include <cstdio>
#include <cstring>
#include <iostream>
#include <queue>
#include <stack>
#include <string>
#include <vector>
#define INF 10000000
#define PI 3.14159265358979323846
using namespace std;
typedef long long ll;
typedef pair<int, int> p;
typedef vector<int> v;
typedef vector<int, int> v2;
typedef vector< pair<int, int> > vp;
int dx[4] = { 0, 1, 0, -1 };
int dy[4] = { 1, 0, -1, 0 };

int w, h;
int c[50][50];
int f[50][50];

bool dfs(int y, int x){
	if (c[y][x] == 0) return false;
	if (f[y][x] == 1) return false;
	
	f[y][x] = 1;
	if (y > 0) dfs(y - 1, x);
	if (x > 0) dfs(y, x - 1);
	
	if (y < h) dfs(y + 1, x);
	if (x < w) dfs(y, x + 1);

	if (y > 0 && x > 0) dfs(y - 1, x - 1);
	if (y < h && x > 0) dfs(y + 1, x - 1);
	if (y > 0 && x < w) dfs(y - 1, x + 1);
	if (y < h && x < w) dfs(y + 1, x + 1);

	return true;
}

int main(){
	ios::sync_with_stdio(false);

	while (1){
		cin >> w >>  h;
		if (w == 0 && h == 0) break;

		int ans = 0;

		for (int i = 0; i < h; i++)
			for (int j = 0; j < w; j++)
				cin >> c[i][j];
			
		for (int i = 0; i < h; i++)
			for (int j = 0; j < w; j++)
				f[i][j] = -1;
		
		for (int i = 0; i < h; i++)
			for (int j = 0; j < w; j++)
				if(dfs(i, j)) ans++;

		cout << ans << endl;

	}

	return 0;
}

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

int Count;
int w,h;
bool area[51][51]={};
int dx[8]={0,1,1,1,0,-1,-1,-1};
int dy[8]={1,1,0,-1,-1,-1,0,1};

void bfs(int x,int y){
  int X,Y;
  queue<pair<int,int> > Q;
  Q.push(make_pair(x,y));
  area[x][y]=0;
  while(!Q.empty()){
    pair<int,int> p1=Q.front();
    X=p1.first;
    Y=p1.second;
    Q.pop();
    for(int i=0;i<8;i++){
      if((X+dx[i]<=-1||Y+dy[i]<=-1)||(X+dx[i]>=w||Y+dy[i]>=h))continue;
      if(area[X+dx[i]][Y+dy[i]]==1){
        Q.push(make_pair(X+dx[i],Y+dy[i]));
        area[X+dx[i]][Y+dy[i]]=0;
      }
    }
  } 
  return;
}

int main(){
  int I;
  while(cin>>w>>h,w){
    Count=0;
    for(int i=0;i<h;i++){
      for(int j=0;j<w;j++){
        cin>>I;
        area[j][i]=I;
      }
    }

    for(int i=0;i<h;i++){
      for(int j=0;j<w;j++){
        if(area[j][i]==1){
          bfs(j,i);
          Count++;
        }
      }
    }
    cout<<Count<<endl;
  }
  return 0;
}