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 <iostream>
#include <cmath>
#include <cstring> 
#include <sstream>
#include <iostream>
#include <algorithm>
#include <cstdlib>
#include <map>
#include <queue>

using namespace std;
int w, h;
int c[100][100];
int dx[8]={-1, 0, 1, 0, -1, -1, 1, 1};
int dy[8]={0, -1, 0, 1, -1, 1, -1, 1};
int cnt=0;

void search(int x, int y);

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 >> c[i][j];
			}
		}
		for(int i  = 0; i < h; i++){
			for(int j = 0; j < w; j++){
				if(c[i][j] == 1){
					c[i][j] = 0;
					search(i, j);
					cnt++;
				}
			}
		}
		cout << cnt << endl;
		cnt = 0;
	}
}

void search(int x, int y)
{
	int nx, ny;
	for(int i = 0; i < 8; i++){
		nx = x + dx[i];
		ny = y + dy[i];
		if(nx < 0 || ny < 0 || nx >= h || ny >= w) continue;
		if(c[nx][ny] == 1){
			c[nx][ny] = 0;
			search(nx, ny);
		}
	}
}

C++: #include<bits/stdc++.h>
using namespace std;
#define rep(i,n) for(int i=0;i<(int)(n);i++)
#define all(obj) (obj).begin(),(obj).end()
typedef long long llong;
llong mod=1e9+7;

int w,h,cnt;
int a[50][50];
void dfs(int i,int j){
  if(a[i][j]==0) return;
  if(i<0||i>=w||j<0||j>=h) return;
  a[i][j]=0;
  dfs(i-1,j-1);
  dfs(i-1,j);
  dfs(i-1,j+1);
  dfs(i,j-1);
  dfs(i,j+1);
  dfs(i+1,j-1);
  dfs(i+1,j);
  dfs(i+1,j+1);
}

int main(){
  cin.tie(0);
  ios::sync_with_stdio(false);
  while(1){
    cnt=0;
    cin>>h;
    if(h=='\0') break;
    cin>>w;
    rep(i,w){
      rep(j,h){
        cin>>a[i][j];
      }
    }
    rep(i,w){
      rep(j,h){
        if(a[i][j]==1){
          dfs(i,j);
          cnt++;
        }
      }
    }
    cout<<cnt<<endl;
  }

  return 0;
  }

C++: #include <iostream>

using namespace std;

int m[51][51];
int w, h;
const int dx[] = {-1, 0, 1, -1, 1, -1, 0, 1};
const int dy[] = {-1, -1, -1, 0, 0, 1, 1, 1};

void dfs(int x, int y)
{
    m[y][x] = 0;
    for (int i = 0; i < 8; i++) {
        int nx = x + dx[i];
        int ny = y + dy[i];
        
        if (nx < 0 || ny < 0 || nx >= w || ny >= h)
            continue;
        else if (m[ny][nx] == 1)
            dfs(nx, ny);
    }
}

int main()
{
    while (cin >> w >> h, w || h) {
        int isl = 0;

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

        for (int i = 0; i < h; i++) 
            for (int j = 0; j < w; j++)
                if (m[i][j] == 1) {
                    isl++;
                    dfs(j, i);
                }

        cout << isl << endl;
    }

    return 0;
}

C++: #include <iostream>
using namespace	std;
int h,w,c[50][50];
void dfs(int y,int x){
  c[y][x]=0;
  for(int i=-1;i<=1;i++)
    for(int j=-1;j<=1;j++)
      if(0<=y+i&&y+i<h&&0<=x+j&&x+j<w)
        if(c[y+i][x+j]) dfs(y+i,x+j);
}

int main(){

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

}

C++: #include <algorithm>
#include <bitset>
#include <cmath>
#include <complex>
#include <cstdio>
#include <cstring>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <vector>

#define rep(i,a) for(int i = 0;i < (a); i++)
#define repi(i,a,b) for(int i = (a); i < (b); i++)
#define repd(i,a,b) for(int i = (a); i >= (b); i--)
#define repit(i,a) for(__typeof((a).begin()) i = (a).begin(); i != (a).end(); i++)
#define all(u) (u).begin(),(u).end()
#define rall(u) (u).rbegin(),(u).rend()
#define UNIQUE(u) (u).erase(unique(all(u)),(u).end())
#define pb push_back
#define mp make_pair
#define INF 1e9
#define EPS 1e-9
#define PI acos(-1.0)

using namespace std;

typedef long long ll;
typedef vector<int> vi;
int w, h;
int c[100][100];
bool inrange(int i, int j){
    return i >= 0 && j >= 0 && i < h && j < w;
}
int di[] = {-1, 0, 1, -1, 1, -1, 0, 1};
int dj[] = {-1,-1,-1, 0, 0, 1, 1, 1};
void rec(int i, int j){
    c[i][j] = 0;
    rep(k, 8){
        int ni = i + di[k];
        int nj = j + dj[k];
        if(inrange(ni, nj) && c[ni][nj]) rec(ni, nj);
    }
}
int main() {
    while(cin >> w >> h, w){
        rep(i, h) rep(j, w) cin >> c[i][j];
        int cnt = 0;
        rep(i, h) rep(j, w){
            if(c[i][j]){
                rec(i, j);
                cnt++;
            }
        }
        cout << cnt << endl;
    }
    return 0;
}

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

void search(vector<vector<int>>& map, int x, int y)
{
	if (map[x][y] < 0)
		return;

	if (map[x][y] == 1) {
		map[x][y] = 0;
		search(map, x - 1, y);
		search(map, x + 1, y);
		search(map, x, y - 1);
		search(map, x, y + 1);
		search(map, x - 1, y - 1);
		search(map, x + 1, y + 1); 
		search(map, x + 1, y - 1);
		search(map, x - 1, y + 1);
	}
}
int main()
{
	int w, h;
	while (1) {
		cin >> w >> h;
		if (w == 0 && h == 0)
			break;

		vector<vector<int>> map;
		map = vector<vector<int>>(h+2, vector<int>(w+2, -1));

		for (int i = 1; i < h+1; i++) {
			for (int j = 1; j < w+1; j++)
				cin >> map[i][j];
		}
		int island_n = 0;
		for (int i = 1; i < h + 1; i++) {
			for (int j = 1; j < w + 1; j++) {
				if (map[i][j] == 1) {
					island_n++;
					search(map,i, j);
				}
			}
		}
		cout << island_n << endl;
	}
	return 0;
}