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<vector>
using namespace std;
int needx[]={-1,0,1,0,1,1,-1,-1};
int needy[]={0,1,0,-1,1,-1,1,-1};
//??¶?????¨???false????????????true???????????¢??°//
bool land(int x,int y,vector<vector<bool> >& cango){
    if(cango[x][y]){
        cango[x][y]=false;
        for(int i=0;i<8;i++){
            int nextx=x+needx[i];
            int nexty=y+needy[i];
            if(cango[nextx][nexty])
                bool flag=land(nextx,nexty,cango);
            }
    return true;
     }else{
return false;
    }
}
int main(void){
    int input;
	int w,h;
	while(1){
cin>>w>>h;
if(h==0)break;
    vector<vector<bool> > cango(h+2,vector<bool>(w+2,false));
    for(int i=1;i<=h;i++){
        for(int j=1;j<=w;j++){
            cin >>input;
            if(input==1){
cango[i][j]=true;
            }
        }
    }
  
    int t=0;
for(int i=1;i<=w;i++){
    for(int j=1;j<=h;j++){
        if(land(i,j,cango)){
            t++;
        }
    }
}
cout <<t<<endl;
	}
return 0;
}

C++: #include <bits/stdc++.h>
using namespace std;
bool d[51][51];
int a,b,s,_x,_y,dx[8]={0,-1,-1,-1,0,1,1,1},dy[8]={1,1,0,-1,-1,-1,0,1};
#define r(z,x) for(int z=0;z<x;z++)
void bfs(int x,int y){
    if(0>x||x>=b||0>y||y>=a)return;
    d[x][y]=0;
    r(i,8)if(d[_x=x+dx[i]][_y=y+dy[i]])bfs(_x,_y);
}
int main(){
    while(cin>>a>>b,a||b){
        s=0;
        r(i,b)r(j,a)cin>>d[i][j];
        r(i,b)r(j,a)if(d[i][j])bfs(i,j),s++;
        cout<<s<<endl;
    }
}

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

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

void solve(int x, int y){
	map[x][y] = 0;

	for (int i = 0; i < 8; i++){
		int X = x + dx[i];
		int Y = y + dy[i];
		if (0 <= X&&X < w && 0 <= Y&&Y < h&&map[X][Y] != 0) solve(X, Y);
	}
}

int main(){
	while (cin >> w >> h, w + h != 0){
		ans = 0;
		for (int y = 0; y < h; y++){
			for (int x = 0; x < w; x++){
				cin >> map[x][y];
			}
		}

		for (int y = 0; y < h; y++){
			for (int x = 0; x < w; x++){
				if (map[x][y] == 1){
					solve(x, y);
					ans++;
				}
			}
		}
		cout << ans << endl;
	}

	return 0;
}

C++: #include<iostream>
#include<algorithm>
#include<cstring>
using namespace std;
int H,W,k[100][100],n[100][100],color=1,i,j,l,m,o,r;
int main(){while(true){memset(n,0,sizeof(n));r=0;cin>>H>>W;if(H==0){break;}for(i=0;i<H;i++){for(j=0;j<W;j++){cin>>k[i][j];}}
for(i=0;i<H;i++){for(j=0;j<W;j++){if(r==1){color++;}r=0;if(k[i][j]==1 && n[i][j]==0){n[i][j]=color;for(l=0;l<20;l++){
	for(m=0;m<H;m++){for(o=0;o<W;o++){if(n[m][o]==color){
		for(int p=-1;p<=1;p++){for(int q=-1;q<=1;q++){if(m-p>=0 && m-p<H){if(o-q>=0 && o-q<W){n[m][o]=color;r=1;}}}}
	}}}
}}}}
cout<<color<<endl;}
}

C++: #include "bits/stdc++.h"

using namespace std;
using ll = long long int;
using ullong = unsigned long long;
#define rep(i,n) for(int i = 0; i < n; i++)
#define FOR(i, a, b)  for(int i = (a); i < (b) ; i++)
#define pb push_back
#define SORT(v,n) sort(v, v+n)
#define ALL(x) (x).begin(),(x).end()
#define debug(x) cerr << #x << ": " << x << '\n'
#define elif else if
//#define llint

#ifdef llint
#define int ll
static const int INF = numeric_limits<long long>::max();
#else
static const int INF = numeric_limits<int>::max();
#endif // llint
static const int MOD = (int)1e9 + 7;
const double EPS = 1e-9;
int dx[8] = { 1, 0, -1, 0, 1, -1, -1, 1 };
int dy[8] = { 0, 1, 0, -1, 1, 1, -1, -1 };

int w, h;

void dfs(int x,int y,vector<vector<int>>& map) {
	map[y][x] = 0;
	//rep(y, h) {
	//	rep(x, w) {
	//		cout << map[y][x] << " ";
	//	}
	//	cout << endl;
	//}
	//cout << endl;
	//cout << "x : " << x << " y : " << y << endl;
	rep(i, 8) {
		int tx = x + dx[i];
		int ty = y + dy[i];
		//cout << "nx : " << tx << " ny : " << ty << endl;
		if (tx >= 0 and tx < map[0].size() and ty >= 0 and ty < map.size()) {
			if(map[ty][tx] == 1)
				dfs(tx, ty, map);
		}
	}
	return;
}

signed main() {
	ios::sync_with_stdio(true);
	while (true) {
		cin >> w >> h;
		if (w == 0 and h == 0) break;
		vector<vector<int>>  map(h);
		rep(i, h) {
			rep(j, w) {
				int x;
				cin >> x;
				map[i].pb(x);
			}
		}
		int ans = 0;
		rep(y,h){
			rep(x, w) {
				if (map[y][x] == 1) {
					dfs(x, y, map);
					ans++;
				}
			}
		}
		cout << ans << endl;
	}
	return 0;
}

C++: #include<bits/stdc++.h>
using namespace std;
#define rep(i, n) for(int i = 0; i < (int)(n); i++)
using ll = long long;
using pint = pair<int,int>;

int w, h;
int field[55][55];

void dfs(int sh, int sw){
    for(int dx = -1; dx <= 1; dx++){
        for(int dy = -1; dy <= 1; dy++){
            int nh = sh + dx, nw = sw + dy;
            if(nh < 0 || h <= nh || nw < 0 || w <= nw) continue;
            if(field[nh][nw] == 0) continue;
            field[nh][nw] = 0;
            dfs(nh, nw);
        }
    }
}

void solve(){
    int cnt = 0;
    for(int i = 0; i < h; i++){
        for(int j = 0; j < w; j++){
            if(field[i][j] == 0) continue;
            field[i][j] = 0;
            dfs(i,j);
            cnt++;
        }
    }

    cout << cnt << endl;
}

int main(){
    while(cin >> w >> h){
        if(h == 0 || w == 0) break;
        rep(i,h)rep(j,w) cin >> field[i][j];
        solve();
    }
}