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<iostream>
#include<queue>
#include<cstring>
using namespace std;
int x[50][50], col[50][50], h, w, c, i, j, k, l, p, q;
queue<int>Q, R;
int main() {
	while (true) {
		c = 0;
		memset(col, 0, sizeof(col));
		cin >> w >> h;
		if (h == 0) { break; }
		for (i = 0; i < h; i++) { for (j = 0; j < w; j++) { cin >> x[i][j]; } }
		for (i = 0; i < 50; i++) {
			for (j = 0; j < 50; j++) {
				if (x[i][j] == 1 && col[i][j] == 0) {
					c++;
					col[i][j] = c;
					Q.push(i * 100 + j);
					while (!Q.empty()) {
						while (!Q.empty()) {
							p = Q.front() / 100;
							q = Q.front() % 100;
							for (k = -1; k < 2; k++) {
								for (l = -1; l < 2; l++) {
									if (x[p + k][q + l] == 1 && col[p + k][q + l] == 0) {
										col[p + k][q + l] = c;
										R.push((p + k) * 100 + q + l);
									}
								}
							}
							Q.pop();
						}
						while (!R.empty()) {
							Q.push(R.front());
							R.pop();
						}
					}
				}
			}
		}
		cout << c << endl;
	}
}

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

int state[100][100];
bool reached[100][100];
int w,h;

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

void dfs(int x, int y) {

	if(x==0 || w<x || y==0 || h<y || state[x][y] == 0)
		return;
	if(reached[x][y] == true)
		return;

	reached[x][y]=true;

	for(int i=0; i<8; i++) {
		dfs(x+dx[i],y+dy[i]);
	}
}

int main() {

	memset(state, 0, sizeof(state));
	memset(reached, false, sizeof(reached));

	cin >> w >> h;
	for(int i=1; i<=w; i++) {
		for(int j=1; j<=h; j++) {
			cin >> state[i][j];
		}
	}
	int count=0;
	for(int i=1; i<=w; i++) {
		for(int j=1; j<=h; j++) {
			if(reached[i][j]==true || state[i][j] == 0)
				continue;
			else{
				dfs(i,j);
				count++;
			}
		}
	}
	cout << count << endl;
	return 0;
}

C++: #include <bits/stdc++.h>
using namespace std;
#define FOR(i,o,n) for(long long i = o;i<n;i++)
#define oneforall ios::sync_with_stdio(false);cin.tie(0);
#define all(v) (v).begin(),(v).end()
#define ini(...) int __VA_ARGS__; in(__VA_ARGS__)
#define inl(...) long long __VA_ARGS__; in(__VA_ARGS__)
#define ins(...) string __VA_ARGS__; in(__VA_ARGS__)
#define int long long 
const long long inf=1e18;
void in(){} template <typename T,class... U> void in(T &t,U &...u){ cin >> t; in(u...);}
void out(){cout << "\n";} template <typename T,class... U> void out(const T &t,const U &...u){ cout << t; if(sizeof...(u)) cout << " "; out(u...);}
typedef vector<int> vi;
typedef vector<long long> vl;
typedef long long ll;
typedef vector<pair<long, long > > vpll;
typedef vector<pair<int, int > > vpii;
#define FORR(x,arr) for(auto& x:arr)
#define ZERO(a) memset(a,0,sizeof(a))





//3111111111111111111111111111111

	int x,x1;
	int morty[310][310];
	int tmpx[3] = {-1,1,0};
	void dfs(int tmp,int tmp1){
		morty[tmp][tmp1] = 0;
		FOR(i,0,3)FOR(i1,0,3){
			int nowtmp = tmp+tmpx[i];
			int nowtmp1 = tmp1+tmpx[i1];
			if(!(nowtmp == tmp and nowtmp1 == tmp1) and nowtmp>=0 and nowtmp<=x-1 and nowtmp1>=0 and nowtmp1<=x1-1 and morty[nowtmp][nowtmp1] == 1){
				dfs(nowtmp,nowtmp1);
			}
		}
	}




	void solve(){
		
		while(1){
			
			in(x1,x);
			if(x == 0 and x1 == 0)break;
			FOR(i,0,x)FOR(i1,0,x1){
				ini(tmp);
				if(tmp == 0){
					morty[i][i1] = 0;
				}
				else{
					morty[i][i1] = 1;
				}
			}
			int rick = 0;
			//FOR(i,0,x){
			//	FOR(i1,0,x1){
				//cout<<morty[i][i1]<<" ";			
			//	}
				//out();
			//}
			FOR(i,0,x)FOR(i1,0,x1){
				if(morty[i][i1] == 1){
					dfs(i,i1);
					rick++;
				}
			}
			out(rick);
		}








		
		
	}























int32_t main() {
	oneforall
	oneforall
	oneforall
	oneforall
	oneforall
	oneforall
	oneforall
	oneforall
	oneforall
	oneforall
	oneforall
	oneforall
	oneforall 
	oneforall

	solve();
	


	return 0;
}

C++: #include <bits/stdc++.h>
using namespace std;
int w,h,c[100][100],ans;
pair<int,int> movemy[8]={{0,1},{1,0},{1,1},{-1,0},{0,-1},{-1,1},{1,-1},{-1,-1}};


void land(int y,int x){
	if(y<0||y>h-1||x<0||x>w-1||c[y][x]==0)return;
	c[y][x]=0;
	for(int i=0;i<8;i++){
		land(y+movemy[i].first,x+movemy[i].second);
	}
}

int main(){
	while (1){
		ans=0;
		cin>>w>>h;
		if(w==0||h==0)break;
		for(int i=0;i<h;i++){
			for(int j=0;j<w;j++){
				cin>>c[i][j];
				//cout<<"AAA"<<endl;
			}
		}
		//cout<<"AAA"<<endl;
		for(int i=0;i<h;i++){
			for(int j=0;j<w;j++){
				if(c[i][j]){land(i,j);ans++;}
				
			}
		}
		cout<<ans<<endl;
	}
}

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

using i64 = long long int;

int c[51][51] = {0};

void bfs(int w, int h, int x, int y){
    if(x < 0 || x >= w)return;
    if(y < 0 || y >= h)return;
    if(c[y][x] == 0)return;

    c[y][x] = 0;
    for(int dy=-1;dy<=1;++dy){
        for(int dx=-1;dx<=1;++dx)
            bfs(w, h, x+dx, y+dy);
    }
}

int main(){
    int w, h;
    while(cin >> w >> h, w != 0){
        for(int i=0;i<h;++i){
            for(int j=0;j<w;++j)
                cin >> c[i][j];
        }

        int ans = 0;
        for(int i=0;i<h;++i){
            for(int j=0;j<w;++j){
                if(c[i][j] == 1){
                    bfs(w, h, j, i);
                    ans++;
                }
            }
        }

        cout << ans << endl;
    }

    return 0;
}

C++: #include <iostream>
#include <vector>
#include <algorithm>
#include <utility>
#include <queue>
#include <set>
#include <stdio.h>
#include <string>
using namespace std;
#define ll long long

int W, H;
vector<vector<int>> field;

void dfs(int h, int w) {
	field[h][w] = 0;

	for (int dx = -1; dx <= 1; dx++) {
		for (int dy = -1; dy <= 1; dy++) {
			int nh = h + dy;
			int nw = w + dx;

			if (nh < 0 || nw < 0 || nh >= H || nw >= W)continue;
			if (field[nh][nw] == 0)continue;

			dfs(nh, nw);
		}
	}
}

int main() {
	while (1) {
		cin >> W >> H;
		if (W == 0 && H == 0)break;
		field.assign(H, vector<int>(W, 0));
		for (int i = 0; i < H; i++) {
			for (int j = 0; j < W; j++) {
				cin >> field[i][j];
			}
		}
		int ans = 0;
		for (int i = 0; i < H; i++) {
			for (int j = 0; j < W; j++) {
				if (field[i][j] == 0)continue;
				dfs(i, j);
				ans++;
			}
		}
		cout << ans << endl;
	}
	return 0;
}