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 <cstdio>
#include <cstdlib>
#include <cstring>
#include <cmath>
#include <climits>
#include <cfloat>
#include <ctime>
#include <cassert>
#include <map>
#include <utility>
#include <set>
#include <iostream>
#include <memory>
#include <string>
#include <vector>
#include <algorithm>
#include <functional>
#include <sstream>
#include <complex>
#include <stack>
#include <queue>
#include <numeric>
#include <list>


using namespace std;

#ifdef _MSC_VER
#define __typeof__ decltype
template <class T> int __builtin_popcount(T n) { return n ? 1 + __builtin_popcount(n & (n - 1)) : 0; }
#endif

#define foreach(it, c) for (__typeof__((c).begin()) it=(c).begin(); it != (c).end(); ++it)
#define all(c) (c).begin(), (c).end()
#define rall(c) (c).rbegin(), (c).rend()
#define clear(arr, val) memset(arr, val, sizeof(arr))

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

template <class T> void max_swap(T& a, const T& b) { a = max(a, b); }
template <class T> void min_swap(T& a, const T& b) { a = min(a, b); }

typedef long long ll;
typedef pair<int, int> pint;

const double EPS = 1e-8;
const double PI = acos(-1.0);
const int dx[] = { 0, 1, 0, -1 };
const int dy[] = { 1, 0, -1, 0 };


bool c[64][64];
void destroy(int x, int y)
{
	if (!c[y][x])
		return;
	c[y][x] = false;
	for (int dy = -1; dy <= 1; ++dy)
		for (int dx = -1; dx <= 1; ++dx)
			destroy(x + dx, y + dy);
}
int main()
{
	int w, h;
	while (cin >> w >> h, w)
	{
		clear(c, 0);
		rep (y, h) rep (x, w)
			cin >> c[y + 1][x + 1];
		int res = 0;
		for (int y = 1; y <= h; ++y)
			for (int x = 1; x <= w; ++x)
				if (c[y][x])
					++res, destroy(x, y);
		cout << res << endl;
	}
}

C++: #include<bits/stdc++.h>
using namespace std;
int a[500][500];
void calc(int i,int j){
  if(a[i][j]==1){
    a[i][j]=2;
    calc(i+1,j);
    calc(i,j+1);
    calc(i+1,j+1);
    calc(i+1,j-1);
    calc(i-1,j);
    calc(i,j-1);
    calc(i-1,j-1);
    calc(i-1,j+1);
  }
  else return;
}

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

C++: #include <bits/stdc++.h>

using namespace std;

using ll = long long;

int w, h;
vector< vector<int> > c, island;

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

void dfs(int x, int y, int id) {

    island[y][x] = id;

    for(int i = 0; i < 8; i++) {

        int nx = x + dx[i];
        int ny = y + dy[i];

        if(nx < 0 || nx >= w || ny < 0 || ny >= h)
            continue;

        if(c[ny][nx] == 1 && island[ny][nx] != id)
            dfs(nx, ny, id);

    }

}

int main(){

    cin.tie(0);
    ios::sync_with_stdio(false);

    int ans;

    while(cin >> w >> h && w) {

        c.assign(h, vector<int>(w));
        island.assign(h, vector<int>(w, -1));
        ans = 0;

        for(int y = 0; y < h; y++)
            for(int x = 0; x < w; x++)
                cin >> c[y][x];

        for(int x = 0; x < w; x++)
            for(int y = 0; y < h; y++)
                if(c[y][x] == 1 && island[y][x] == -1) {
                    dfs(x, y, ans);
                    ans++;
                }

        cout << ans << endl;

    }
}

C++: #include <bits/stdc++.h>
#define rep(i, n) for(int i = 0; i < n; i++)
using namespace std;
typedef long long ll;

bool done[60][60];
int islands[60][60];
void dfs(int vx, int vy, int w, int h)
{
    done[vx][vy] = true;
    int dx[8] = {-1, -1, 0, 1, 1, 1, 0, -1,};
    int dy[8] = {0, 1, 1, 1, 0, -1, -1, -1,};
    rep(i, 8)
    {
        if(islands[vx + dx[i]][vy + dy[i]] == 0) continue;
        if(vx + dx[i] < 0 || vx + dx[i] >= h) continue;
        if(vy + dy[i] < 0 || vy + dy[i] >= w) continue;
        if(done[vx + dx[i]][vy + dy[i]]) continue;
        dfs(vx + dx[i], vy + dy[i], w, h);
    }
    return;
}

int main(void)
{
    while(1)
    {
        int w, h;
        cin >> w >> h;
        if(w == 0 && h == 0) break;
        rep(i, h)rep(j, w) done[i][j] = false;
        rep(i, h)rep(j, w) cin >> islands[i][j];
        int ans = 0;
        rep(i, h)rep(j, w)
        {
            if(islands[i][j] == 0) continue;
            if(done[i][j]) continue;
            ans++;
            dfs(i, j, w, h);
        }
        cout << ans << endl;
    }
    return 0;
}

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

int a[100][100]={};

void del(int x,int y){
  a[x][y]=0;
  for(int i=-1;i<=1;i++){
    for(int t=-1;t<=1;t++){
      if(a[x+i][y+t]==1){del(x+i,y+t);}
    }}
}

int main(){

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

  for(int i=1;i<=h;i++){
    for(int t=1;t<=w;t++){
      cin>>a[i][t];
    }}

  int count=0;
  for(int i=1;i<=h;i++){
    for(int t=1;t<=w;t++){
      if(a[i][t]==1){del(i,t); count++;}
    }}
  
  cout<<count<<endl;
  }
  
  return 0;
}