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<algorithm>
#include<string>
#include<map>
using namespace std;
typedef long long ll;
typedef vector<int> VI;
#define REP(i,n) for(int i=0;i<n;i++)
#define FOR(i,a,b) for(int i=(a);i<(b);++i)
#define SORT(c) sort((c).begin(),(c).end())
#define INF 1000000000
#define mod 1000000007
int H,W;
int hw[50][50];
void dfs(int x,int y){
	hw[x][y]=0;
	for(int dx=-1;dx<=1;dx++){
		for(int dy=-1;dy<=1;dy++){
			int nx=x+dx,ny=y+dy;
			if(0<=nx && nx<H && 0<=ny && ny<W && hw[nx][ny]==1) dfs(nx,ny);
		}
	}
	return;
}

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

	st:
	int ans=0;
	cin>>W>>H;
	if(H==0 && W==0) return 0;
	REP(i,H){
		REP(j,W){
			cin>>hw[i][j];
		}
	}
	REP(i,H){
		REP(j,W){
			if(hw[i][j]==1){
				ans++;
				//cout<<i<<" "<<j<<endl;
				dfs(i,j);
			}
		}
	}
	
	cout<<ans<<endl;
	goto st;
		
	return 0;
}

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

typedef long long int ll;
typedef pair<int, int> pii;
typedef vector<int> vi;
typedef vector<pair<int, int> > vii;
#define rrep(i, m, n) for(int (i)=(m); (i)<(n);  (i)++)
#define erep(i, m, n) for(int (i)=(m); (i)<=(n); (i)++)
#define  rep(i, n)    for(int (i)=0; (i)<(n);  (i)++)
#define rrev(i, m, n) for(int (i)=(n)-1; (i)>=(m); (i)--)
#define erev(i, m, n) for(int (i)=(n); (i)>=(m); (i)--)
#define  rev(i, n)    for(int (i)=(n)-1; (i)>=0; (i)--)
#define vrep(i, c)    for(__typeof((c).begin())i=(c).begin(); i!=(c).end(); i++)
#define  ALL(v)       (v).begin(), (v).end()
#define pb            push_back
template<class T, class S> inline pair<T, S> mp(T x, S y){ return make_pair(x, y); }
template<class T, class S> inline bool minup(T& m, S x){ return m>(T)x ? (m=(T)x, true) : false; }
template<class T, class S> inline bool maxup(T& m, S x){ return m<(T)x ? (m=(T)x, true) : false; }

static const int    INF = 1000000000;
static const ll     MOD = 1000000007LL;
static const double EPS = 1E-12;

const int MAX_W = 50;
const int MAX_H = 50;
int w, h;
int c[MAX_W][MAX_H+1];

inline bool check(int hx, int hy){ return 0 <= hx && hx < w && 0 <= hy && hy < h; }

int main()
{
  while(cin >> h >> w, w){
    int res = 0;
    rep(i, w) rep(j, h) cin >> c[i][j];

    rep(i, w) rep(j, h) if(c[i][j]){
      stack<pii> st;
      st.push(mp(i, j));
      c[i][j] = 0;
      res += 1;
      while(!st.empty()){
        pii here = st.top();  st.pop();
        int hx = here.first;
        int hy = here.second;
        erep(dx, -1, 1) erep(dy, -1, 1) if(dx || dy){
          int nx = hx + dx;
          int ny = hy + dy;
          if(!check(nx, ny)) continue;
          if(!c[nx][ny]) continue;
          st.push(mp(nx, ny));
          c[nx][ny] = 0;
        }
      }
    }

    cout << res << endl;
  }

  return 0;
}

C++: #include <iostream>

int h, w;
bool island[51][51];

bool func(int y, int x){
	if(!island[y][x]) return false;
	island[y][x] = false;

	for(int i = -1; i <= 1; ++i){
		for(int j = -1; j <= 1; ++j){
			if(i == 0 && j == 0) continue;
			if(y + i < 0 || y + i >= h || x + j < 0 || x + j >= w) continue;
			if(!island[y + i][x + j]) continue;
			func(y + i, x + j);
		}
	}

	return true;
}

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

		int cnt = 0;
		for(int i = 0; i < h; ++i){
			for(int j = 0; j < w; ++j){
				cnt += func(i, j);
			}
		}

		std::cout << cnt << std::endl;
	}

	return 0;
}

C++: #define _USE_MATH_DEFINES
#include <algorithm>
#include <cstdio>
#include <functional>
#include <iostream>
#include <cfloat>
#include <climits>
#include <cstring>
#include <cmath>
#include <map>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <time.h>
#include <vector>
using namespace std;

typedef long long ll;
typedef unsigned long long ull;
typedef pair<int, int> i_i;
typedef pair<ll, int> ll_i;
typedef pair<double, int> d_i;
typedef pair<ll, ll> ll_ll;
typedef pair<double, double> d_d;
struct edge { int u, v; ll w; };

ll MOD = 1000000007;
ll _MOD = 1000000009;
double EPS = 1e-10;

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

void dfs(int i, int j, vector< vector<int> >& a) {
	a[i][j] = false;
	for (int di = -1; di <= 1; di++)
		for (int dj = -1; dj <= 1; dj++)
			if (a[i + di][j + dj]) dfs(i + di, j + dj, a);
}

int main() {
	for (;;) {
		int w, h; cin >> w >> h;
		if (w == 0 && h == 0) break;
		vector< vector<int> > a(h + 2, vector<int>(w + 2));
		for (int i = 1; i <= h; i++)
			for (int j = 1; j <= w; j++)
				cin >> a[i][j];
		int cnt = 0;
		for (int i = 1; i <= h; i++)
			for (int j = 1; j <= w; j++)
				if (a[i][j]) {
					cnt++;
					dfs(i, j, a);
				}
		cout << cnt << endl;
	}
}

C++: #include<iostream>
using namespace std;
void dfs(int,int);
int box[50][50];
int main(){
  int w,h,i,I,a;
  while(1){
    a=0;
    cin>>w>>h;
    if(w==0&&h==0) break;
    for(i=0;i<=h+1;i++){
      for(I=0;I<=w+1;I++){
	box[i][I]==0;
      }
    }
    for(i=1;i<=h;i++){
      for(I=1;I<=w;I++){
	cin>>box[i][I];
      }
    }
    for(i=1;i<=h;i++){
      for(I=1;I<=w;I++){
	if(box[i][I]==1){
	  dfs(i,I);
	  a++;
	}
      }
    }
    cout<<a<<endl;
  }
  return 0;
}
  void dfs(int i,int I){
  if(box[i][I]==0) return;
  box[i][I]=0;
  dfs(i,I+1);
  dfs(i+1,I+1);
  dfs(i+1,I);
  dfs(i+1,I-1);
  dfs(i,I-1);
  dfs(i-1,I-1);
  dfs(i-1,I);
  dfs(i-1,I+1);
  }