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>
#include <string>
#include <vector>

using namespace std;

void del(int x, int y);

vector< vector<int> > arr;
int w, h;

int main(){
	
	while(cin >> w >> h, w | h){
		int ans = 0;
		arr.resize(h);
		for(int i = 0; i < h; i++){
			arr[i].resize(w);
		}
		
		for(int i = 0; i < h; i++){
			for(int j = 0; j < w; j++){
				cin >> arr[i][j];
			}
		}
		
		for(int i = 0; i < h; i++){
			for(int j = 0; j < w; j++){
				if(arr[i][j] == 1){
					del(i, j);
					ans++;
				}
			}
		}
				
		
		cout << ans << endl;
		arr.clear();
	}
	
	return 0;
	
}

void del(int x, int y){
	arr[x][y] = 0;	
	
	if(x + 1 < h && arr[x + 1][y] == 1){
		del(x + 1, y);
	}
	if(y + 1 < w && arr[x][y + 1] == 1){
		del(x, y + 1);
	}
	if(x - 1 > -1 && arr[x - 1][y] == 1){
		del(x - 1, y);
	}
	if(y - 1 > -1 && arr[x][y - 1] == 1){
		del(x, y - 1);
	}
	if(x + 1 < h && y + 1 < w && arr[x + 1][y + 1] == 1){
		del(x + 1, y + 1);
	}
	if(x - 1 > -1 && y + 1 < w && arr[x - 1][y + 1] == 1){
		del(x - 1, y + 1);
	}
	if(x - 1 > -1 && y - 1 > -1 && arr[x - 1][y - 1] == 1){
		del(x - 1, y - 1);
	}
	if(x + 1 < h && y - 1 > -1 && arr[x + 1][y - 1] == 1){
		del(x + 1, y - 1);
	}
	
	return;
}

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

int field[52][52];
int vw[8] = {-1,0,1,-1,1,-1,0,1};
int vh[8] = {-1,-1,-1,0,0,1,1,1};
void search(int i,int j)
{
	field[i][j] = 0;
	for(int k=0;k<8;k++){
		if(field[i+vw[k]][j+vh[k]] == 1)
			search(i+vw[k],j+vh[k]);
	}
}

int main()
{
	int w,h;
	while(cin >> w >> h){
		if(w == 0 && h == 0)
			break;
		for(int i=0;i<52;i++){
			for(int j=0;j<52;j++){
				field[i][j] = 0;
			}
		}
		for(int i=1;i<=h;i++){
			for(int j=1;j<=w;j++){
				cin >> field[i][j];
			}
		}

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

C++: #include<cstdio>
using namespace std;
void func(int,int);
int a,b;
int sima[51][51];
int cnt;
int main(void)
{
	int i,j;
	scanf("%d",&a);
	scanf("%d",&b);
	for(i=0;i<a;i++){
		for(j=0;j<b;j++){
			scanf("%d",&sima[i][j]);
		}
	}
	for(i=0;i<a;i++){
		for(j=0;j<b;j++){
			if(sima[i][j]==1){
				func(i,j);
				cnt++;
			}
		}
	}
	printf("%d\n",cnt);
	return 0;
}
void func(int y,int x)
{
	if(y-1>=0){
		if(sima[y-1][x]==1){
			func(y-1,x);
		}
	}
	if(x-1>=0){
		if(sima[y][x-1]==1){
			func(y,x-1);
		}
	}
	if(y+1<=a){
		if(sima[y+1][x]==1){
			func(y+1,x);
		}
	}
	if(x+1<=b){
		if(sima[y][x+1]==1){
			func(y,x+1);
		}
	}
	if(y-1>=0 && x+1<=b){
		if(sima[y-1][x+1]==1){
			func(y-1,x+1);
		}
	}
	if(y-1>=0 && x-1>=0){
		if(sima[y-1][x-1]==1){
			func(y-1,x-1);
		}
	}
	if(y+1<=a && x+1<=b){
		if(sima[y+1][x+1]){
			func(y+1,x+1);
		}
	}
	if(y+1<=a && x-1>=0){
		if(sima[y+1][x-1]){
			func(y+1,x-1);
		}
	}
}

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

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

void Dfs(int i, int j) {
  c[i][j] = 0;
  for (int di = -1; di <= 1; ++di) {
    for (int dj = -1; dj <= 1; ++dj) {
      if (di == 0 && dj == 0)
        continue;

      int ni = i + di, nj = j + dj;
      if (ni < 0 || ni >= h || nj < 0 || nj >= w) continue;
      if (c[ni][nj]) Dfs(ni, nj);
    }
  }
}

int main() {
  while (true) {
    cin >> w >> h;

    if (w == 0 && h == 0)
      break;

    c.resize(h, vector<int>(w));
    for (int i = 0; i < h; ++i)
      for (int j = 0; j < w; ++j)
        cin >> c[i][j];

    int ans = 0;
    for (int i = 0; i < h; ++i) {
      for (int j = 0; j < w; ++j) {
        if (c[i][j]) {
          Dfs(i, j);
          ++ans;
        }
      }
    }
    c.clear();

    cout << ans << endl;
  }
}

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

using namespace std;

typedef long long ll;
typedef vector<int> VI;

#define FOR(i,n) for(int (i)=0;(i)<(n);(i)++)
#define FOR1(i,n) for(int (i)=1;(i)<(n);(i)++)
#define eFOR(i,n) for(int (i)=0;(i)<=(n);(i)++)
#define eFOR1(i,n) for(int (i)=1;(i)<=(n);(i)++)
#define SORT(i) sort((i).begin(),(i).end())
constexpr auto INF = 1000000000;
constexpr auto LLINF = 9223372036854775807;
constexpr auto mod = 1000000007;

int w, h, c[50][50];
int dx[8] = { -1,0,1,-1,1,-1,0,1 }, dy[8] = { -1,-1,-1,0,0,1,1,1 };
void search(int x, int y) {
	if (x >= 0 && x < w && y >= 0 && y < h) {
		if (c[y][x] == 1) {
			c[y][x] = 0;
			FOR(k, 8)search(x + dx[k], y + dy[k]);
		}
	}
}

int main() {
	while (1) {
		cin >> w >> h;
		if (w == 0 && h == 0)break;
		FOR(i, h)FOR(j, w)cin >> c[i][j];
		int cnt = 0;
		FOR(i, h)FOR(j, w)if (c[i][j] == 1) {
			search(j, i);
			cnt++;
		}
		cout <<"   "<< cnt << endl;
	}
	

	return 0;
}

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

#define int long long

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

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

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

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

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

        cout << ans << endl;
    }
    return 0;
}