Go: package main

import (
	"bufio"
	"fmt"
	"os"
	"strconv"
)

var sc = bufio.NewScanner(os.Stdin)
var route [][]int

func scanInt() int {
	num, _ := strconv.Atoi(scanString())
	return num
}

func scanString() string {
	sc.Scan()
	return sc.Text()
}

func init() {
	sc.Split(bufio.ScanWords)
}

func dfs(h, w int) {
	if route[h][w] == 0 {
		return
	}

	route[h][w] = 0
	for _, x := range []int{-1, 0, 1} {
		for _, y := range []int{-1, 0, 1} {
			if route[h+x][w+y] == 0 {
				continue
			}
			dfs(h+x, w+y)
		}
	}
}

func main() {
	writer := bufio.NewWriter(os.Stdout)
	for {
		w, h := scanInt(), scanInt()
		if w == 0 && h == 0 {
			break
		}

		route = make([][]int, h+2)
		route[0] = make([]int, w+2)
		for i := 1; i <= h; i++ {
			route[i] = make([]int, w+2)
			for j := 1; j <= w; j++ {
				route[i][j] = scanInt()
			}
		}
		route[h+1] = make([]int, w+2)

		total := 0
		for i := 1; i <= h; i++ {
			for j := 1; j <= w; j++ {
				if route[i][j] == 1 {
					total++
					dfs(i, j)
				}
			}
		}
		fmt.Fprintln(writer, total)
	}
	writer.Flush()
}

C++: #include <iostream>

using namespace std;

int m[51][51];
int w, h;
const int dx[] = {-1, 0, 1, -1, 1, -1, 0, 1};
const int dy[] = {-1, -1, -1, 0, 0, 1, 1, 1};

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

int main()
{
    while (cin >> w >> h, w || h) {
        int isl = 0;

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

        cout << isl << endl;
    }

    return 0;
}

C++: #include <iostream>
using namespace	std;
int h,w,c[50][50];
void dfs(int y,int x){
  c[y][x]=0;
  for(int i=-1;i<=1;i++)
    for(int j=-1;j<=1;j++)
      if(0<=y+i&&y+i<h&&0<=x+j&&x+j<w)
        if(c[y+i][x+j]) dfs(y+i,x+j);
}

int main(){

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

}

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

#define rep(i,a) for(int i = 0;i < (a); i++)
#define repi(i,a,b) for(int i = (a); i < (b); i++)
#define repd(i,a,b) for(int i = (a); i >= (b); i--)
#define repit(i,a) for(__typeof((a).begin()) i = (a).begin(); i != (a).end(); i++)
#define all(u) (u).begin(),(u).end()
#define rall(u) (u).rbegin(),(u).rend()
#define UNIQUE(u) (u).erase(unique(all(u)),(u).end())
#define pb push_back
#define mp make_pair
#define INF 1e9
#define EPS 1e-9
#define PI acos(-1.0)

using namespace std;

typedef long long ll;
typedef vector<int> vi;
int w, h;
int c[100][100];
bool inrange(int i, int j){
    return i >= 0 && j >= 0 && i < h && j < w;
}
int di[] = {-1, 0, 1, -1, 1, -1, 0, 1};
int dj[] = {-1,-1,-1, 0, 0, 1, 1, 1};
void rec(int i, int j){
    c[i][j] = 0;
    rep(k, 8){
        int ni = i + di[k];
        int nj = j + dj[k];
        if(inrange(ni, nj) && c[ni][nj]) rec(ni, nj);
    }
}
int main() {
    while(cin >> w >> h, w){
        rep(i, h) rep(j, w) cin >> c[i][j];
        int cnt = 0;
        rep(i, h) rep(j, w){
            if(c[i][j]){
                rec(i, j);
                cnt++;
            }
        }
        cout << cnt << endl;
    }
    return 0;
}

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

void search(vector<vector<int>>& map, int x, int y)
{
	if (map[x][y] < 0)
		return;

	if (map[x][y] == 1) {
		map[x][y] = 0;
		search(map, x - 1, y);
		search(map, x + 1, y);
		search(map, x, y - 1);
		search(map, x, y + 1);
		search(map, x - 1, y - 1);
		search(map, x + 1, y + 1); 
		search(map, x + 1, y - 1);
		search(map, x - 1, y + 1);
	}
}
int main()
{
	int w, h;
	while (1) {
		cin >> w >> h;
		if (w == 0 && h == 0)
			break;

		vector<vector<int>> map;
		map = vector<vector<int>>(h+2, vector<int>(w+2, -1));

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

C++: #include<iostream>

using namespace std;

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

void island(int _w, int _h) {
	map[_w][_h] = 2;
	for (int i = -1;i <= 1;i++) {
		for (int j = -1;j <= 1;j++) {
			if (_w + i >= 1 && _w + i <= h&&_h + j >= 1 && _h + j <= w&&map[_w + i][_h + j] == 1) {
				island(_w + i, _h + j);
			}
		}
	}
}

int main() {
	while (cin >> w >> h, w + h) {
		for (int i = 1;i <= h;i++) {
			for (int j = 1;j <= w;j++) {
				cin >> map[i][j];
			}
		}
		int count = 0;
		for (int i = 1;i <= h;i++) {
			for (int j = 1;j <= w;j++) {
				if (map[i][j] == 1) {
					count++, island(i, j);
				}
			}
		}
		cout << count << endl;
		for (int i = 1;i <= 50;i++)for (int j = 1;j <= 50;j++)map[i][j] = -1;
	}
	return 0;
}

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

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

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

//void printmap() {
//	for (int i = 0 ; i < h; i++) {
//		for (int j = 0; j < w; j++)
//			cout << map[i][j] << " ";
//		cout << endl;
//	}
//}

void oneIsland(int x, int y) {
	for (int i = 0; i < 8; i++) {
		int nextX = x + dx[i];
		int nextY = y + dy[i];
		if (nextX < 0 || w <= nextX) continue;
		if (nextY < 0 || w <= nextY) continue;
		if (map[nextY][nextX] == 0) continue;
		map[nextY][nextX] = 0;
		oneIsland(nextX, nextY);
	}
}

int main() {
	while (1) {
		cin >> w >> h;
		if (w == 0 && h == 0) break;
		for (int i = 0; i < h; i++) {
			for (int j = 0; j < w; j++) {
				cin >> map[i][j];
			}
		}
		
		int count = 0;
		for (int i = 0; i < h; i++) {
			for (int j = 0; j < w; j++) {
				if (map[i][j] == 1) {
					map[i][j] = 0;
					oneIsland(j, i);
					//printmap();
					count++;
				}
			}
		}
		cout << count <<endl;
	}
	return 0;
}