Go: package main

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

func getScanner(fp *os.File) *bufio.Scanner {
	scanner := bufio.NewScanner(fp)
	scanner.Split(bufio.ScanWords)
	scanner.Buffer(make([]byte, 1000005), 1000005)
	return scanner
}

func getNextString(scanner *bufio.Scanner) string {
	scanner.Scan()
	return scanner.Text()
}

func getNextInt(scanner *bufio.Scanner) int {
	i, _ := strconv.Atoi(getNextString(scanner))
	return i
}

func getNextInt64(scanner *bufio.Scanner) int64 {
	i, _ := strconv.ParseInt(getNextString(scanner), 10, 64)
	return i
}

func getNextUint64(scanner *bufio.Scanner) uint64 {
	i, _ := strconv.ParseUint(getNextString(scanner), 10, 64)
	return i
}

func getNextFloat64(scanner *bufio.Scanner) float64 {
	i, _ := strconv.ParseFloat(getNextString(scanner), 64)
	return i
}

func main() {
	fp := os.Stdin
	wfp := os.Stdout
	cnt := 0

	if os.Getenv("MASPY") == "ますピ" {
		fp, _ = os.Open(os.Getenv("BEET_THE_HARMONY_OF_PERFECT"))
		cnt = 0
	}
	if os.Getenv("MASPYPY") == "ますピッピ" {
		wfp, _ = os.Create(os.Getenv("NGTKANA_IS_GENIUS10"))
	}

	scanner := getScanner(fp)
	writer := bufio.NewWriter(wfp)
	for solve(scanner, writer) {

	}
	for i := 0; i < cnt; i++ {
		fmt.Fprintln(writer, "-----------------------------------")
		solve(scanner, writer)
	}
	writer.Flush()
}

func solve(scanner *bufio.Scanner, writer *bufio.Writer) bool {
	sol := solver{}
	sol.w = getNextInt(scanner)
	sol.h = getNextInt(scanner)
	if sol.w == 0 && sol.h == 0 {
		return false
	}
	sol.land = makeInts(sol.h, sol.w)
	for y := 0; y < sol.h; y++ {
		for x := 0; x < sol.w; x++ {
			sol.land[y][x] = getNextInt(scanner)
		}
	}
	ans := 0
	for y := 0; y < sol.h; y++ {
		for x := 0; x < sol.w; x++ {
			if sol.land[y][x] == 0 {
				continue
			}
			ans++
			sol.dfs(y, x)
		}
	}
	fmt.Fprintln(writer, ans)
	return true
}

type solver struct {
	w, h int
	land [][]int
}

func (sol *solver) dfs(y, x int) {
	if y < 0 {
		return
	}
	if y >= sol.h {
		return
	}
	if x < 0 {
		return
	}
	if x >= sol.w {
		return
	}
	if sol.land[y][x] == 0 {
		return
	}
	sol.land[y][x] = 0
	dy := [8]int{-1, -1, -1, 0, 0, 1, 1, 1}
	dx := [8]int{-1, 0, 1, -1, 1, -1, 0, 1}
	for i := 0; i < 8; i++ {
		sol.dfs(y+dy[i], x+dx[i])
	}
}
func makeInts(h, w int) [][]int {
	index := make([][]int, h, h)
	data := make([]int, h*w, h*w)
	for i := 0; i < h; i++ {
		index[i] = data[i*w : (i+1)*w]
	}
	return index
}

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;
}