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;
void kazoeruyo(int p,int P,int box[50][50]){
  box[p][P]=0;
    if(box[p][P+1]==1) {
      box[p][P+1]=0;
      P=P+1; 
      kazoeruyo(p,P,box);
    }
    if(box[p][P-1]==1){
      box[p][P-1]=0;
      P=P-1;
      kazoeruyo(p,P,box);
    }
    if(box[p+1][P]==1){ 
      box[p+1][P]=0;
      p=p+1;
      kazoeruyo(p,P,box);
    }
    if(box[p-1][P]==1){
      box[p-1][P]=0;
      p=p-1;
      kazoeruyo(p,P,box);
    }
    if(box[p-1][P-1]==1){
      box[p-1][P-1]=0;
      p=p-1;
      P=P-1;
      kazoeruyo(p,P,box);
    }
    if(box[p-1][P+1]==1){
      box[p-1][P+1]=0;
      p=p-1;
      P=P+1;
      kazoeruyo(p,P,box);
    }
    if(box[p+1][P+1]==1){
      box[p+1][P+1]=0;
      p=p+1;
      P=P+1;
      kazoeruyo(p,P,box);
    }
    if(box[p+1][P-1]==1){
      box[p+1][P-1]=0;
      p=p+1;
      P=P-1;
      kazoeruyo(p,P,box);
    }	
}
int main(){
  int w,h,i,I,p,P,o,t,T,y,Y;
  int box[50][50];
  while(1){
    cin>>w>>h;
    if(w==0&&h==0) break;
    for(i=0;i<h;i++){
      for(I=0;I<w;I++){
	cin>>box[i][I];
      }
    }
    o=0;
    for(i=0;i<h;i++){
      for(I=0;I<w;I++){
	if(box[i][I]==1) {
	  p=i;
	  P=I;
	  kazoeruyo(p,P,box);
	  box[p][P]=1;
	}
      }
    }
    for(i=0;i<h;i++){
      for(I=0;I<w;I++){
	if(box[i][I]==1) o++;
      }
    }
    cout<<o<<endl;
    o=0;
  }
  return 0;
}

C++: #include<iostream>
//#include<>

using namespace std;

struct Node{

	Node();

	int aLandType;

};

Node::Node(){
	aLandType = 0;
}

bool check(const int x, const int y, const int LandNum, const int aNodeCol[50][50], const int aXMax, const int aYMax){
	if ((x<0 || x >= aXMax) || (y<0 || y >= aYMax) || (aNodeCol[x][y] != 1)){
		return false;
	}
	return true;
}
bool LandConfirmedAndMawariCheck(const int x, const int y, const int LandNum, int aNodeCol[50][50], const int aXMax, const int aYMax){
	if (aNodeCol[x][y] == 1){
		aNodeCol[x][y] = LandNum;
		/*if (check(x - 1, y, LandNum, aNodeCol, aXMax, aYMax))LandConfirmedAndMawariCheck(x - 1, y, LandNum, aNodeCol, aXMax, aYMax);
		if (check(x - 1, y - 1, LandNum, aNodeCol, aXMax, aYMax))LandConfirmedAndMawariCheck(x - 1, y - 1, LandNum, aNodeCol, aXMax, aYMax);
		if (check(x - 1, y + 1, LandNum, aNodeCol, aXMax, aYMax))LandConfirmedAndMawariCheck(x - 1, y + 1, LandNum, aNodeCol, aXMax, aYMax);
		if (check(x + 1, y - 1, LandNum, aNodeCol, aXMax, aYMax))LandConfirmedAndMawariCheck(x + 1, y - 1, LandNum, aNodeCol, aXMax, aYMax);
		if (check(x + 1, y, LandNum, aNodeCol, aXMax, aYMax))LandConfirmedAndMawariCheck(x + 1, y, LandNum, aNodeCol, aXMax, aYMax);
		if (check(x + 1, y + 1, LandNum, aNodeCol, aXMax, aYMax))LandConfirmedAndMawariCheck(x + 1, y + 1, LandNum, aNodeCol, aXMax, aYMax);
		if (check(x, y - 1, LandNum, aNodeCol, aXMax, aYMax))LandConfirmedAndMawariCheck(x, y - 1, LandNum, aNodeCol, aXMax, aYMax);
		if (check(x, y + 1, LandNum, aNodeCol, aXMax, aYMax))LandConfirmedAndMawariCheck(x, y + 1, LandNum, aNodeCol, aXMax, aYMax);*/
		if (!(x - 1<0 || x - 1 >= aXMax) || (y    <0 || y     >= aYMax) || (aNodeCol[x-1][y    ] != 1))LandConfirmedAndMawariCheck(x - 1, y    , LandNum, aNodeCol, aXMax, aYMax);
		if (!(x - 1<0 || x - 1 >= aXMax) || (y - 1<0 || y - 1 >= aYMax) || (aNodeCol[x-1][y - 1] != 1))LandConfirmedAndMawariCheck(x - 1, y - 1, LandNum, aNodeCol, aXMax, aYMax);
		if (!(x - 1<0 || x - 1 >= aXMax) || (y + 1<0 || y + 1 >= aYMax) || (aNodeCol[x-1][y + 1] != 1))LandConfirmedAndMawariCheck(x - 1, y + 1, LandNum, aNodeCol, aXMax, aYMax);
		if (!(x + 1<0 || x + 1 >= aXMax) || (y - 1<0 || y - 1 >= aYMax) || (aNodeCol[x+1][y - 1] != 1))LandConfirmedAndMawariCheck(x + 1, y - 1, LandNum, aNodeCol, aXMax, aYMax);
		if (!(x + 1<0 || x + 1 >= aXMax) || (y    <0 || y     >= aYMax) || (aNodeCol[x+1][y    ] != 1))LandConfirmedAndMawariCheck(x + 1, y    , LandNum, aNodeCol, aXMax, aYMax);
		if (!(x + 1<0 || x + 1 >= aXMax) || (y + 1<0 || y + 1 >= aYMax) || (aNodeCol[x+1][y + 1] != 1))LandConfirmedAndMawariCheck(x + 1, y + 1, LandNum, aNodeCol, aXMax, aYMax);
		if (!(x    <0 || x     >= aXMax) || (y - 1<0 || y - 1 >= aYMax) || (aNodeCol[x  ][y - 1] != 1))LandConfirmedAndMawariCheck(x    , y - 1, LandNum, aNodeCol, aXMax, aYMax);
		if (!(x    <0 || x     >= aXMax) || (y + 1<0 || y + 1 >= aYMax) || (aNodeCol[x  ][y + 1] != 1))LandConfirmedAndMawariCheck(x    , y + 1, LandNum, aNodeCol, aXMax, aYMax);
		return true;
	}
	return false;
}



int main(void){
	int NodeCol[50][50];
	int a, b, c;
	int XMax, YMax;
	while(1){
		cin >> a >> b;
		XMax = a; YMax = b;

		for (int Lone = 0;  Lone < XMax; ++Lone){
			for (int Ltwo = 0; Ltwo < YMax; ++Ltwo){
				cin >> c;
				NodeCol[Lone][Ltwo] = c;
			}
		}
		int t = 2;
		for (int Lone = 0;  Lone < XMax; ++Lone){
			for (int Ltwo = 0; Ltwo < YMax; ++Ltwo){
				if (NodeCol[Lone][Ltwo]==1)
				{
					if (LandConfirmedAndMawariCheck(Lone, Ltwo, t, NodeCol, XMax, YMax))t++;
				}
			}
		}
		cout << t-2;
	}
	return 0;
}