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;
using ll=long long;
using ull=unsigned long long;
using pii=pair<int,int>;

#define INF LONG_MAX
#define MOD 1000000007
#define rng(a) a.begin(),a.end()
#define rrng(a) a.end(),a.begin()

int main(){
  ios::sync_with_stdio(false);
  cin.tie(0);

  while(true){
    int W,H;
    cin>>W>>H;
    if(H==0&&W==0)break;
    vector<vector<int>>c(H,vector<int>(W));
    for(int i=0;i<H;i++)for(int j=0;j<W;j++)cin>>c[i][j];
    queue<pii>q;
    int dx[8]={0,0,1,-1,1,-1,-1,1};
    int dy[8]={1,-1,0,0,1,-1,1,-1};
    int ans=0;
    for(int i=0;i<H;i++){
      for(int j=0;j<W;j++){
        if(c[i][j]==0)continue;
        c[i][j]=0;
        q.push({i,j});
        while(q.size()){
          int y=q.front().first;
          int x=q.front().second;
          q.pop();
          for(int k=0;k<8;k++){
            int X=x+dx[k];
            int Y=y+dy[k];
            if(0<=X&&X<W&&0<=Y&&Y<H&&c[Y][X]==1){
              c[Y][X]=0;
              q.push({Y,X});
            }
          }
        }
        ans++;
      }
    }
    cout<<ans<<endl;
  }
  return 0;
}

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

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

int map[51][51];
int w, h;

void oneIsland(int x, int y) {
	map[y][x] = 0;
	for (int i = 0; i < 8; i++) {
		int nextX = x + dx[i];
		int nextY = y + dy[i];
		if (nextX < 0 || w <= nextX) continue;
		if (nextY < 0 || w <= nextY) continue;
		if (map[nextY][nextX] == 1) {
			oneIsland(nextX, nextY);
		}
	}
}

int main() {
	while (1) {
		cin >> w >> h;
		if (w == 0 && h == 0) break;
		for (int i = 0; i < h; i++) {
			for (int j = 0; j < w; j++) {
				cin >> map[i][j];
			}
		}
		
		int count = 0;
		for (int i = 0; i < h; i++) {
			for (int j = 0; j < w; j++) {
				if (map[i][j] == 1) {
					oneIsland(j, i);
					count++;
				}
			}
		}
		cout << count <<endl;
	}
}

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

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

int map[51][51];
int w, h;

void oneIsland(int x, int y) {
	for (int i = 0; i < 8; i++) {
		int nextX = x + dx[i];
		int nextY = y + dy[i];
		if ((0 <= nextX && nextX < w)
			&& (0 <= nextY && nextY < h)) {
			if (map[nextY][nextX] == 1) {
				map[nextY][nextX] = 0;
				oneIsland(nextX, nextY);
			}
		}
	}
}

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

C++: #include <iostream>

using namespace std;

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

int main(){
  int i,j,ans;
  cin >> w >> h;
  while(1){
    ans=0;
    if(w+h==0)break;
    for(i=0;i<50;i++)for(j=0;j<50;j++)c[i][j]=0;
    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]){
        dfs(i,j);
        ans++;
      }
    }
    cout << ans << endl;
    cin >> w >> h;
  }
  return 0;
}

C++: #include<iostream>
#include<string>
#include<cstdio>
#include<algorithm>
#include<stack>
#include<queue>
#include<vector>
#include<cmath>
#include<utility>
#include<set>
#include<complex>
#define vi vector<int>
#define vvi vector<vector<int> >
#define ll long long int
#define vl vector<ll>
#define vvl vector<vector<ll>>
#define vb vector<bool>
#define vc vector<char>
#define vs vector<string>
#define ld long double
#define INF 1e9
#define EPS 0.0000000001
#define rep(i,n) for(int i=0;i<n;i++)
#define CC puts("-------ok--------");
#define all(in) in.begin(), in.end()
#define bv vector<bool>
using namespace std;
typedef pair<int, int> PI;
#define MAX 99999999
vvi v(60,vi(60,0));
int w,h;
int mx[]={-1,0,1};
int my[]={-1,0,1};
void dfs(int y,int x){
        if (!v[y][x])return;
        if(v[y][x]){
            v[y][x]=0;
            rep(p,3)rep(q,3){
                int tx=x+mx[p]; int ty=y+my[q];
                if(0<=tx&&tx<w&&0<=ty&&ty<h&&v[ty][tx]!=0){
                    dfs(ty,tx);
                }
            }
        }
    }
int main(){
    while(cin>>w>>h,w+h){
        rep(i,h)rep(j,w)cin>>v[i][j];
        int ans=0;
        rep(i,h)rep(j,w){
            if(v[i][j]){dfs(i,j);
            ans++;
            }
        }
        cout<<ans<<endl;
        rep(i,60)rep(j,60)v[i][j]=0;
    }
}

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

int w, h;
int data[99][99]; //y,x

int solve(int x, int y){

  if(x < 0 || y < 0 || x == w || y == h) return 0;
  if(!data[y][x]) return 0;

  data[y][x] = 0;
  for(int i=-1;i<2;i++) for(int j=-1;j<2;j++) solve(x+j, y+i);

  return 1;
}

int main(){

  while(true){
    cin >> w >> h;
    if(!w && !h) break;

    for(int i=0;i<h;i++) for(int j=0;j<w;j++) cin >> data[i][j];

    int ret = 0;
    for(int i=0;i<h;i++) for(int j=0;j<w;j++) ret += solve(j, i);

    cout << ret << endl;
  }

}