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>
#include <queue>

using namespace std;

int main(void) {
	int w,h;
	while(cin >> w >> h && w && h) {
		vector< vector<int> > data(w+2,vector<int>(h+2));
		vector< vector<bool> > check(w+2,vector<bool>(h+2,false));
		queue<int> sx,sy;
		for (int i = 0; i < h + 2; ++i) {
			for (int j = 0; j < w + 2; ++j) {
				if (i == 0 || i == h+1 || j == 0 || j == w+1) {
					data[i][j] = 0;
				}
				else {
					cin >> data[i][j];
					if (data[i][j] == 1) {
						sx.push(j);
						sy.push(i);
					}
				}
			}
		}

		int nx,ny;
		int count = 0;
		queue<int> fx,fy;
		while(!sx.empty()) {
			nx = sx.front();
			ny = sy.front();
			sx.pop();
			sy.pop();
			if (!check[ny][nx]) {
				check[ny][nx] = true;
				fx.push(nx);
				fy.push(ny);
				++count;
			}
			while (!fx.empty()) {
				nx = fx.front();
				ny = fy.front();
				fx.pop();
				fy.pop();
				//cout << ny << " " << nx << endl;
				if (!check[ny+1][nx] && data[ny+1][nx] == 1) {
					check[ny+1][nx] = true;
					fx.push(nx);
					fy.push(ny+1);
				}
				if (!check[ny-1][nx] && data[ny-1][nx] == 1) {
					check[ny-1][nx] = true;
					fx.push(nx);
					fy.push(ny-1);
				}
				if (!check[ny+1][nx+1] && data[ny+1][nx+1] == 1) {
					check[ny+1][nx+1] = true;
					fx.push(nx+1);
					fy.push(ny+1);
				}
				if (!check[ny+1][nx-1] && data[ny+1][nx-1] == 1) {
					check[ny+1][nx-1] = true;
					fx.push(nx-1);
					fy.push(ny+1);
				}
				if (!check[ny-1][nx-1] && data[ny-1][nx-1] == 1) {
					check[ny-1][nx-1] = true;
					fx.push(nx-1);
					fy.push(ny-1);
				}
				if (!check[ny-1][nx+1] && data[ny-1][nx+1] == 1) {
					check[ny-1][nx+1] = true;
					fx.push(nx+1);
					fy.push(ny-1);
				}
				if (!check[ny][nx+1] && data[ny][nx+1] == 1) {
					check[ny][nx+1] = true;
					fx.push(nx+1);
					fy.push(ny);
				}
				if (!check[ny][nx-1] && data[ny][nx-1] == 1) {
					check[ny][nx-1] = true;
					fx.push(nx-1);
					fy.push(ny);
				}
			}
		}
		cout << count << endl;
	}
	return 0;
}

C++: #include <bits/stdc++.h>
#define rep(i, a, n) for(int i = a; i < n; i++)
#define repb(i, a, b) for(int i = a; i >= b; i--)
#define all(a) a.begin(), a.end()
#define int long long
using namespace std;

int w, h;
char c[51][51];
bool f[51][51];
int ans = 0;


bool contain(int y, int x){
    return (0 <= y && y < h && 0 <= x && x < w);
}

void dfs(int y, int x){
    f[y][x] = true;
    if(c[y][x] == '0') return;
    rep(i, 0, 3){
        rep(j, 0, 3){
            int ny = y + i - 1;
            int nx = x + j - 1;
            if(contain(ny, nx) && f[ny][nx] == false){
                dfs(ny, nx);
            }
        }
    }
    return;
}

signed main(){
    while(cin >> w >> h){
        if(w + h == 0) break;
        rep(i, 0, h){
            rep(j, 0, w){
                cin >> c[i][j];
            }
        }
        rep(i, 0 ,51){
            rep(j, 0, 51){
                f[i][j] = false;
            }
        }
        // cout << -1 << endl;
        ans = 0;
        rep(i, 0, h){
            rep(j, 0 ,w){
                if(c[i][j] == '1' && f[i][j] == false){
                    // cout << i << ' ' << j << endl;
                    dfs(i, j);
                    ans++;
                }
            }
        }
        cout << ans << endl;
    }

}

C++: #include<iostream>
#include<vector>
#include<algorithm>
#include<cmath>
#include<ctime>

using namespace std;
using ll = long long;

int dir[2][8] = {{-1, -1, -1, 0, 1, 1, 1, 0}, {-1, 0, 1, -1, -1, 0, 1, 1}};

void dfs(int i, int j, vector<vector<int>>& used, vector<vector<int>>& color){
  used[i][j] = 1;
  for(int d = 0; d < 8; d++){
    if(i + dir[0][d] >= 0 && i + dir[0][d] < used.size() && j + dir[1][d] >= 0 && j + dir[1][d] < used[0].size()){
      if(!used[i+dir[0][d]][j+dir[1][d]] && color[i+dir[0][d]][j+dir[1][d]]){
        dfs(i + dir[0][d], j + dir[1][d], used, color);
      }
    }
  }
}

int main(){
  cin.tie(0);
  ios::sync_with_stdio(false);
  int ti = clock();
  // start-----------------------------------------------
  int w, h;
  while(cin >> w >> h && w){
    vector<vector<int> > used(h, vector<int>(w, 0));
    vector<vector<int> > color(h, vector<int>(w, 0));
    int ans = 0;
    for(int i = 0; i < h; i++){
      for(int j = 0; j < w; j++){
        cin >> color[i][j];
      }
    }
    for(int i = 0; i < h; i++){
      for(int j = 0; j < w; j++){
        if(!used[i][j] && color[i][j]) ans++, dfs(i, j, used, color);
      }
    }
    cout << ans << endl;
  }

  // end-----------------------------------------------
  // cerr << 1.0 * (clock() - ti) / CLOCKS_PER_SEC << endl;
}

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

int w, h, ans, tmp;
vector<vector<int>> c(60, vector<int>(60,0));

void ume(int x, int y){
	if(c[x][y]==0) return;
	c[x][y]=0;
	ume(x-1,y);
	ume(x+1,y);
	ume(x-1,y-1);
	ume(x,y-1);
	ume(x+1,y-1);
	ume(x-1,y+1);
	ume(x,y+1);
	ume(x+1,y+1);
	return;
}

bool hantei(int x, int y){
	if(c[x][y]==1){
		ume(x,y);
		return 1;
	}
	else return 0;
}

int main(){
	while(cin >> w >> h){
		if(w == 0&&h==0) break;
		ans = 0;
		for(int i = 1;i<=h;i++){
			for(int j = 1;j<=w;j++){
				cin >> c[i][j];
			}
		}
		for(int i = 1;i<=h;i++){
			for(int j = 1;j<=w;j++){
				ans+=hantei(i,j);
			}
		}
		cout << ans << endl;
	}
	return 0;
}

C++: #include<stdio.h>
#include<string>
#include<iostream>
using namespace std;
int a[55][55]={};
int w,h;
int count=0;
void dfs(int y,int x){
  a[y][x]=0;
  int dy[8]={-1,-1,-1,0,0,1,1,1};
  int dx[8]={-1,0,1,-1,1,-1,0,1};
  for(int i=1;i<8;i++){
    int ny=y+dy[i];
    int nx=x+dx[i];
    if(nx<0||ny<0||nx>=w||ny>=h)continue;
    if(a[ny][nx]==0)continue;
    dfs(ny,nx);
  }
}
int main(){
  while(1){
    int 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>>a[i][j];
      }
    }
    for(int i=0;i<h;i++){
      for(int j=0;j<w;j++){
	if(a[i][j]==0)continue;
	ans++;
	dfs(i,j);
      }
    }
    cout<<ans<<endl;
  }
}