Go: package main

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

var (
	readString func() string
	readBytes  func() []byte
	stdout     *bufio.Writer
)

func init() {
	readString, readBytes = newReadString(os.Stdin)
	stdout = bufio.NewWriter(os.Stdout)
}

func newReadString(ior io.Reader) (func() string, func() []byte) {
	r := bufio.NewScanner(ior)
	r.Buffer(make([]byte, 1024), math.MaxInt32)
	r.Split(bufio.ScanWords)

	f1 := func() string {
		if !r.Scan() {
			panic("Scan failed")
		}
		return r.Text()
	}
	f2 := func() []byte {
		return []byte(f1())
	}
	return f1, f2
}

func readInt() int {
	return int(readInt64())
}

func readInt64() int64 {
	i, err := strconv.ParseInt(readString(), 10, 64)
	if err != nil {
		panic(err.Error())
	}
	return i
}

func readFloat64() float64 {
	f, err := strconv.ParseFloat(readString(), 64)
	if err != nil {
		panic(err.Error())
	}
	return f
}

func printf(f string, args ...interface{}) (int, error) {
	return fmt.Fprintf(stdout, f, args...)
}

func println(args ...interface{}) (int, error) {
	return fmt.Fprintln(stdout, args...)
}

func eprintln(args ...interface{}) (int, error) {
	return fmt.Fprintln(os.Stderr, args...)
}

func eprintf(f string, args ...interface{}) (int, error) {
	return fmt.Fprintf(os.Stderr, f, args...)
}

func min(a, b int) int {
	if a < b {
		return a
	}
	return b
}

func min64(a, b int64) int64 {
	if a < b {
		return a
	}
	return b
}

func max(a, b int) int {
	if a < b {
		return b
	}
	return a
}

func max64(a, b int64) int64 {
	if a < b {
		return b
	}
	return a
}

func abs(a int) int {
	if a < 0 {
		return -a
	}
	return a
}

func abs64(a int64) int64 {
	if a < 0 {
		return -a
	}
	return a
}

func gcd(a, b int) int {
	if b == 0 {
		return a
	}
	return gcd(b, a%b)
}

func gcd64(a, b int64) int64 {
	if b == 0 {
		return a
	}
	return gcd64(b, a%b)
}

// readString() string
// readInt() int
// readInt64() int64
// readFloat64() float64

// -----------------------------------------------------------------------------

func main() {
	defer stdout.Flush()

	for {
		ans, ok := solve()
		if !ok {
			break
		}
		println(ans)
	}

}

func solve() (int, bool) {
	w := readInt()
	h := readInt()
	if w == 0 && h == 0 {
		return 0, false
	}

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

	var ans int
	for i := 1; i-1 < h; i++ {
		for j := 1; j-1 < w; j++ {
			if 0 < c[i][j] {
				ans++
				dfs(c, i, j)
			}
		}
	}
	return ans, true
}

func dfs(c [][]int, y, x int) {
	c[y][x] = 0
	dx := []int{-1, 0, 1, -1, 1, -1, 0, 1}
	dy := []int{-1, -1, -1, 0, 0, 1, 1, 1}
	for i := range dx {
		nx := x + dx[i]
		ny := y + dy[i]
		if 0 < c[ny][nx] {
			dfs(c, ny, nx)
		}
	}
}

// -----------------------------------------------------------------------------

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

void dfs(vector< vector<int> > &island, int x, int y) {
    island[x][y] = 0;

    for (int dx = -1; dx <= 1; dx++) {
        for (int dy = -1; dy <= 1; dy++) {
            int nx = x + dx;
            int ny = y + dy;
            if (nx >= 0 && nx < island.size() && ny >= 0 && ny < island[0].size() && island[nx][ny] == 1) {
                dfs(island, nx, ny);
            }
        }
    }
}

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

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

        int res = 0;
        for (int i = 0; i < h; i++) {
            for (int j = 0; j < w; j++) {
                if (island[i][j] == 1) {
                    dfs(island, i, j);
                    res++;
                }
            }
        }

        cout << res << endl;
    }

    return 0;
}

C++: #include<stdio.h>
#include <stdlib.h>
#include<iostream>
#include<algorithm>
#include<cstdio>
#include<string>
#include<cmath>
#include<vector>
#include<stack>
#include<queue>
#include<time.h>
using namespace std;
#define FOR(i,a,b) for(ll i=a;i<b;i++)
#define vint vector<int> 
#define vvint vector<vint>
#define ct(a) cout<<a<<endl
typedef long long ll;

int w, h,cn[1000];
bool field[51][51];

void dfs(int i,int k) {
	field[i][k] = 0;
	FOR(x, -1, 2) {
		FOR(y, -1, 2) {
			int nx = k + x, ny = i + y;
			if (nx < w&&nx >= 0 && ny < h&&ny >= 0 && field[ny][nx] == 1)dfs(ny, nx);
		}
	}
	return;
}

int main(void)
{
	int c = 0;
	while (1) {
		cin >> w >> h;
		if (w == 0 && h == 0)break;
		FOR(i, 0, h)FOR(k, 0, w)cin >> field[i][k];

		FOR(i, 0, h)FOR(k, 0, w) {
			if (field[i][k] == 1) {
				dfs(i, k);
				cn[c]++;
			}
		}
		ct(cn[c]);
		c++;
	}

	return 0;
}

C++: #include <iostream>
#include <map>
// #include <cstdio>
#include <stack>

using namespace std;

int main() {
	while(1) {
	int w, h, data[52][52]={{0}};
	cin >> w >> h;
	if (w == 0 && h ==0) break;
	// memset(data, 0, sizeof(data));
	for (int i=1; i<=h; i++) {
		for (int j=1; j<=w; j++) {
			cin >> data[i][j];
		}
	}
	// for (int i=0; i<h; i++) {
	// 	for (int j=0; j<w; j++) {
	// 		cout << data[i][j] << "," ;
	// 	}cout <<endl;
	// }
	stack<pair<int, int> > s;
	int count=0;
	int H[8] = {-1, -1, -1, 0, 0, 1, 1, 1}, W[8] ={-1, 0, 1, -1, 1, -1, 0, 1};
	for(int i=1; i<=h; i++) {
		for (int j=1; j<=w; j++) {
			if (data[i][j]) {
				count++;
				s.push(make_pair(i, j));
				while (!(s.empty())) {
					pair<int, int> now;
					now = s.top();
					s.pop();
					data[now.first][now.second]=0;
					for (int k=0; k<8; k++) {
						if (data[H[k]+now.first][W[k]+now.second])
							s.push(make_pair(H[k]+now.first, W[k]+now.second));
					}
				}
			}
		}
	}
	cout << count <<endl;
}
}

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

const int W = 50, H = 50;
int w, h;
int board[H][W];

void search(int y, int x){
	board[y][x] = 0;
	for(int dx = -1; dx <= 1;++dx){
		for(int dy = -1; dy <= 1;++dy){
			int nx = x + dx, ny = y + dy;
			if(0 <= ny && 0 <= nx && nx < w && ny < h){
				if(board[ny][nx]) search(ny, nx);
			}
		}
	}
	return;
}

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

C++: #include <cstdio>

int map[50][50];
int w, h;
int ans;
int xx, yy;
const int dx[8] = {0, 1, 1, 1, 0, -1, -1, -1}, dy[8] = {-1, -1, 0, 1, 1, 1, 0, -1};

void Walk(int X, int Y) {
	map[Y][X] = 0;
	for(int i = 0; i < (sizeof(dx) / sizeof(dx[0])); i++) {
		xx = X + dx[i];
		yy = Y + dy[i];
		if(0 <= xx && xx < w && 0 <= yy && yy < h && map[yy][xx] == 1) {
			Walk(xx, yy);
		}
	}
}

int main(void) {
	while(1) {
		ans = 0;
		scanf("%d %d", &w, &h);
		if(!w && !h) break;
		for(int i = 0; i < h; i++) {
			for(int j = 0; j < w; j++) {
				scanf("%d", &map[i][j]);
			}
		}
		for(int i = 0; i < h; i++) {
			for(int j = 0; j < w; j++) {
				if(map[i][j] == 1) {
					ans++;
					Walk(j, i);
				}
			}
		}
		printf("%d\n", ans);
	}
	return 0;
}