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 <iostream>
using namespace std;

const int W = 50, H = 50;
int w, h;
int board[H][W];

void search(int y, int x){
	board[y][x] = 0;
	for(int dx = -1; dx <= 1;++dx){
		for(int dy = -1; dy <= 1;++dy){
			int nx = x + dx, ny = y + dy;
			if(0 <= ny && 0 <= nx && nx < w && ny < h){
				if(board[ny][nx]) search(ny, nx);
			}
		}
	}
	return;
}

int main(){
	int c;
	while(cin >> w >> h, w){
		c = 0;
		for(int i = 0;i < h;++i){
			for(int j = 0;j < w;++j){
				cin >> board[i][j];
			}
		}
		for(int i = 0;i < h;++i){
			for(int j = 0;j < w;++j){
				if(board[i][j]){
					search(i, j);
					++c;
				}
			}
		}
		cout << c << endl;
	}
	return 0;
}

C++: #include <cstdio>

int map[50][50];
int w, h;
int ans;
int xx, yy;
const int dx[8] = {0, 1, 1, 1, 0, -1, -1, -1}, dy[8] = {-1, -1, 0, 1, 1, 1, 0, -1};

void Walk(int X, int Y) {
	map[Y][X] = 0;
	for(int i = 0; i < (sizeof(dx) / sizeof(dx[0])); i++) {
		xx = X + dx[i];
		yy = Y + dy[i];
		if(0 <= xx && xx < w && 0 <= yy && yy < h && map[yy][xx] == 1) {
			Walk(xx, yy);
		}
	}
}

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

C++: #include <bits/stdc++.h>
using ll = long long;
using namespace std;
#define rep(i,n) for(int i=0, i##_len=(int)(n); i<i##_len; i++)
#define reps(i,n) for(int i=1 , i##_len=(int)(n);i<=i##_len;i++)
#define rrep(i,n) for(int i=((int)(n)-1);i>=0;i--)
#define rreps(i,n) for(int i=((int)(n));i>0;i--)
#define all(x) (x).begin(), (x).end()
#define F first
#define S second
#define mp make_pair
#define pb push_back
#define solve(a) ((a)?"Yes":"No")
typedef vector<long long> V;
typedef vector<V> VV;
typedef pair<long long , long long> P;
typedef vector<P> VP;
template<class T> inline bool chmax(T& a, T b) { if (a < b) { a = b; return 1;} return 0;}
template<class T> inline bool chmin(T& a, T b) { if (a > b) { a = b; return 1;} return 0;}
const long long INFLL = 1LL<<62;
const int INF = 1<<30;
const double PI=acos(-1);
int ans = 0;


void func(int h,int w,vector<vector<int>>&path,vector<vector<bool>>&has){
   
    
    //clog<<h<<" "<<w<<endl;
    if(path[h][w]==1 and has[h][w]==false and h<52 and w<52){
        path[h][w] = 0;
        has[h][w] = true;
        func(h+1,w,path,has);
        func(h + 1, w + 1, path, has);
        func(h, w + 1, path, has);
        if(h>0){
            func(h - 1, w, path, has);
            func(h - 1, w + 1, path, has);
        }
        if(w>0){
            func(h, w - 1, path, has);
            func(h + 1, w - 1, path, has);
        }
        if(h>0 and w>0){
            func(h - 1, w - 1, path, has);
        }
    }
}
int main()
{
    int h, w;

    while (1)
    {
        vector<vector<int>> path(60, vector<int>(60, 0));
        vector<vector<bool>> has(60, vector<bool>(60, false));

        ans = 0;

        rep(i, 59){
            rep(j, 59){
                path[i][j] = 0;
                has[i][j] = false;
            }
        }
        cin >> h >> w;
        if (h == 0 and w == 0)
        {
            break;
        }

        rep(i, w)
        {
            rep(j, h)
            {
                cin >> path[i][j];
            }
        }
        rep(i, w)
        {
            rep(j, h)
            {
                if (has[i][j] == false)
                {
                 //   clog<<i<<" "<<j<< " -> "<<path[i][j]<<endl;
                    if (path[i][j] == 1)
                    {
                        ans++;
                       
                    }
                    func(i, j, path, has);
                }
            }
        }
     //   clog<<'f'<<endl;
        cout << ans << endl;
    }
}

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

#define W 50
#define H 50

using namespace std;
void dfs(int w, int h);
int map[H][W];
int dy[8] = {1,1,0,-1,-1,-1,0,1}, dx[8] = {0,-1,-1,-1,0,1,1,1};
int w,h;

int main(){
  int i = 0,j = 0,is=0;
  cin >> w >> h;

  while(w != 0 && h != 0){
    for(i = 0;i < h;i++){
      for(j = 0;j<w;j++){
	cin>>map[i][j];
      }
    }
    i = j = 0;
    is = 0;
    while(i <= h-1  && j <= w-1){
      if(map[i][j] == 1){
	dfs(i,j);
	is++;
      }
      i++;
      if(i == h){
	i = 0;
	j++;
      }
    }
    cout << is << endl;
    cin >> w >> h;
  }
  return 0;
}

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

C++: #include<iostream>
using namespace std;
int map[114][514];
int h, w;
void dfs(int i, int j) {
	map[i][j] = 0;
	if (i > 0) {
		if (j > 0) {
			if (map[i - 1][j - 1] == 1)dfs(i - 1, j - 1);
		}
		if(map[i - 1][j] = 1)dfs(i - 1, j);
		if (j < w) {
			if (map[i - 1][j + 1] == 1)dfs(i - 1, j + 1);
		}
	}
	if (j > 0) {
		if (map[i][j - 1] == 1)dfs(i, j - 1);
	}
	if (j < w) {
		if (map[i][j + 1] == 1)dfs(i, j + 1);
	}
	if (i < h) {
		if (j > 0) {
			if (map[i + 1][j - 1] == 1)dfs(i + 1, j - 1);
		}
		if (map[i + 1][j] = 1)dfs(i + 1, j);
		if (j < w) {
			if (map[i + 1][j + 1] == 1)dfs(i + 1, j + 1);
		}
	}
	return;
}
int main() {
	while (cin >> h >> w) {
		if (!h && !w)return 0;
		int INF = 0;
		for (int i = 0; i < h; i++)for (int j = 0; j < w; j++)cin >> map[i][j];
		for (int i = 0; i < h; i++)for (int j = 0; j < w; j++)if (map[i][j] == 1) {
			INF++;
			dfs(i, j);
		}
		cout << INF << endl;
	}
}