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 <stdio.h>
#include <stack>

#define MAX_W 50
#define MAX_H 50
#define LAND 1
#define SEA 0
int map[MAX_H][MAX_W];
int h, w;
using namespace std;
stack<int> x, y;

bool input() {
  int i, j;

  scanf("%d %d", &w, &h);
  if (w == 0 && h == 0) return false;
  for (i = 0; i < h; i++) {
    for (j = 0; j < w; j++) {
      scanf("%d", &map[i][j]);
    }
  }

  return true;
}

int main() {
  int i, j, cnt;

  while (input()) {
    cnt = 0;
    for (i = 0; i < h; i++) {
      for (j = 0; j < w; j++) {
        if (map[i][j] == LAND) {
          cnt++;
          x.push(j);
          y.push(i);
          while (!x.empty() && !y.empty()) {
            int a = x.top();
            x.pop();
            int b = y.top();
            y.pop();
            if (a < 0 || w <= a || b < 0 || h <= b || map[b][a] == SEA)
              continue;
            map[b][a] = SEA;
            x.push(a - 1);
            y.push(b - 1);
            x.push(a - 1);
            y.push(b);
            x.push(a - 1);
            y.push(b + 1);
            x.push(a);
            y.push(b - 1);
            x.push(a);
            y.push(b + 1);
            x.push(a + 1);
            y.push(b - 1);
            x.push(a + 1);
            y.push(b);
            x.push(a + 1);
            y.push(b + 1);
          }
        }
      }
    }
    printf("%d\n", cnt);
  }

  return 0;
}

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

void input(int **, int, int);
int howManyIslands(int **, int, int);
void howBigIslands(int **, bool **, int, int, int, int);

int main()
{
	int **island, w, h;
	while(true){
		cin >> w >> h;
		if(w==h && h==0) break;
		island = new int*[w];
		for(int i=0; i<w; i++)
			island[i] = new int[h];
		input(island, w, h);
		cout << howManyIslands(island, w, h) << endl;
		for(int i=0; i<w; i++)
			delete[] island[i];
		delete[] island;
	}
	return 0;
}

void input(int **island, int w, int h)
{
	char buf[60];
	cin.clear();cin.ignore();
	for(int i=0; i<h; i++){
		cin.getline(buf, sizeof(buf));
		int j=0, k=0;
		while(buf[j] != '\0'){
			if(buf[j] != ' '){
				switch(buf[j]){
				case '0':
					island[k][i] = 0;
					k++;
					break;
				case '1':
					island[k][i] = 1;
					k++;
					break;
				}
				if(w==k) break;
			}
			j++;
		}
	}
}

int howManyIslands(int **island, int w, int h)
{
	bool **check;
	int ans = 0;
	check = new bool*[w];
	for(int i=0; i<w; i++){
		check[i] = new bool[h];
		for(int j=0; j<h; j++)
			check[i][j] = false;
	}
	for(int i=0; i<w; i++){
		for(int j=0; j<h; j++){
			if(check[i][j])continue;
			if(island[i][j]){
				ans++;
				howBigIslands(island, check, i, j, w, h);
			} else {
				check[i][j] = true;
			}
		}
	}
	for(int i=0; i<w; i++)
		delete[] check[i];
	delete[] check;

	return ans;
}

void howBigIslands(int **island, bool **check, int x, int y, int w, int h)
{
	if(check[x][y]) return;
	enum __DIR{ UPPER_LEFT, UP, UPPER_RIGHT, LEFT, RIGHT, LOWER_LEFT,
		UNDER, LOWER_RIGHT, DIR_END};
	int periphe[DIR_END][2]={
			{-1, -1}, {0, -1}, {1, -1},
			{-1, 0}, {1, 0},
			{-1, 1}, {0, 1}, {1, 1}
	};
	check[x][y] = true;
	for(int i=0; i<DIR_END; i++){
		if(x+periphe[i][0] < 0 || w-1 < x+periphe[i][0] ||
			y+periphe[i][1] < 0 || h-1 < y+periphe[i][1]) continue;
		if(island[x+periphe[i][0]][y+periphe[i][1]]){
			if(!check[x+periphe[i][0]][y+periphe[i][1]])
				howBigIslands(island, check,
				x+periphe[i][0], y+periphe[i][1], w, h);
		}
	}
}

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

int main(void){
	int w, h;
	int dx[] = {1,1,0,-1,-1,-1,0,1};
	int dy[] = {0,1,1,1,0,-1,-1,-1};
	while(true){
		cin >> w >> h;
		if(!w&&!h)break;
		vector<int> c[h+2];
		c[0].resize(w+2);
		c[h+1].resize(w+2);
		for(int i = 1; i <= h; ++i){
			c[i].resize(w+2);
			for(int j = 1; j <= w; ++j){
				cin >> c[i][j];
			}
		}
		int ans = 0;
		queue<pair<int, int>> q;
		for(int i = 1; i <= h; ++i){
			for(int j = 1; j <= w; ++j){
				if(c[i][j] == 1){
					q.emplace(i,j);
					ans++;
					c[i][j] = 0;
					while(!q.empty()){
						int x = q.front().first;
						int y = q.front().second;
						q.pop();
						for(int dir = 0; dir < 8; ++dir){
							int nx = x + dx[dir];
							int ny = y + dy[dir];
							if(c[nx][ny]==1){
								q.emplace(nx,ny);
								c[nx][ny]=0;
							}
						}
					}
				}
			}
		}
		cout << ans << endl;
	}
	return 0;
}

C++: #include<bits/stdc++.h>
#include<algorithm>
#include<string>
#include<iostream>
using namespace std;
#define int long long
#define double long double
#define fo(a,b) for(int a=0;a<b;a++)
#define Sort(a) sort(a.begin(),a.end())
#define rev(a) reverse(a.begin(),a.end())
#define fi first
#define se second
#define th third
#define sz size()
#define bgn begin()
#define en end()
#define pb push_back
#define pp() pop_back()
#define V vector
#define P pair
#define yuko(a) setprecision(a)
#define uni(a) a.erase(unique(a.begin(),a.end()),a.end())
#define Q queue
#define pri priority_queue
#define Pri priority_queue<int,vector<int>,greater<int>>
#define PriP priority_queue<P<int,int>,vector<P<int,int>>,greater<P<int,int>>>
#define ff first.first
#define fs first.second
#define sf second.first
#define ss second.second
#define all(a)  (a).begin(),(a).end()
#define elif else if

const int dx[8]={1,1,1,0,0,-1,-1,-1};
const int dy[8]={-1,0,1,-1,1,-1,0,1};
int H,W;
V<V<int>> field;

void dfs(int h,int w){
  field[h][w]=0;
  fo(dir,8){
    int nh=h+dx[dir];
    int nw=w+dy[dir];
    if(nh<0||nw<0||nh>=H||nw>=W)
      continue;
    if(field[nh][nw]==0)
      continue;
    dfs(nh,nw);
  }
}
signed main(){
  while(cin>>W>>H){
    if(H==0||W==0)
      break;
    field.assign(H,V<int> (W,0));
    fo(h,H){
      fo(w,W){
        cin>>field[h][w];
      }
    }
    int ans=0;
    fo(h,H){
      fo(w,W){
        if(field[h][w]==0)
          continue;
        dfs(h,w);
        ++ans;
      }
    }
    cout<<ans<<endl;
  }
}