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

using namespace std;



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

int W, H;
int field[50][50];

void dfs(int field[50][50], int sx, int sy) {

    field[sy][sx] = 0;

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

        if (0 <= nx && nx < W && 0 <= ny && ny < H && field[ny][nx] == 1) {
            dfs(field, nx, ny);
        }
    }
}

int main(void) {
    
    while (true) {
        cin >> W >> H;

        if (W == 0 && H == 0) break;
        
        for (int i = 0; i < H; i++) {
            for (int j = 0; j < W; j++) {
                cin >> field[i][j];
            }
        }

        int count = 0;

        for (int i = 0; i < H; i++) {
            for (int j = 0; j < W; j++) {
                if (field[i][j] == 1) {
                    dfs(field, j, i);
                    count++;
                }
            }
        }

        cout << count << endl;
    }

    return 0;
}

C++: #define	_USE_MATH_DEFINES
#include <iostream>
#include <algorithm>
#include <functional>
#include <vector>
#include <cstdio>
#include <string>
#include <cmath>
#include <cfloat>
#include <map>
#include <queue>
#include <stack>
#include <list>
using namespace std;
int x,y;
int o[51][51];
queue<int> q;
void tansaku(int j,int i){
	if(j<1||j>x||i<1||i>y||o[i][j]==0||o[i][j]==2){
		return ;
	}
	if(o[i][j]==1){
		if(q.empty())
		q.push(1);
		o[i][j]=2;
	}
	
	tansaku(j+1,i);
	tansaku(j-1,i);
	tansaku(j,i+1);
	tansaku(j,i-1);
	tansaku(j+1,i+1);
	tansaku(j-1,i-1);
	tansaku(j+1,i-1);
	tansaku(j-1,i+1);
}
int main(){

	while(cin>>x>>y,x!=0||y!=0){
		int count=0;
		for(int i=1;i<=y;i++){
			for(int j=1;j<=x;j++){
				cin>>o[i][j];
			}
		}
		for(int i=1;i<=y;i++){
			for(int j=1;j<=x;j++){
				tansaku(j,i);
				if(!q.empty()){
				count+=q.front();
				}
				q.pop();
			}
		}
		
		cout<<count<<endl;
	}

}

C++: #include<iostream>
#include<algorithm>
#include<vector>
#include<numeric>
#include<string>
#include<cmath>
#include<set>
#include<queue>
#include<bitset>
using namespace std;
const int inf = 1001001000;

void dfs(vector<vector<int>> &graph, int v, set<int> &seen){
	seen.insert(v);
	for (int i=0;i<graph[v].size();i++){
		int nxt = graph[v][i];
		if (seen.count(nxt)) continue;
		dfs(graph, nxt, seen);
	}
}

void print(vector<vector<int>> a){
	for (int i=0;i<a.size(); i++){
		if (a[i].size() == 0) continue;
		for (int j=0;j<a[i].size();j++){
			cout << a[i][j] << " ";
		}
		cout << endl;
	}
}

int main(){
	while(true){
		int w,h; cin >> w >> h;
		if (w == 0 && h == 0) break;
		vector<vector<int>> map(h+2,vector<int>(w+2,0));
		for (int i=1;i<h+1;i++){
			for (int j=1;j<w+1;j++){
				int c; cin >> c;
				map[i][j] = c;
			}
		}
		vector<vector<int>> graph;
		int r = h+2,c=w+2;
		for (int i = 0; i < r; i++){
			for (int j = 0; j < c;j++){
				vector<int> a;
				if (map[i][j] == 1){	
					for (int k = -1; k < 2; k++){
						for (int l = -1; l < 2; l++){
							if (map[i+k][j+l] == 1) a.push_back((i+k)*c+j+l);
						}
					}
					
				}
				graph.push_back(a);
			}
		}
		set<int> gseen;
		int ans = 0;
		for (int i = 0; i < graph.size(); i++){
			set<int> seen;
			if (graph[i].size() == 0) continue;
			if (gseen.find(i) == gseen.end()){
				dfs(graph,i,seen);
				ans++;
				set_union(seen.begin(),seen.end(),gseen.begin(),gseen.end(),inserter(gseen,gseen.end()));
			}
			
		}
		cout << ans << endl;
	}
	
	return 0;
}

C++: #include <bits/stdc++.h>

#define INF INT_MAX
#define ll long long

using namespace std;

int main(){
	while(true){
		int w,h;
		cin >> w >> h;
		if(w == 0 && h == 0)
			break;
		int c[h][w];
		//??\???
		for(int j = 0;j < h;j++){
			for(int i = 0;i < w;i++){
				cin >> c[j][i];
			}
		}

		//??????????????°????????????
		int ans = 0;
		for(int j = 0;j < h;j++){
			for(int i = 0;i < w;i++){
				if(c[j][i] == 1){
					ans++;
					vector< pair<int,int> > v;
					v.push_back(make_pair(j,i));
					while(!v.empty()){
						int y = v.back().first, x = v.back().second;
						c[y][x] = 0;	//????????¶????????????
						v.pop_back();
						for(int dirY = -1;dirY < 2;dirY++){
							for(int dirX = -1;dirX < 2;dirX++){
								int yy = y + dirY,xx = x + dirX;
								if((0 <= yy && yy < h) && (0 <= xx && xx < w) && (c[yy][xx] == 1)){
									v.push_back(make_pair(yy,xx));
								}
							}
						}
					}
				}
			}
		}
		cout << ans << endl;
	}
	return 0;
}