Go: package main

import (
	"bufio"
	"fmt"
	"os"
	"strconv"
)

var sc = bufio.NewScanner(os.Stdin)
var route [][]int

func scanInt() int {
	num, _ := strconv.Atoi(scanString())
	return num
}

func scanString() string {
	sc.Scan()
	return sc.Text()
}

func init() {
	sc.Split(bufio.ScanWords)
}

func dfs(h, w int) {
	if route[h][w] == 0 {
		return
	}

	route[h][w] = 0
	for _, x := range []int{-1, 0, 1} {
		for _, y := range []int{-1, 0, 1} {
			if route[h+x][w+y] == 0 {
				continue
			}
			dfs(h+x, w+y)
		}
	}
}

func main() {
	writer := bufio.NewWriter(os.Stdout)
	for {
		w, h := scanInt(), scanInt()
		if w == 0 && h == 0 {
			break
		}

		route = make([][]int, h+2)
		route[0] = make([]int, w+2)
		for i := 1; i <= h; i++ {
			route[i] = make([]int, w+2)
			for j := 1; j <= w; j++ {
				route[i][j] = scanInt()
			}
		}
		route[h+1] = make([]int, w+2)

		total := 0
		for i := 1; i <= h; i++ {
			for j := 1; j <= w; j++ {
				if route[i][j] == 1 {
					total++
					dfs(i, j)
				}
			}
		}
		fmt.Fprintln(writer, total)
	}
	writer.Flush()
}

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


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

void dfs(int h, int w){
	field[h][w] = 0;
	for(int dh=-1;dh<=1;dh++){
		for(int dw=-1;dw<=1;dw++){
			int nh=h+dh, nw=w+dw;
			if(nh<0||nw<0||H-1<nh||W-1<nw) 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 cnt = 0;
		for(int i=0;i<H;i++){
			for(int j=0;j<W;j++){
				if(field[i][j] == 0) continue;
				dfs(i, j);
				cnt++;
			}
		}
		cout<<cnt<<endl;
	}
}

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

int c[MAX][MAX];

void dfs(int w, int h, int x, int y);
int main(int argc, char const *argv[]){
    int w, h, ans;

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

    return 0;
}

void dfs(int w, int h, int x, int y){
    int dx[8] = {1, 1, 0, -1, -1, -1, 0, 1};
    int dy[8] = {0, 1, 1, 1, 0, -1, -1, -1};
    c[x][y] = 0;

    for (int i = 0; i < 8; i++){
        int nx = x + dx[i];
        int ny = y + dy[i];

        if (0 <= nx && nx < w && 0 <= ny && ny < h && c[nx][ny] == 1){
            dfs(w, h, nx, ny);
        }
    }
    return;
}

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

#define REP(i,m,n) for(int i=(int)m; i < (int) n; ++i)
#define rep(i,n) REP(i,0,n)

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

void dfs(int h, int w) {
    field[h][w] = 0;
    for(int i = -1; i <= 1; i++) {
        for(int j = -1; j <= 1; j++) {
            int nh = h+i;
            int nw = w+j;
            
            if(nh < 0 || nw < 0 || nh >= H || nw >= W) continue;
            if(field[nh][nw] == 0) continue;
            dfs(nh, nw);
        }
    }
}

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

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

        cout << ans << '\n';
    }
    return 0;
}

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

constexpr int LEN = 55;
bool m[LEN][LEN];

int main(){
	while(true){
		int w, h; cin >> w >> h;
		if(!w && !h) break;
		rep(i, h)rep(j, w) cin >> m[i][j];

		int ans = 0;
		rep(i, h)rep(j, w){
			if(!m[i][j]) continue;
			ans++;
			queue<P> q;
			q.push(make_pair(i, j));
			while(q.size()){
				P now = q.front(); q.pop();
				loop(di, -1, 2)loop(dj, -1, 2){
					int ni = now.first + di, nj = now.second + dj;
					if(0 <= ni && ni < h && 0 <= nj && nj < w && m[ni][nj]){
						m[ni][nj] = false;
						q.push(make_pair(ni, nj));
					}
				}
			}
		}
		cout << ans << endl;
	}
	return 0;
}

C++: #include <stdio.h>

void solve(int a[50][50],int x,int y){
	int i, j;
	a[y][x] = 0;
	for(i = y - 1;i <= y + 1;i++){
		for(j = x - 1;j <= x + 1;j++)
			if(a[i][j]) solve(a,j,i);
	}
	return;
}

int main(void){
	int w, h, i, j, flag[50][50], count;
	scanf("%d%d",&w,&h);
	while(w != 0 || h != 0){
		for(i = 0;i < h;i++)
			for(j = 0;j < w;j++) scanf("%d",&flag[i][j]);
		count = 0;
		for(i = 0;i < h;i++){
			for(j = 0;j < w;j++){
				if(flag[i][j]){
					count++;
					solve(flag,j,i);
				}
			}
		}
		printf("%d\n",count);
		scanf("%d%d",&w,&h);
	}
	return 0;
}

C++: #include <iostream>
#define MAX_W 50
#define MAX_H 50
using namespace std;

int w, h;
int t[MAX_H][MAX_W];

void PRINT() {
  for(int i = 0; i < h; ++i) {
    for(int j = 0; j < w; ++j) {
      cout << t[i][j] << ' ';
    }
    cout << endl;
  }
  cout << endl;
}

// 深さ優先探索
// 現在位置(y, x)
void dfs(int y, int x) {
  t[y][x] = 8;

  for(int dx = -1; dx <= 1; dx++) {
    for(int dy = -1; dy <= 1; dy++) {
      // 現在位置を(nx, ny)に更新
      int nx = x + dx;
      int ny = y + dy;

      // (nx, ny)は部屋の中か？ & t[nx][ny]は陸か？ を判定
      if(0 <= nx && nx < w && 0 <= ny && ny < h && t[ny][nx] == 1) {
        dfs(ny, nx);
        //PRINT();
      }
    }
  }
  return;
}

void solve() {
  int cnt = 0;
  for(int y = 0; y < h; ++y) {
    for(int x = 0; x < w; ++x) {

      if(t[y][x] == 1) {
        dfs(y, x);
        cnt++;
      }
      
    }
  }
  cout << cnt << endl;
}

int main() {
  while(cin >> w >> h && w && h) {

    for(int i = 0; i < h; ++i) {
      for(int j = 0; j < w; ++j) {
        cin >> t[i][j];
      }
    }
    solve();
  }
  return 0;
}