Go: package main

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

const (
	initialBufSize = 100000
	maxBufSize     = 1000000
)

var sc *bufio.Scanner

func initScanner(r io.Reader) *bufio.Scanner {
	buf := make([]byte, initialBufSize)

	sc := bufio.NewScanner(r)
	sc.Buffer(buf, maxBufSize)
	sc.Split(bufio.ScanWords) // bufio.ScanLines
	return sc
}

func main() {
	sc = initScanner(os.Stdin)
	resolve()
}

func resolve() {
	results := []int{}
	for true {
		w, h := scanInt(sc), scanInt(sc)
		if w == 0 && h == 0 {
			break
		}
		grid := make([][]int, h)
		for i := 0; i < h; i++ {
			grid[i] = make([]int, w)
			for j := 0; j < w; j++ {
				grid[i][j] = scanInt(sc)
			}
		}
		num := 0
		for i := range loop0(h) {
			for j := range loop0(w) {
				start := pos{y: i, x: j}
				if grid[start.y][start.x] == 0 {
					continue
				}
				num++
				_dfs(grid, w, h, start)
			}
		}
		results = append(results, num)
	}
	for _, v := range results {
		fmt.Println(v)
	}
	return
}

func _dfs(grid [][]int, w, h int, start pos) {
	grid[start.y][start.x] = 0
	for _, df := range []pos{
		pos{1, 0}, pos{-1, 0},
		pos{0, -1}, pos{0, 1},
		pos{1, 1}, pos{-1, -1},
		pos{-1, 1}, pos{1, -1},
	} {
		np := addPos(start, df)
		if np.y < 0 || h <= np.y || np.x < 0 || w <= np.x {
			continue
		}
		if grid[np.y][np.x] == 0 {
			continue
		}
		_dfs(grid, w, h, np)
	}
}

type pos struct{ x, y int }

func addPos(p, d pos) pos {
	return pos{
		x: p.x + d.x,
		y: p.y + d.y,
	}
}

func loop0(n int) chan int {
	return loop(0, n-1, 1)
}

func loop1(n int) chan int {
	return loop(1, n, 1)
}

func loop(s, e, d int) chan int {
	ch := make(chan int)
	go func() {
		for i := s; i <= e; i += d {
			ch <- i
		}
		close(ch)
	}()
	return ch
}

// snip-scan-funcs
func scanInt(sc *bufio.Scanner) int {
	sc.Scan()
	i, _ := strconv.Atoi(sc.Text())
	return int(i)
}
func scanString(sc *bufio.Scanner) string {
	sc.Scan()
	return sc.Text()
}

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;
}