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 w, h;

void dfs(vector<vector<short>>& masu, int x, int y) {
    for (int i = -1; i <= 1; i++) {
        for (int j = -1; j <= 1; j++) {
            if (0 <= x+i && x+i < w && 0 <= y+j && y+j < h) {
                if (masu[y+j][x+i]) {
//                    cout << "#: x=" << x+i << ", y="  << y+j << endl;
                    masu[y+j][x+i] = 0;
                    dfs(masu, x+i, y+j);
                }
            }
        }
    }
}

int main(void) {
    while (cin >> w >> h, w) {
        vector<vector<short>> masu(h, vector<short>(w));
        for (int i = 0; i < h; i++) {
            for (int j = 0; j < w; j++) {
                cin >> masu[i][j];
            }
        }

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

        cout << ans << endl;
    }

    return 0;
}

C++: #define _GLIBCXX_DEBUG
#include <bits/stdc++.h>
using namespace std;
#define rep(i, n) for (int i = 0; i < (int)(n); i++)
#define all(v) v.begin(), v.end()

const vector<int> di = {-1, -1, -1, 0, 0, 1, 1, 1};
const vector<int> dj = {-1, 0, 1, -1, 1, -1, 0, 1};

int w, h;
vector<vector<int>> field;

void dfs(int i, int j) {
  field[i][j] = 0;

  rep(dir, 8) {
    int ni = i + di[dir];
    int nj = j + dj[dir];

    if (ni < 0 || nj < 0 || ni >= h || nj >= w) continue;
    if (field[ni][nj] == 0) continue;
    dfs(ni, nj);
  }
}

int main() {
  vector<int> ans;

  while (1) {
    cin >> w >> h;
    if (w == 0 && h == 0) break;

    field.assign(h, vector<int>(w));
    rep(i, h) rep(j, w) { cin >> field[i][j]; }

    int count = 0;

    rep(i, h) rep(j, w) {
      if (field[i][j] == 0) continue;
      dfs(i, j);
      count++;
    }

    ans.push_back(count);
  }

  for (auto &&i : ans) {
    printf("%d\n", i);
  }
}

C++: #include <iostream>
#include <queue>
using namespace std;
struct Point{int x,y;};
int main(){
  int w,h,p[60][60]={},v[60][60]={},i,j,k,o;
  queue <Point> q;
  Point n,b;
  cin >> w >> h;
  while(w!=0){
    for(j=0;j<h;j++){
      for(i=0;i<w;i++){
	cin >> p[i][j];
	v[i][j]=0;
      }
    }
    o=0;
    for(j=0;j<h;j++){
      
      for(i=0;i<w;i++){
	if(p[i][j]==1){
	  o++;
	  n.x=i;n.y=j;
	  q.push(n);
	  p[i][j]=0;
	  while(!q.empty()){
	    n=q.front();
	    q.pop();
	    if(n.x+1<w&&p[n.x+1][n.y]==1) {
	      b.x=n.x+1;b.y=n.y;
	      p[n.x+1][n.y]=0;
	      q.push(b);
	    }
	    if(n.y+1<h&&p[n.x][n.y+1]==1) {
	      b.x=n.x;b.y=n.y+1;
	      p[n.x][n.y+1]=0;
	      q.push(b);
	    }
	    if(n.x-1>=0&&p[n.x-1][n.y]==1) {
	      b.x=n.x-1;b.y=n.y;
	      p[n.x-1][n.y]=0;
	      q.push(b);
	    }
	    if(n.y-1>=0&&p[n.x][n.y-1]==1) {
	      b.x=n.x;b.y=n.y-1;
	      p[n.x][n.y-1]=0;
	      q.push(b);
	    }
	    if(n.x+1<w&&n.y+1<h&&p[n.x+1][n.y+1]==1) {
	      b.x=n.x+1;b.y=n.y+1;
	      p[n.x+1][n.y+1]=0;
	      q.push(b);
	    }
	    if(n.x-1>=0&&n.y-1>=0&&p[n.x-1][n.y-1]==1) {
	      b.x=n.x-1;b.y=n.y-1;
	      p[n.x-1][n.y-1]=0;
	      q.push(b);
	    }
	    if(n.x+1<w&&n.y-1>=0&&p[n.x+1][n.y-1]==1) {
	      b.x=n.x+1;b.y=n.y-1;
	      p[n.x+1][n.y-1]=0;
	      q.push(b);
	    }
	    if(n.x-1>=0&&n.y+1<h&&p[n.x-1][n.y+1]==1) {
	      b.x=n.x-1;b.y=n.y+1;
	      p[n.x-1][n.y+1]=0;
	      q.push(b);
	    }
	  }
	}
      }
    }
    cout << o << endl;
    cin >> w >> h;
  }
  return 0;
}