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;

int w, h;
int maps[50][50];

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

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

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

    int cnt = 0;
    for(int i = 0; i < h; i++)
      for(int j = 0; j < w; j++)
	if(maps[i][j] == 1)
	  cnt++, dfs(j, i);
    
    cout << cnt << endl;
  }
  return 0;
}

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

using namespace std;

bool check(bool[52][52],pair<int,int>);
void rmv(bool[52][52],int,int);

int main(void){
	for(int w,h;cin>> w >> h,w||h;){
		bool fld[52][52]={0};
		int n;

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

		for(n=0;check(fld,make_pair(w,h));n++);
		cout << n << endl;
	}
	return 0;
}

bool check(bool fld[52][52],pair<int,int> wide){
	for(int i=1;i<=wide.second;i++)
		for(int j=1;j<=wide.first;j++)
			if(fld[i][j]){
				rmv(fld,i,j);
				return true;
			}

	return false;
}

void rmv(bool fld[52][52],int y,int x){
	if(fld[y][x]){
		fld[y][x]=false;
		for(int i=-1;i<=1;i++)
			for(int j=-1;j<=1;j++)
				rmv(fld,y+i,x+j);
	}
}

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

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

void dfs(int y, int x)
{
	f[y][x] = 0;
	for(int i=0; i<8; i++)
	{
		int ny = y+dy[i];
		int nx = x+dx[i];
		if(f[ny][nx])
		{
			dfs(ny,nx);
		}
	}
}

int main()
{
	int w,h,c,x,y;
	while(cin >> w >> h, w||h)
	{
		memset(f,0,sizeof(f));

		for(y=1; y<=h; y++)
		for(x=1; x<=w; x++)
		{
			cin >> f[y][x];
		}

		c=0;
		for(y=1; y<=h; y++)
		for(x=1; x<=w; x++)
		{
			if(f[y][x])
			{
				c++;
				dfs(y,x);
			}
		}
		cout << c << endl;
	}
}

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

int w,h;
int m[50][50];

void solve(int x,int y){
	if(m[y][x] == 1){
		m[y][x] = 0;
		for(int i=-1;i<2;i++){
			for(int j=-1;j<2;j++){
				if(i==0&&j==0){
					continue;
				}
				if(y+i >= 0 && y+i < h && x+j >= 0 && x+j < w){
					solve(x+j,y+i);
				}
			}
		}
	}
}

int main(){
	while(true){
		int count=0;

		cin >> w >> h;
		if(w==0&&h==0){
			break;
		}

		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){
					solve(j,i);
					count++;
				}
			}
		}

		cout << count << endl;
	}
}

C++: #include <iostream>
#include <vector>
using namespace std;
typedef vector<vector<int> > VVI;
typedef vector<vector<bool> > VVB;

int w, h, cnt;

void labeling(VVI& map, VVB& visited, int x, int y) {
    visited[x][y] = true;
    for (int i = -1; i <= 1; ++i) {
        for (int j = -1; j <= 1; ++j) {
            if (x+i < 0 || h <= x+i || y+j < 0 || w <= y+j)
                continue;
            if (!visited[x+i][y+j] && map[x+i][y+j])
                labeling(map, visited, x+i, y+j);
        }
    }
}

int main(int argc, char *argv[])
{
    while (cin >> w >> h, w | h) {
        VVI map(h, vector<int>(w));
        VVB visited(h, vector<bool>(w, false));
        cnt = 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 (!visited[i][j] && map[i][j]) {
                    cnt++;
                    labeling(map, visited, i, j);
                }
            }
        }
        cout << cnt << endl;
    }
    return 0;
}

C++: #include<iostream>
#include<vector>
using namespace std;
int dx[8]={0,1,0,-1,1,1,-1,-1};
int dy[8]={1,0,-1,0,1,-1,1,-1};

bool dfs(int sx,int sy,vector<vector<bool> >&c){
if(c[sx][sy]==false) return false;
c[sx][sy]=false;
for(int i=0;i<8;i++){
int nx=sx+dx[i];
int ny=sy+dy[i];
bool flag=dfs(nx,ny,c);
}
return true;
}

int main(void){
int h,w,temp;
while(1){
cin>>w>>h;
if(w==0) break;
vector<vector<bool> > c(w+2,vector<bool>(h+2,false)) ;
for(int i=1;i<=h;i++){
for(int j=1;j<=w;j++){
cin>>temp;
if(temp==1) c[i][j]=true;
}
}
int t=0;
for(int i=1;i<=h;i++){
for(int j=1;j<=w;j++){
bool flag=dfs(i,j,c);
if(flag) t++;
}
}
cout<<t<<endl;
}
return 0;
}

C++: #include <bits/stdc++.h>
#define ll long long
#define INF 1000000005
#define MOD 1000000007
#define EPS 1e-10
#define rep(i,n) for(int i=0;i<(int)n;++i)
#define each(a, b) for(auto (a): (b))
#define all(v) (v).begin(),(v).end()
#define fi first
#define se second
#define pb push_back
#define sint(x) cout <<#x<<" = "<<(x)<<endl
#define spair(p) cout <<#p<<": "<<p.fi<<" "<<p.se<<endl
#define svec(v) cout<<#v<<":";rep(i,v.size())cout<<" "<<v[i];cout<<endl
#define sset(s) cout<<#s<<":";each(i,s)cout <<" "<<i;cout<<endl

using namespace std;

typedef pair<int,int>P;

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

int flag[MAX_N][MAX_N];
int w,h;

void dfs(int u,int v)
{
    flag[u][v] = 0;
    rep(i,8){
        int nu = u + dx[i],nv = v + dy[i];
        if(0 <= nu && nu < h && 0 <= nv && nv < w && flag[nu][nv]){
            dfs(nu,nv);
        }
    }
    return;
}

int main()
{
    while(1){
        cin >> w >> h;
        if(w == 0){
            break;
        }
        rep(i,h){
            rep(j,w){
                cin >> flag[i][j];
            }
        }
        int ans = 0;
        rep(i,h){
            rep(j,w){
                if(flag[i][j] != 0){
                    dfs(i,j);
                    ans++;
                }
            }
        }
        cout << ans << "\n";
    }
}