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 <cmath>
#include <set>
#include <string>
#include <algorithm>
#include <vector>
#include <bitset>
#include <iomanip>
#include <map>

using std::cin;
using std::cout;
using std::endl;
using std::string;
using std::vector;
using std::bitset;

#define rep(i,n)    for(int i=0; i<(int)n; i++)
typedef long double ld;
typedef long long ll;
typedef vector<int> vint;
typedef vector<ll> vll;
typedef std::pair<int, int> pii;
typedef std::pair<ll, ll> pll;

template<class T>
using Graph = vector<vector<T>>;

void printFixedDouble(double d, int fix) {
    cout << std::fixed;
    cout << std::setprecision(fix) << d << endl;
}

void mark(const int w, const int h, Graph<int> &map, int x, int y) {
    if (x < 0 || x >= w || y < 0 || y >= h) {
        return;
    }
    if (map[x][y] != 1) {
        return;
    }
    map[x][y] = 2;
    mark(w, h, map, x + 1, y - 1);
    mark(w, h, map, x, y - 1);
    mark(w, h, map, x - 1, y - 1);
    mark(w, h, map, x + 1, y);
    mark(w, h, map, x - 1, y);
    mark(w, h, map, x + 1, y + 1);
    mark(w, h, map, x, y + 1);
    mark(w, h, map, x - 1, y + 1);
}

int main() {
    while (1) {
        int w, h;
        cin >> w >> h;
        if (w == 0 && h == 0) {
            break;
        }

        Graph<int> map(w, vint(h, 0));
        rep(y, h) {
            rep(x, w) {
                cin >> map[x][y];
            }
        }

        int count = 0;
        rep(y, h) {
            rep(x, w) {
                if (map[x][y] == 1) {
                    mark(w, h, map, x, y);
                    count++;
                }
            }
        }
        cout << count << endl;
    }
}

C++: #include<iostream>

using namespace std;

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

const int vx[] = {1,0,-1,0}, vy[] = {0,1,0,-1};

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

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 <= w;i++) {
			for (int j = 1;j <= h;j++) {
				if (map[i][j] == 1) {
					count++, island(i, j);
				}
			}
		}
		cout << count << endl;
		for (int i = 1;i <= w;i++)for (int j = 1;j <= h;j++)map[i][j] = -1;
	}
	return 0;
}

C++: #include <bits/stdc++.h>
using namespace std;
#define N_PRIME 1000000007
#define ll int64_t

int w,h;
bool is_land[64][64];

void show(){
    for(int y = 1 ; y <= h ; y++ ){
        for(int x = 1; x <= w ;x++){
            cout << is_land[x][y] ?'1':'0';
        }
        cout << endl;
    }
}

bool fill(int x,int y){
    if( is_land[x][y] == false )
        return false;
    
    is_land[x][y] = false;
    for(int dx = -1 ; dx <= 1 ; dx++){
        for(int dy = -1 ; dy <= 1 ; dy++ ){ 
            fill(x+dx,y+dy);
        }
    }
    
    return true;
}

void subroutine(){
    
    for(int i = 0 ; i < 64 * 64 ; i++ )
        is_land[i/64][i%64] = false;
    
    cin >> w >> h;
    if( w == 0 && h == 0 )exit(0);
    
    for(int y = 0 ; y < h ; y++ ){
        for(int x = 0 ;x < w ; x++ ){
            int temp;
            cin >> temp;
            is_land[x+1][y+1] = temp == 1;
        }
    }
    int count = 0 ;
    
    for(int y = 1 ; y <= h ; y++ ){
        for(int x = 1 ;x <= w ; x++ ){
            if(  fill(x,y) )
                count++;
            //show();
        }
    }
    
    cout << count << endl;
}

int main(void){
    while(true)subroutine();
    return 0;
}

C++: #include<iostream>
#include<queue>
#include<cstring>
using namespace std;
int x[50][50], col[50][50], h, w, c, i, j, k, l, p, q;
queue<int>Q, R;
int main() {
	while (true) {
		c = 0;
		memset(col, 0, sizeof(col));
		cin >> h >> w;
		if (h == 0) { break; }
		for (i = 0; i < h; i++) { for (j = 0; j < w; j++) { cin >> x[i][j]; } }
		for (i = 0; i < 50; i++) {
			for (j = 0; j < 50; j++) {
				if (x[i][j] == 1 && col[i][j] == 0) {
					c++;
					col[i][j] = c;
					Q.push(i * 100 + j);
					while (!Q.empty()) {
						while (!Q.empty()) {
							p = Q.front() / 100;
							q = Q.front() % 100;
							for (k = -1; k < 2; k++) {
								for (l = -1; l < 2; l++) {
									if (x[p + k][q + l] == 1 && col[p + k][q + l] == 0) {
										col[p + k][q + l] = c;
										R.push((p + k) * 100 + q + l);
									}
								}
							}
							Q.pop();
						}
						while (!R.empty()) {
							Q.push(R.front());
							R.pop();
						}
					}
				}
			}
		}
		cout << c << endl;
	}
}

C++: #include <iostream>
#include <vector>
#include <algorithm>
#include <cmath>
#include <queue>
#include <string>
#include <map>
#include <set>
#include <stack>
#include <tuple>
#include <deque>
#include <numeric>
#include <bitset>
#include <iomanip>
#include <cassert>
#include <chrono>
#include <random>
#include <limits>
#include <iterator>
#include <functional>
#include <sstream>
#include <complex>
using namespace std;

using ll = long long;
using ull = uint64_t;
using P = pair<int, int>;
constexpr double EPS = 1e-9;
constexpr int INF = 1001001001;
constexpr int mod = 1000000007;
// constexpr int mod = 998244353;

template<class T>
inline bool chmax(T& x, T y){
    if(x < y){
        x = y;
        return true;
    }
    return false;
}
template<class T>
inline bool chmin(T& x, T y){
    if(x > y){
        x = y;
        return true;
    }
    return false;
}

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

int main(){
    ios::sync_with_stdio(false);
    cin.tie(nullptr);

    for(;;){
        int R, C;
        cin >> R >> C;
        if(R == 0 && C == 0)    return 0;
        vector<vector<int>> c(C, vector<int>(R));
        for(int i = 0; i < C; ++i){
            for(int j = 0; j < R; ++j){
                cin >> c[i][j];
            }
        }
        vector<vector<bool>> used(C, vector<bool>(R, false));
        auto dfs = [&](auto&& self, int x, int y) -> void {
            used[x][y] = true;
            for(int i = 0; i < 8; ++i){
                int nx = x + dx[i], ny = y + dy[i];
                if(nx < 0 || nx >= C || ny < 0 || ny >= R)  continue;
                if(used[nx][ny] || c[nx][ny] == 0)  continue;
                self(self, nx, ny);
            }
            return;
        };
        int ans = 0;
        for(int i = 0; i < C; ++i){
            for(int j = 0; j < R; ++j){
                if(used[i][j] || c[i][j] == 0)  continue;
                ++ans;
                dfs(dfs, i, j);
            }
        }
        cout << ans << '\n';
    }
}