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

using namespace std;
typedef std::vector<std::vector<int> > Field;
typedef std::vector<std::vector<bool> > Seen;

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

// DFS
bool dfs(const Field& field, Seen& seen, const int ih, const int iw, const int H, const int W) {
    seen[ih][iw] = true;
    if (field[ih][iw] == 0) return false;
    // search oct
    for (int i = 0; i < 8; ++i) {
        const int curh = ih + dx[i];
        const int curw = iw + dy[i];

        // check the boundaries
        if (curh >= H || curh < 0) continue;
        if (curw >= W || curw < 0) continue;

        if (seen[curh][curw]) continue;

        dfs(field, seen, curh, curw, H, W);
    }
    return true;
}

int search(const Field& field, const int H, const int W) {
    Seen seen(H, std::vector<bool>(W, false));
    int cnt = 0;
    for (int ih = 0; ih < H; ++ih) {
        for (int iw = 0; iw < W; ++iw) {
            if (seen[ih][iw]) continue;
            if (field[ih][iw] == 0) continue;
            if (dfs(field, seen, ih, iw, H, W)) ++cnt;
        }
    }
    return cnt;
}

int main() {
    vector<int> res;
    for (;;) {
        int W, H; cin >> W >> H;
        if ( (W == 0 || H == 0) ) break;
        Field field(H, std::vector<int>(W, 0));
        for (int ih = 0; ih < H; ++ih) {
            for (int iw = 0; iw < W; ++iw) {
                cin >> field[ih][iw];
            }
        }
        res.push_back(search(field, H, W));
    }
    for (unsigned int i = 0; i < res.size(); ++i) {
        cout << res[i] << "\n";
    }
    return 0;
}

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

int fld[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)
{
	fld[y][x] = 0;
	for(int i=0; i<8; i++)
	{
		int ny = y+dy[i];
		int nx = x+dx[i];

		if(fld[ny][nx])
		{
			dfs(ny,nx);
		}
	}
}


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

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

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

		cout << c << endl;
	}
}

C++: 
#include <iostream>
#include <cstdlib>
#include <cstdio>
#include <cstring>
#include <cmath>
#include <stack>
#include <cctype>
#include <complex>
#include <vector>
#include <set>
#include <map>
#include <algorithm>
#include <functional>
#include <cassert>

using namespace std;

void dfs(int x, int y, int h, int w, vector<vector<int> > &f){
	
	f[x][y] = 0;
	for(int i=-1;i<=1;i++){
		for(int j=-1;j<=1;j++){
			int x2 = x+i;
			int y2 = y+j;
			if((0<=x2 && x2<=h-1) && (0<=y2 && y2<=w-1)){
				if(f[x2][y2]) dfs(x2, y2, h, w, f);
			}
		}
	}
	
	return;
}

bool solve(){
	int w, h;
	cin>> w>> h;
	if(w==0 && h==0) return false;
	
	vector<vector<int> > field(h, vector<int>(w,0));
	for(int i=0;i<h;i++){
		for(int j=0;j<w;j++){
			int c;
			cin>> c;
			field[i][j] += c;
		}
	}
	
	int ans = 0;
	for(int i=0;i<h;i++){
		for(int j=0;j<w;j++){
			if(field[i][j]){
				dfs(i, j, h, w, field);
				ans++;
			}
		}
	}
	
	cout<< ans<< endl;
	return true;
}

int main(){
	cout.setf(ios::fixed);
	cout.precision(10);
	while(solve());

	return 0;
}

C++: #include<iostream>
#include<string>
#include<vector>
#include<iomanip>
#include<algorithm>
#include<queue>
#include<stack>
#include<list>
#include<map>
#include<deque>
#include<math.h>
using namespace std;
#define ll long long
ll mp[100][100];
ll visited[100][100];
void dfs(ll w,ll h,ll i,ll j){
    ll a,b;
    visited[i][j]=1;
    for(a=i-1;a<=i+1;a++){
        for(b=j-1;b<=j+1;b++){
            if(a>=0&&a<h&&b>=0&&b<w&&mp[a][b]==1&&visited[a][b]==0)dfs(w,h,a,b);
        }
    }
    return ;
}

int main(){
    while(true){
        ll w,h;
        cin >> w >> h;
        if(w==0&&h==0) return 0;
        ll i,j;
        for(i=0;i<h;i++){
            for(j=0;j<w;j++){
                cin >> mp[i][j];
                visited[i][j]=0;
            }
        }
        ll ans =0;
        for(i=0;i<h;i++){
            for(j=0;j<w;j++){
                if(mp[i][j]==1&&visited[i][j]==0){
                    ans++;
                    dfs(w,h,i,j);
                }
            }
        }
        cout << ans <<endl;
    }
}

C++: #include <iostream>
#include <cstdio>
#include <vector>
#include <algorithm>
#include <complex>
#include <cstring>
#include <cstdlib>
using namespace std;

#define REP(i,n) for(int i=0;i<(int)n;++i)
#define FOR(i,c) for(__typeof((c).begin())i=(c).begin();i!=(c).end();++i)
#define ALL(c) (c).begin(), (c).end()

int w,h;
int dx[] = {0,1,1,1,0,-1,-1,-1};
int dy[] = {-1,-1,0,1,1,1,0,-1};
int ba[50][50];

void go(int x, int y) {
  if (ba[x][y]==0) return;
  ba[x][y]=0;
  REP(i,8) {
    int xx=x+dx[i];
    int yy=y+dy[i];
    if (xx<0||w<=xx||yy<0||h<=yy) continue;
    if (ba[xx][yy]==1)
      go(xx,yy);
  }
}




int main() {
  while(cin>>w>>h,w+h) {
    REP(y,h)
      REP(x,w)
      cin >> ba[x][y];
    int ans = 0;
    REP(y,h) {
      REP(x,w) {
        if (ba[x][y]) {
          go(x,y);
          ans++;
        }
      }
    }
    cout << ans << endl;
  }

}