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

using std::cin;
using std::cout;
using std::endl;

int main(void) {
	int w,h;
	cin >> w;
	cin >> h;

	while (w!=0||h!=0) {
		int count = 0;
		std::stack<int> st_x;
		std::stack<int> st_y;
		int xn,yn;
		int c;
		bool all = false;
		std::vector< std::vector<bool> > island(w+2, std::vector<bool>(h+2));
		for (int i = 0; i < h+2; i++) {
			for (int j = 0; j < w+2; j++) {
				if (i==0||i==h+1||j==0||j==w+1) {
					island[i][j] = false;
				} else {
					cin >> c;
					if (c==1) {
						island[i][j] = true;
						st_x.push(j);
						st_y.push(i);
					} else {
						island[i][j] = false;
					}
				}
			}
		}

		while (!st_x.empty()) {
			xn = st_x.top();
			yn = st_y.top();
			st_x.pop();
			st_y.pop();
			if (island[xn][yn]) {
				std::stack<int> st_xn,st_yn;
				island[xn][yn] = false;
				count++;
				st_xn.push(xn);
				st_yn.push(yn);
				while (!st_xn.empty()) {
					xn = st_xn.top();
					yn = st_yn.top();
					st_xn.pop();
					st_yn.pop();
					if (island[xn+1][yn]) {
						st_xn.push(xn+1);
						st_yn.push(yn);
						island[xn+1][yn] = false;
					}
					if (island[xn-1][yn]) {
						st_xn.push(xn-1);
						st_yn.push(yn);
						island[xn-1][yn] = false;
					}
					if (island[xn+1][yn+1]) {
						st_xn.push(xn+1);
						st_yn.push(yn+1);
						island[xn+1][yn+1] = false;
					}
					if (island[xn+1][yn-1]) {
						st_xn.push(xn+1);
						st_yn.push(yn-1);
						island[xn+1][yn-1] = false;
					}
					if (island[xn-1][yn+1]) {
						st_xn.push(xn-1);
						st_yn.push(yn+1);
						island[xn-1][yn+1] = false;
					}
					if (island[xn-1][yn-1]) {
						st_xn.push(xn-1);
						st_yn.push(yn-1);
						island[xn-1][yn-1] = false;
					}
					if (island[xn][yn+1]) {
						st_xn.push(xn);
						st_yn.push(yn+1);
						island[xn][yn+1] = false;
					}
					if (island[xn][yn-1]) {
						st_xn.push(xn);
						st_yn.push(yn-1);
						island[xn][yn-1] = false;
					}
				}
			}
		}
		cout << count << endl;

		cin >> w;
		cin >> h;
	}
		return 0;
}

C++: #include <bits/stdc++.h>
using namespace std;
#define FOR(i,a,b) for(int i=(a);i<(b);++i)
#define rep(i,n) FOR(i,0,n)
#define repr(i,n) for(int i=(n)-1;0<=i;--i)
#define each(e,v) for(auto&& e:(v))
#define all(v) begin(v),end(v)
#define dump(x) cerr<<#x<<": "<<(x)<<endl
#define INF 100000000
#define IINF 9999999999999;
using vint = vector<int>;
using ll = long long;
using vll = vector<ll>;
template <class T> void chmin(T& a, const T& b) { a = min(a, b); }
template <class T> void chmax(T& a, const T& b) { a = max(a, b); }

bool earth[51][51];

void erase_island(int h, int w) {
    earth[h][w] = false;
    //cout << "# " << h << " " << w << endl;
    if(earth[h - 1][w - 1]) erase_island(h - 1, w - 1);
    if(earth[h - 1][w]) erase_island(h - 1, w);
    if(earth[h - 1][w + 1]) erase_island(h - 1, w + 1);
    if(earth[h][w + 1]) erase_island(h, w + 1);
    if(earth[h + 1][w + 1]) erase_island(h + 1, w + 1);
    if(earth[h + 1][w]) erase_island(h + 1, w);
    if(earth[h + 1][w - 1]) erase_island(h + 1, w - 1);
    if(earth[h][w - 1]) erase_island(h, w - 1);
}

int main() {
    //ofstream outputfile("output.txt");
    int w, h;
    while(cin >> w >> h, w + h) {
        if(w == 0 && h == 0) break;
        int temp;
        rep(i, h) {
            rep(j, w) {
                cin >> temp;
                if(temp == 0) earth[i][j] = false;
                else earth[i][j] = true;
            }
        }

        int ans = 0;
        rep(i, h) {
            rep(j, w) {
                if(earth[i][j]) {
                    erase_island(i, j);
                    ++ans;
                }
            }
        }

        cout << ans << endl;
    }

    return 0;
}

C++: #include<iostream>
#include<vector>
#include<algorithm>
#include<cmath>
#include<string>
#include<tuple>
#include<utility>
#include<stack>
#include<queue>
#include<deque>
#include<set>
#include<iomanip>
#include<map>
using namespace std;
typedef long long int ll;
typedef pair<int, int> P;
const int INF = 1e9 + 7;
int dx[8] = { 1,0,-1,0,1,1,-1,-1 };
int dy[8] = { 0,1,0,-1,1,-1,1,-1 };

int w, h;
int s[55][55];
void dfs(int y, int x) {

	s[y][x] = 0;

	for (int i = 0; i < 8; i++) {
		int ny = y + dy[i];
		int nx = x + dx[i];

		if (0 <= ny && ny < h && 0 <= nx && nx < w && s[ny][nx] == 1) {
			dfs(ny, nx);
		}


	}


}



int main() {

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

		for (int i = 0; i < h; i++) {
			for (int j = 0; j < w; j++) {
				cin >> s[i][j];
			}
		}

		int cnt = 0;

		for (int i = 0; i < h; i++) {
			for (int j = 0; j < w; j++) {
				if (s[i][j] == 1) {

					dfs(i, j);
					cnt++;
				}


			}


		}


		cout << cnt << endl;

	}



	return 0;
}

C++: #include<iostream>
using namespace std;
int a[51][51]={},d[51][51]={},ans=0,w,h,dx[8]={-1,0,1,-1,1,-1,0,1},dy[8]={-1,-1,-1,0,0,1,1,1};
int f(int y,int x){
	if(d[y][x]==1 && a[y][x]==0){
		a[y][x]=1;
		for(int i=0;i<8;i++){
			if(y+dy[i]>-1 && y+dy[i]<h && x+dx[i]>-1 && x+dx[i]<w){
				if(a[y+dy[i]][x+dx[i]]==0)
				f(y+dy[i],x+dx[i]);
			}
		}
		return 1;
	}
	else{
		return 0;
	}
}
int main(){
	while(cin>>w>>h,w){
		ans=0;
		for(int i=0;i<h;i++){
			for(int j=0;j<w;j++){
				a[i][j]=0;
				cin>>d[i][j];
			}
		}
		for(int i=0;i<h;i++){
			for(int j=0;j<w;j++){
				if(f(i,j))
				ans++;
			}
		}
		cout<<ans<<endl;
	}
	return 0;
}

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

vector<vector<int> > d(MAX,vector<int>(MAX));
int w,h,data[MAX][MAX];

void dfs(int x, int y,int num){
    int vx[] = {1,-1,0,0,1,-1,1,-1};
    int vy[] = {0,0,1,-1,1,-1,-1,1};
    data[y][x] = num;

    for(int i=0; i<8; i++){
        int dx = vx[i] + x; int dy = vy[i] + y;
        if(dx >= w || dx < 0 || dy >= h || dy < 0 ||
            d[dy][dx] != 1 || data[dy][dx] == num) continue;
        dfs(dx,dy,num);
    }
}

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

        for(int i=0; i<MAX; i++){
            for(int j=0; j<MAX; j++){
                data[i][j] = -2;
            }
        }

        int ans = 0;

        for(int i=0; i<h; i++){
            for(int j=0; j<w; j++){
                if(d[i][j] == 1 && data[i][j] < 0){
                    dfs(j,i,ans);
                    ans++;
                }
            }
        }   
        cout<<ans<<endl;
    }
    return 0;
}