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 <bits/stdc++.h>
#define FOR(i,a,b) for(int i=(a);i<(b);i++)
#define REP(i,n) for(int i=0;i < (n);i++)
#define ALL(obj) (obj).begin(),(obj).end()
#define SCANF(i) int i;scanf("%d",&i)
using namespace std;
using P = pair<int,int>;
const long long INF = 1LL << 60;
const int IINF=100000000;
const int MOD = (int)1e9 + 7;
typedef long long ll;
//約数列挙
vector<long long> divisor(long long n) {
    vector<long long> ret;
    for (long long i = 1; i * i <= n; i++) {
        if (n % i == 0) {
            ret.push_back(i);
            if (i * i != n) ret.push_back(n / i);
        }
    }
    //sort(ret.begin(), ret.end()); // 昇順に並べる
    return ret;
}
vector<int> dx={1,0,-1,0};vector<int> dy={0,-1,0,1};
int h,w;
vector<vector<int>> grid;
vector<vector<bool>> seen;
int xx[8] = {0,-1,-1,-1,0,1,1,1};
int yy[8] = {1,1,0,-1,-1,-1,0,1};
void dfs(int x,int y){
    seen[x][y] = true;
    for(int i=0;i<8;i++){
        int nx = x+xx[i];int ny = y+yy[i];
        if(nx<0||nx>h-1||ny<0||ny>w-1)continue;
        if(!seen[nx][ny] && grid[nx][ny]==1){
            dfs(nx,ny);
        }
    }
}
signed main () {
    while(cin >> w >> h){
        if(h+w==0)break;
        int cnt = 0;
        grid.assign(h,vector<int>(w));
        seen.assign(h,vector<bool>(w,false));
        REP(i,h)REP(j,w)cin >> grid[i][j];
        REP(i,h)REP(j,w){
            if(grid[i][j]==1 && !seen[i][j]){
                dfs(i,j);
                cnt++;
            }
        }
        cout << cnt << endl;
    }
}

C++: // Time-stamp: <2012-06-26 20:15:18 naga>

#include<iostream>
#include<cstdio>
using namespace std;
int maps[52][52];
int w,h;
void init(){
  for(int i = 0; i < 52; i++){
    for(int j = 0; j < 52; j++){
      maps[i][j] = 0;
    }
  }
  return;
}

void search(int x, int y){
  maps[y][x] = 0;
  if(maps[y - 1][x] && y > 0){
    search(x, y -1);
  }
  if(maps[y - 1][x - 1] && x > 0 && y > 0){
    search(x - 1, y - 1);
  }
  if(maps[y][x - 1] && x > 0){
    search(x - 1, y);
  }
  if(maps[y + 1][x - 1] && y < h && x > 0){
    search(x - 1, y + 1);
  }
  if(maps[y + 1][x] && y < h){
    search(x, y + 1);
  }
  if(maps[y + 1][x + 1] && y < h && x < w){
    search(x + 1, y + 1);
  }
  if(maps[y][x + 1] && x < w){
    search(x + 1, y);
  }
  if(maps[y - 1][x + 1] && y > 0 && x < w){
    search(x + 1, y - 1);
  }
  return;
}


int main(){
  int count;
  while(true){
    cin >> w >> h;
    if( !w && !h){
      break;
    }
    count = 0;
    init();
    for(int i = 0; i < h; i++){
      for(int j = 0; j < w; j++){
	cin >> maps[i][j];
      }
    }
    
    for(int i = 0; i < h; i++){
      for(int j = 0; j < w; j++){
	if(maps[i][j]){
	  count++;
	  search(j,i);
	}
      }
    }

    printf("%d\n", count);
  }
  return 0;
}

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

bool search(vector<vector<bool>>& isvisit, vector<vector<int>> c, int i, int j){
    if(!(i >= 0 && i < isvisit.size() && j >= 0 && j < isvisit[i].size())){
        return false;
    }else if(isvisit[i][j]){
        return false;
    }else{
        isvisit[i][j] = true;
        if(c[i][j] == 0) return false;

        for(int ii = -1; ii <= 1; ii += 2){
            search(isvisit, c, i+ii, j);
            search(isvisit, c, i, j+ii);
            search(isvisit, c, i+ii, j+ii);
            search(isvisit, c, i+ii, j-ii);
        }
        return true;
    }
}

int main(){
    int w, h;
    while(true){
        cin >> w >> h;
        if(w == 0 && h == 0) break;
        vector<vector<int>> c(h, vector<int>(w));
        for(int i = 0; i < h; i++){
            for(int j = 0; j < w; j++) cin >> c[i][j];
        }
        int ans = 0;
        vector<vector<bool>> isvisit(h, vector<bool>(w, false));
        for(int i = 0; i < h; i++){
            for(int j = 0; j < w; j++){
                if(search(isvisit, c, i, j)) ans++;
            }
        }
        cout << ans << endl;
    }
    
    return 0;
}

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

int main() {
	int n;
	while (cin >> n, n) {

		vector<vector<int>>list(n, vector<int>(n, -1));
		int number;

		for (int i = 0;i < n;i++) {
			cin >> number;
			for (int j = 0;j < number;j++) {
				int codename;
				cin >> codename;
				list[i][codename - 1] = 1;
			}
		}//list[a][b]:a know b

		cin >> number;
		
		vector<int>ans(n,1);
		int leaked;
		for (int i = 0;i < number;i++) {
			cin >> leaked;
			for (int j = 0;j < n;j++) {
				if (list[j][leaked - 1] <= 0) ans[j] = 0;
			}
		}//who is imposible
		int sum = 0,telltale;

		for (int i = 0;i < n;i++) {
			sum += ans[i];
			if (ans[i] == 1) telltale = i + 1;
		}

		if (sum == 1)cout << telltale << endl;
		else cout << -1 << endl;

	}
	return 0;
}

C++: #include <iostream>

using namespace std;
int c[51][51];
int w ,h;

bool input(){
	cin >> h >> w;
	if(w==0) return false;
	for(int i = 0;i <51;i++){
		for(int j = 0;j <51 ;j++){
			c[i][j] = 2;
		}
	}

	for(int i = 0;i < w ;i++){
		for(int j = 0; j < h ;j++){
			cin >> c[i][j];
//			cout <<c[i][j];
		}
	}
	return true;
}

void dfs(int x,int y){
	c[x][y] = 0;
	for(int i = -1; i <=1 ;i++){
		for(int j = -1; j <=1 ;j++){
			int nx = x + i;
			int ny = y + j;
//			cout <<nx << ' ' << ny << endl;
			if(0 <= nx && nx < w && 0 <= ny && ny < h && c[nx][ny] == 1)dfs(nx,ny);
		}
	}
}

void solve(){
	int res = 0;
/*	for(int i=0;i < w ;i++){
		for (int j=0;j<h;j++){
			cout <<c[i][j];
		}
		cout <<endl;
	}
*/
	for(int i=0; i < h;i++){
		for(int  j=0;j < w;j++){
			if(c[i][j] ==1){
				res++;
				dfs(i,j);
			}
		}
	}
	cout << res << endl;
}

int main(){
	while(input())solve();
}