Go: package main

import (
	"fmt"
)

func main() {
	var h, w int
	for {
		fmt.Scan(&w, &h)
		if w == 0 && h == 0 {
			return
		}
		matrix := make([][]int, h)
		Sean := make([][]bool, h)
		for i := 0; i < h; i++ {
			matrix[i] = make([]int, w)
			Sean[i] = make([]bool, w)
			for j := 0; j < w; j++ {
				fmt.Scan(&matrix[i][j])
			}
		}
		var count int
		for i := 0; i < h; i++ {
			for j := 0; j < w; j++ {
				if matrix[i][j] == 1 && Sean[i][j] == false {
					search(i, j, h, w, matrix, Sean)
					count++
				}
			}
		}
		fmt.Println(count)
	}
}
func search(x int, y int, h int, w int, matrix [][]int, Sean [][]bool) {
	if x < 0 || y < 0 || x >= h || y >= w || matrix[x][y] == 0 {
		return
	}
	if Sean[x][y] == true {
		return
	}

	Sean[x][y] = true

	search(x+1, y, h, w, matrix, Sean)
	search(x-1, y, h, w, matrix, Sean)
	search(x, y+1, h, w, matrix, Sean)
	search(x, y-1, h, w, matrix, Sean)
	search(x+1, y-1, h, w, matrix, Sean)
	search(x-1, y+1, h, w, matrix, Sean)
	search(x+1, y+1, h, w, matrix, Sean)
	search(x-1, y-1, h, w, matrix, Sean)
}

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

int w,h,C[100][100];
int diff[8][2]={{-1,-1},{-1,0},{-1,1},{0,-1},{0,1},{1,-1},{1,0},{1,1}};

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

int main(){
    int i,j,ans;
    while(cin>>w>>h,w!=0||h!=0){
        ans=0;
        for(i=0;i<h;i++)for(j=0;j<w;j++)cin>>C[i][j];
        for(i=0;i<h;i++){
            for(j=0;j<w;j++){
                if(C[i][j]==1){
                    dfs(i,j);
                    ans++;
                }
            }
        }
        cout<<ans<<endl;
    }
}

C++: #include <iostream>
#include <vector>
#include <queue>

using namespace std;

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

        // bfs
        int islandCount = 0;
        for (int i = 0; i < h; ++i) {
            for (int j = 0; j < w; ++j) {
                queue<pair<int, int> > que;
                if (map[i][j] != 1) {
                    continue;
                }
                ++islandCount;
                que.push({i, j});
                while (!que.empty()) {
                    pair<int, int> p = que.front(); que.pop();
                    if (map[p.first][p.second] == -1) {
                        continue;
                    }
                    map[p.first][p.second] = -1;
                    for (int k = -1; k < 2; ++k) {
                        for (int l = -1; l < 2; ++l) {
                            if ((k | l) == 0) {
                                continue;
                            }
                            int ny = p.first + k;
                            int nx = p.second + l;
                            if (0 <= nx && nx < w &&
                                    0 <= ny && ny < h) {
                                if (map[ny][nx] == 1) {
                                    que.push({ny, nx});
                                }
                            }
                        }
                    }
                }
            }
        }
        cout << islandCount << endl;
    }

    return 0;
}

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

using namespace std;

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

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

		for(int i=0;i<h;i++)
			for(int j=0;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[50][50],pair<int,int> wide){
	for(int i=0;i<wide.second;i++)
		for(int j=0;j<wide.first;j++)
			if(fld[i][j]){
				rmv(fld,i,j);
				return true;
			}

	return false;
}

void rmv(bool fld[50][50],int y,int x){
	if(x>=0&&y>=0&&x<=50&&y<=50)
		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<vector>
#include<stdio.h>
using namespace std;
#define DUMP(x)  cerr << #x << " = " << (x) << endl;
#define DEBUG(x) cerr << #x << " = " << (x) << " (L" << __LINE__ << ")" << " " << __FILE__ << endl;

vector<vector<int> > map;
vector<vector<bool> > reach;
int w,h;

void dfs(int i, int j){
    if((reach[i][j]) or (map[i][j]==0))
        return;
    reach[i][j] = true;
    if(0<i && 0<j) dfs(i-1,j-1);
    if(0<i) dfs(i-1,j);
    if(0<i && j<w-1) dfs(i-1,j+1);
    if(0<j) dfs(i, j-1);
    if(j<w-1) dfs(i, j+1);
    if(i<h-1 && 0<j) dfs(i+1,j-1);
    if(i<h-1) dfs(i+1,j);
    if(i<h-1 && j<w-1) dfs(i+1,j+1);
}

int main(){

    while((cin >> w >> h) && (w>0 && h>0)){
        int res=0;
        for (int i = 0; i < h; i++) {
            vector<int> temp(w,0);
            for (int j = 0; j < w; j++)
                cin >> temp[j];
            map.push_back(temp);
        }
        for (int i = 0; i < h; i++){
            vector<bool> temp(w,false);
            reach.push_back(temp);
        }
        for (int i = 0; i < h; i++) {
            for (int j = 0; j < w; j++) {
                if(map[i][j]==1 && !reach[i][j]){
                    res++;
                    dfs(i,j);
                }
            }
        }
        cout << res << endl;
        map.clear();
        reach.clear();

    }
    return 0;
}

C++: #include <iostream>
#include <cstdio>
#include <cstdlib>
#include <string>
#include <vector>
#include <deque>
#include <list>
#include <algorithm>
#include <numeric>
#include <functional>
#include <cmath>
#include <cstring>
#include <cctype>
#include <sstream>
#include <set>
#include <map>
#include <queue>
#include <complex>
using namespace std;
#define REP(i,n) for(int i = 0; i < (int)n; i++)
#define FOR(i,a,b) for(int i = a; i < (int)b; i++)
const int INF = 1<<28;

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

void dfs(int x, int y, int z) {
	if(m[y][x]) {
		if(!z)
			cnt++;
		m[y][x] = false;
		if(y > 0 && m[y-1][x])
			dfs(x, y-1, 1);
		if(y > 0 && x > 0 && m[y-1][x-1])
			dfs(x-1, y-1, 1);
		if(x > 0 && m[y][x-1])
			dfs(x-1, y, 1);
		if(x > 0 && y < h-1 && m[y+1][x-1])
			dfs(x-1, y+1, 1);
		if(x < w-1 && m[y][x+1])
			dfs(x+1, y, 1);
		if(x < w-1 && 0 < y && m[y-1][x+1])
			dfs(x+1, y-1, 1);
		if(y < h-1 && m[y+1][x])
			dfs(x, y+1, 1);
		if(y < h-1 && x < w-1 && m[y+1][x+1])
			dfs(x+1, y+1, 1);
	}
	return;
}

int main() {
	while(cin >> w >> h, w|h) {
		REP(i, h) REP(j, w)
			cin >> m[i][j];

		cnt = 0;
		REP(i, h) REP(j, w)
			dfs(j, i, 0);

		cout << cnt << endl;
	}
	return 0;
}