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>
using namespace std;
#include<vector>

int wm,hm;
vector< vector<int> > wh(51, vector<int>(51,0));

int xp[]={-1,0,1,-1,1,-1,0,1};
int yp[]={-1,-1,-1,0,0,1,1,1};

void dfs(int x,int y){
  for(int i=0;i<8;i++){
    int nx=x+xp[i];
    int ny=y+yp[i];
    if(nx>-1&&nx<wm&&ny>-1&&ny<hm&&wh[nx][ny]==1){
      //cout<<nx<<" "<<ny<<" "<<wh[nx][ny]<<endl;
      wh[nx][ny]=0;
      dfs(nx,ny);
    }
  }
}

int main(){
  while(1){
    //int wm,hm;
    int aa,bb;
    cin>>aa>>bb;
    wm=bb;
    hm=aa;
    if(wm==0&&hm==0)break;
    //vector< vector<int> > wh(wm, vector<int>(hm));
    for(int i=0;i<wm;i++){
      for(int j=0;j<hm;j++){
        int a;
        cin>>a;
        wh[i][j]=a;
      }
    }
    int ans=0;
    for(int i=0;i<hm;i++){
      for(int j=0;j<wm;j++){
        if(wh[i][j]==1){
          //cout<<"*"<<i<<" "<<j<<endl;
          wh[i][j]=0;
          dfs(i,j);
          ans++;
        }
      }
    }
    cout<<ans<<endl;
  }
  return 0;
}

C++: #include<iostream>
using namespace std;
int box[51][51];
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){
	  kazu(i,I);
	  a++;
	}
      }
    }
    cout<<a<<endl;
  }
  return 0;
}
  void kazu(int i,int I){
    if(box[i][I+1]==1) {
      box[i][I+1]=0;
      kazu(i,I+1);
    }
    if(box[i][I-1]==1) {
      box[i][I-1]=0;
      kazu(i,I-1);
    }
    if(box[i+1][I]==1) {
      box[i+1][I]=0;
      kazu(i+1,I);
    }
    if(box[i+1][I+1]==1) {
      box[i+1][I+1]=0;
      kazu(i+1,I+1);
    }
    if(box[i+1][I-1]==1) {
      box[i+1][I-1]=0;
      kazu(i+1,I-1);
    }
    if(box[i-1][I+1]==1) {
      box[i-1][I+1]=0;
      kazu(i-1,I+1);
    }
    if(box[i-1][I-1]==1) {
      box[i-1][I+1]=0;
      kazu(i-1,I+1);
    }
    if(box[i-1][I]==1) {
      box[i-1][I]=0;
      kazu(i-1,I);
    }

  }

C++: #include<iostream>
#include<cmath>
#include<algorithm>
#include<vector>

using namespace std;


vector<vector<bool>> seen;
vector<int> dx;
vector<int> dy;

void dfs(int u,int v,vector<vector<int>> &c){

    seen[u][v]=true;

    for(int i=0;i<8;i++){
        if(c[u+dx[i]][v+dy[i]]==0)continue;
        if(seen[u+dx[i]][v+dy[i]])continue;
        dfs(u+dx[i],v+dy[i],c);
    }

}

int main(){

    dx={-1,0,1,-1,1,-1,0,1};
    dy={1,1,1,0,0,-1,-1,-1};

    int w=1,h=1;
    cin >> w >> h;
    while(true){
        if(h==0 && w==0)break;
        else{
            vector<vector<int>> c(h+2);
            for(int i=0;i<h+2;i++)c[i].resize(w+2);

            for(int i=0;i<w+2;i++)c[0][i]=0;

            for(int i=1;i<h+1;i++){
                c[i][0]=0;
                for(int j=0;j<w;j++){
                    cin >> c[i][j+1];
                }   
                c[i][w+1]=0;
            }
            for(int i=0;i<w+2;i++)c[h+1][i]=0;
            seen.resize(h+2);
            for(int i=0;i<h+2;i++)seen[i].assign(w+2,false);            

            int e=0;

            for(int i=1;i<h+1;i++){
                for(int j=1;j<w+1;j++){
                    if(seen[i][j]==false && c[i][j]==1){
                        dfs(i,j,c);
                        e++;
                    }
                }
            }
            cout << e << endl;
        }
        cin >> w >> h;
    }
}

C++: #include <iostream>
#include <vector>
#include <stack>

using std::cin;
using std::cout;
using std::endl;

int main(void) {
	int w,h;
	cin >> w;
	cin >> h;

	while (w!=0||h!=0) {
		int count = 0;
		std::stack<int> st_x,st_y;
		int xn,yn;
		int c;
		bool all = false;
		std::vector< std::vector<bool> > island(h+2, std::vector<bool>(w+2));
		for (int i = 0; i < h+2; i++) {
			for (int j = 0; j < w+2; j++) {
				if (i==0||i==h+1||j==0||j==w+1) {
					island[i][j] = false;
				} else {
					cin >> c;
					if (c==1) {
						island[i][j] = true;
					} else {
						island[i][j] = false;
					}
				}
			}
		}

		while (!all) {
			all = true;
			for (int i = 1; i < h+1; i++) {
				for (int j = 1; j < w+1; j++) {
					if (island[i][j]) {
						xn = i;
						yn = j;
						all = false;
					}
				}
			}
			if (!all) {
				st_x.push(xn);
				st_y.push(yn);
				if (island[xn][yn]) {
					island[xn][yn] = false;
					count++;

					while (!st_x.empty()) {
						xn = st_x.top();
						yn = st_y.top();
						st_x.pop();
						st_y.pop();
						if (island[xn+1][yn]) {
							st_x.push(xn+1);
							st_y.push(yn);
							island[xn+1][yn] = false;
						}
						if (island[xn-1][yn]) {
							st_x.push(xn-1);
							st_y.push(yn);
							island[xn-1][yn] = false;
						}
						if (island[xn+1][yn+1]) {
							st_x.push(xn+1);
							st_y.push(yn+1);
							island[xn+1][yn+1] = false;
						}
						if (island[xn+1][yn-1]) {
							st_x.push(xn+1);
							st_y.push(yn-1);
							island[xn+1][yn-1] = false;
						}
						if (island[xn-1][yn+1]) {
							st_x.push(xn-1);
							st_y.push(yn+1);
							island[xn-1][yn+1] = false;
						}
						if (island[xn-1][yn-1]) {
							st_x.push(xn-1);
							st_y.push(yn-1);
							island[xn-1][yn-1] = false;
						}
						if (island[xn][yn+1]) {
							st_x.push(xn);
							st_y.push(yn+1);
							island[xn][yn+1] = false;
						}
						if (island[xn][yn-1]) {
							st_x.push(xn);
							st_y.push(yn-1);
							island[xn][yn-1] = false;
						}
					}
				}
			}
		}
		cout << count << endl;

		cin >> w;
		cin >> h;
	}	
		return 0;
}