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 <bits/stdc++.h>
using namespace std;

#define FOR( i, m, n )  for( int (i) = (m); (i) < (n); (i)++ )
#define REP( i, n )     FOR( i, 0, n )
#define ALL( a )        (a).begin(), (a).end()             

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

int field[50][50] = {};

int w, h;

void dfs( int x, int y );

int main() {

	while( cin >> w >> h, w ) {
		REP( i, h ) {
			REP( j, w ) {
				cin >> field[j][i];
			}
		}

		int cnt = 0;
		REP( i, h ) {
			REP( j, w ) {
				if( field[j][i] == 1 ) {
					cnt++;
					dfs( j, i );
				}
			}
		}

		cout << cnt << endl;

	}

}

void dfs( int x, int y ) {
	field[x][y] = 0;
	REP( i, 8 ) {
		if( x + dx[i] >= 0 && x + dx[i] < w &&
			y + dy[i] >= 0 && y + dy[i] < h &&
			field[x + dx[i]][y + dy[i]] == 1 ) {

			dfs( x + dx[i], y + dy[i] );

		}

	}
}

C++: #include <bits/stdc++.h>
using namespace std;
#define rep(i,n) for(ll i=0; i<n; i++)
typedef long long ll;
typedef long double ld;
typedef long long unsigned int llu;
ll MOD = 1000000007;
ll INF = 1000000009;

int c[55][55];
bool v[55][55];

void solve(){

    int w,h;
    vector<int> a;

    while(true){
        cin >> w >> h;
        if(w==0&&h==0) break;
        rep(i,h){
            rep(j,w){
                v[i][j]=false;
            }
        }
        rep(i,h) v[i][w]=true;
        rep(j,w) v[h][j]=true;
        v[h][w]=true;

        rep(i,h){
            rep(j,w){
                cin >> c[i][j]; 
                if(c[i][j]==0) v[i][j]=true;
            }
        }

        int ans=0;
            
            rep(i,h){
                rep(j,w){
                    if(!v[i][j]) {
                        stack<pair<int,int>> st;
                        st.push(make_pair(i,j));
                        while(!st.empty()){
                            int ti = st.top().first;
                            int tj = st.top().second;
                            st.pop();
                            v[ti][tj]=true;
                            
                            if(v[ti+1][tj]==false) st.push(make_pair(ti+1,tj));
                            if(v[ti][tj+1]==false) st.push(make_pair(ti,tj+1));
                            if(ti>0) {if(v[ti-1][tj]==false) st.push(make_pair(ti-1,tj));}
                            if(tj>0) {if(v[ti][tj-1]==false) st.push(make_pair(ti,tj-1));}
                            if(v[ti+1][tj+1]==false) st.push(make_pair(ti+1,tj+1));
                            if(ti>0) {if(v[ti-1][tj+1]==false) st.push(make_pair(ti-1,tj+1));}
                            if(tj>0) {if(v[ti+1][tj-1]==false) st.push(make_pair(ti+1,tj-1));}
                            if(ti>0&&tj>0) {if(v[ti-1][tj-1]==false) st.push(make_pair(ti-1,tj-1));}
                            
                        }
                        ans++;
                    }
                }
            }

        a.emplace_back(ans);
        


    }

    for(auto e:a){
        cout << e << endl;
    }

}

int main(){
  cin.tie(0);
  ios::sync_with_stdio(false);
  solve();
  return 0;
}

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

}