Go: package main

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

func getScanner(fp *os.File) *bufio.Scanner {
	scanner := bufio.NewScanner(fp)
	scanner.Split(bufio.ScanWords)
	scanner.Buffer(make([]byte, 1000005), 1000005)
	return scanner
}

func getNextString(scanner *bufio.Scanner) string {
	scanner.Scan()
	return scanner.Text()
}

func getNextInt(scanner *bufio.Scanner) int {
	i, _ := strconv.Atoi(getNextString(scanner))
	return i
}

func getNextInt64(scanner *bufio.Scanner) int64 {
	i, _ := strconv.ParseInt(getNextString(scanner), 10, 64)
	return i
}

func getNextUint64(scanner *bufio.Scanner) uint64 {
	i, _ := strconv.ParseUint(getNextString(scanner), 10, 64)
	return i
}

func getNextFloat64(scanner *bufio.Scanner) float64 {
	i, _ := strconv.ParseFloat(getNextString(scanner), 64)
	return i
}

func main() {
	fp := os.Stdin
	wfp := os.Stdout
	cnt := 0

	if os.Getenv("MASPY") == "ますピ" {
		fp, _ = os.Open(os.Getenv("BEET_THE_HARMONY_OF_PERFECT"))
		cnt = 0
	}
	if os.Getenv("MASPYPY") == "ますピッピ" {
		wfp, _ = os.Create(os.Getenv("NGTKANA_IS_GENIUS10"))
	}

	scanner := getScanner(fp)
	writer := bufio.NewWriter(wfp)
	for solve(scanner, writer) {

	}
	for i := 0; i < cnt; i++ {
		fmt.Fprintln(writer, "-----------------------------------")
		solve(scanner, writer)
	}
	writer.Flush()
}

func solve(scanner *bufio.Scanner, writer *bufio.Writer) bool {
	sol := solver{}
	sol.w = getNextInt(scanner)
	sol.h = getNextInt(scanner)
	if sol.w == 0 && sol.h == 0 {
		return false
	}
	sol.land = makeInts(sol.h, sol.w)
	for y := 0; y < sol.h; y++ {
		for x := 0; x < sol.w; x++ {
			sol.land[y][x] = getNextInt(scanner)
		}
	}
	ans := 0
	for y := 0; y < sol.h; y++ {
		for x := 0; x < sol.w; x++ {
			if sol.land[y][x] == 0 {
				continue
			}
			ans++
			sol.dfs(y, x)
		}
	}
	fmt.Fprintln(writer, ans)
	return true
}

type solver struct {
	w, h int
	land [][]int
}

func (sol *solver) dfs(y, x int) {
	if y < 0 {
		return
	}
	if y >= sol.h {
		return
	}
	if x < 0 {
		return
	}
	if x >= sol.w {
		return
	}
	if sol.land[y][x] == 0 {
		return
	}
	sol.land[y][x] = 0
	dy := [8]int{-1, -1, -1, 0, 0, 1, 1, 1}
	dx := [8]int{-1, 0, 1, -1, 1, -1, 0, 1}
	for i := 0; i < 8; i++ {
		sol.dfs(y+dy[i], x+dx[i])
	}
}
func makeInts(h, w int) [][]int {
	index := make([][]int, h, h)
	data := make([]int, h*w, h*w)
	for i := 0; i < h; i++ {
		index[i] = data[i*w : (i+1)*w]
	}
	return index
}

C++: # include <vector>
# include <list>
# include <map>
# include <set>
# include <deque>
# include <stack>
# include <bitset>
# include <algorithm>
# include <functional>
# include <numeric>
# include <utility>
# include <sstream>
# include <iostream>
# include <iomanip>
# include <cstdio>
# include <cmath>
# include <cstdlib>
# include <cctype>
# include <string>
# include <cstring>
# include <ctime>
# include <queue>


# define FOR(i, a, b) for(int i{a}; i < (b); ++i)
# define REP(i, n)    FOR(i, 0, n)

using namespace std;

using ll = long long;

template<typename T>
using Vec = vector<T>;


template<typename T>
using vec = vector<T>;
template <class T>
using vv = vector<vector<T>>;

constexpr int ocean = 0;

void dfs(const vv<int>& map, vv<bool>& reached, int r, int c) {
    reached[r][c] = true;
    if (reached[r][c] == ocean)
        return;
    for (int i = -1; i <= 1; ++i)
        for (int j = -1; j <= 1; ++j)
        {
            if (i == 0 && j == 0)
                continue;
            int y = r + i;
            int x = c + j;
            if (!reached[y][x] && map[y][x] != ocean)
                dfs(map, reached, y, x);
        }
}

int main()
{

    while (true)
    {
        int w, h;
        cin >> w >> h;

        if (w == 0)
            break;

        vv<int> map(h + 2, vec<int>(w + 2, ocean));

        for (int i = 1; i <= h; ++i)
            for (int j = 1; j <= w; ++j)
                cin >> map[i][j];

        vv<bool> hasReached(h + 2, vec<bool>(w + 2, false));

        // init
        

        int count{};

        for (int i = 1; i < map.size(); ++i)
            for (int j = 1; j < map[0].size(); ++j)
            {
                if (!hasReached[i][j] && map[i][j] != ocean)
                {
                    ++count;
                    dfs(map, hasReached, i, j);
                }
            }

        cout << count << endl;
    }

    return 0;
}

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

#define INF_LL (int64)1e18
#define INF (int32)1e9
#define REP(i, n) for(int64 i = 0;i < (n);i++)
#define FOR(i, a, b) for(int64 i = (a);i < (b);i++)
#define all(x) x.begin(),x.end()
#define fs first
#define sc second

using int32 = int_fast32_t;
using uint32 = uint_fast32_t;
using int64 = int_fast64_t;
using uint64 = uint_fast64_t;
using PII = pair<int32, int32>;
using PLL = pair<int64, int64>;

const double eps = 1e-10;

template<typename A, typename B>inline void chmin(A &a, B b){if(a > b) a = b;}
template<typename A, typename B>inline void chmax(A &a, B b){if(a < b) a = b;}

class UnionFind{
private:
	vector<int32> par;
	int32 n;

public:
	UnionFind(){}
	UnionFind(int32 n):n(n){
		par.resize(n, -1);
	}

	int32 find(int32 x){
		return par[x] < 0 ? x : par[x] = find(par[x]);
	}

	int32 size(int32 x){
		return -par[find(x)];
	}

	void unite(int32 x, int32 y){
		x = find(x);
		y = find(y);
		if(x == y) return;
		if(size(x) < size(y)) swap(x, y);
		par[x] += par[y];
		par[y] = x;
	}

	bool same(int32 x, int32 y){
		return find(x) == find(y);
	}
};

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

	int32 w, h;
	while(cin >> w >> h && w && h){
		UnionFind uf((w+2)*(h+2));
		vector<vector<int32>> f(h+2, vector<int32>(w+2, '0'));
		REP(i, h){
			REP(j, w){
				cin >> f[i+1][j+1];
			}
		}
		int32 dx[] = {-1, -1, -1, 0, 0, 1, 1, 1};
		int32 dy[] = {-1, 0, 1, -1, 1, -1, 0, 1};
		FOR(y, 1, h+1){
			FOR(x, 1, w+1){
				REP(k, 8){
					int32 yy = y+dy[k], xx = x+dx[k];
					if(f[y][x] == 1 && f[yy][xx] == 1 && !uf.same(yy*(w+2)+xx, y*(w+2)+x)){
						uf.unite(yy*(w+2)+xx, y*(w+2)+x);
					}
				}
			}
		}
		int32 cnt = 0;
		REP(y, h+2){
			REP(x, w+2){
				if(f[y][x] == 1 && uf.find(y*(w+2)+x) == y*(w+2)+x)
					cnt++;
			}
		}
		cout << cnt << endl;
	}
}

C++: #include <bits/stdc++.h>
using namespace std;
 
int w=50,h=50;
vector<vector<int>>data(h,vector<int>(w));


void dfs(int x,int y){
	data[x][y]=0;
	for(int dx=-1;dx<=1;dx++){
    	for(int dy=-1;dy<=1;dy++){
        //四方の移動先を考え、陸を探す。
          int nx=x+dx, ny=y+dy;
        	if( 0<=nx && nx<h && 0<=ny && ny<w &&data[nx][ny]==1){
              dfs(nx,ny);
        	}
        }
    }     
}

int main() {
 while(1){w=0;h=0;
  cin >>w>>h;
  if(w==0&&h==0)return 0;
  for(int i=0;i<h;i++){
  	for(int j=0;j<w;j++){
    cin>>data.at(i).at(j);
    } 
  }
  
  int ans=0;
  for(int i=0;i<h;i++){
  	for(int j=0;j<w;j++){
   		if(data[i][j]==1){
        	dfs(i,j);
        	ans++;
        }
    } 
  }
 	cout <<ans <<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 <= w;i++) {
			for (int j = 1;j <= h;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;
}