Go: package main

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

var sc = bufio.NewScanner(os.Stdin)
var route [][]int

func scanInt() int {
	num, _ := strconv.Atoi(scanString())
	return num
}

func scanString() string {
	sc.Scan()
	return sc.Text()
}

func init() {
	sc.Split(bufio.ScanWords)
}

func dfs(h, w int) {
	if route[h][w] == 0 {
		return
	}

	route[h][w] = 0
	for _, x := range []int{-1, 0, 1} {
		for _, y := range []int{-1, 0, 1} {
			if route[h+x][w+y] == 0 {
				continue
			}
			dfs(h+x, w+y)
		}
	}
}

func main() {
	writer := bufio.NewWriter(os.Stdout)
	for {
		w, h := scanInt(), scanInt()
		if w == 0 && h == 0 {
			break
		}

		route = make([][]int, h+2)
		route[0] = make([]int, w+2)
		for i := 1; i <= h; i++ {
			route[i] = make([]int, w+2)
			for j := 1; j <= w; j++ {
				route[i][j] = scanInt()
			}
		}
		route[h+1] = make([]int, w+2)

		total := 0
		for i := 1; i <= h; i++ {
			for j := 1; j <= w; j++ {
				if route[i][j] == 1 {
					total++
					dfs(i, j)
				}
			}
		}
		fmt.Fprintln(writer, total)
	}
	writer.Flush()
}

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

int w, h, ans = 0, map[N][N];

void search_zahyou(int w, int h);

int main(){
	while(cin >>w >>h){
		ans = 0;
		if(w == 0 && h == 0) break;
		for(int i = 0; i < h; i++){
			for(int k = 0; k < w; k++){
				cin >>map[i][k];
			}
		}
		for(int i = 0; i < h; i++){
			for(int k = 0; k < w; k++){
				if(map[i][k] == 0){
					continue;
				}
				else{
					ans++;
					search_zahyou(i, k);
				}
			}
		}
		cout <<ans <<endl;
	}
	return 0;
}

void search_zahyou(int inh, int inw){
	int searchh[8] = {-1, -1, -1, 0, 1, 1, 1, 0}, searchw[8] = {-1, 0, 1, 1, 1, 0, -1, -1};
	if(h <= inh || w <= inw || inh < 0 || inw < 0){
	} else{
		map[inh][inw] = 0;
		for(int i = 0; i < 8; i++){
			if(map[inh + searchh[i]][inw + searchw[i]] == 1){
				search_zahyou(inh + searchh[i], inw + searchw[i]);
			}
		}
	}
}

C++: #include <iostream>

using namespace std;

int w,h;
int dx[8] = { -1 , -1 , -1 ,  0 , 0 ,  1 , 1 , 1};
int dy[8] = { -1 ,  0 ,  1 , -1 , 1 , -1 , 0 , 1};
int map[51][51];

void check(int x , int y){
	int mx,my;

	for(int i=0 ; i<8 ; i++){//üÍWûüð²×é
		mx = x + dx[i];
		my = y + dy[i];

		//tB[hOÉoÄ¢È¯êÎ²×é(0ÈãE³Èà)
		if(mx>=0 && mx<w && my>=0 && my<h){
			if(map[my][mx]==1){//¾Á½ç
				map[my][mx] = 2;//ñdÉ²×È¢½ßÉÊÌlðüêé
				check( mx , my );
			}
		}
	}
}

int main(){
	int count;//ÌðJEg·éÏ

	while(1){
		//ÏÌú»
		count = 0;
		
		//E³ÌüÍ
		cin >> w >> h;
		if(w==0 && h==0) break;//0 0ªüÍ³ê½çI¹

		//tB[hª©C©ÌîñðüÍ
		for(int y=0 ; y<h ; y++){
			for(int x=0 ; x<w ; x++){
				cin >> map[y][x];
			}
		}

		for(int y=0 ; y<h ; y++){
			for(int x=0 ; x<w ; x++){
				if(map[y][x]==1){//¾Á½çÄAÖðÄÑo·
					map[y][x] = 2;
					count++;
					check( x , y );
				}
			}
		}

		//¦ÌoÍ
		cout << count << endl;
	}

	return 0;
}

C++: #include <iostream>
#include <queue>
#include <cstdio>
#include <cstring>
#define rep(i,n) for(int i=0;i<(n);i++)

using namespace std;

typedef pair<int, int> P;
const int dir[][2] = { {-1, 0 }, { -1, 1 }, { 0, 1 }, { 1, 1 }, { 1, 0 }, { 1, -1 }, {0, -1 }, {-1, -1 } };
int w, h;
int Map[52][52];
bool used[52][52];

void bfs (int row, int col )
{
	queue<P> que;
	que.push (P (row, col ) );
	while (!que.empty() ){
		P curr = que.front(); que.pop();
		int cr = curr.first, cc = curr.second;
		used[cr][cc] = true;
		rep (k, 8 ){
			int nr = cr + dir[k][0];
			int nc = cc + dir[k][1];
			if (nr < 0 || nc < 0 || nr >= h || nc >= w || Map[nr][nc] == 0 || used[nr][nc] ) continue;
			que.push (P (nr, nc ) );
		} // end rep
	} // end while
}

int main()
{
	while (scanf ("%d %d", &w, &h ) && w && h ){
		memset (used, false, sizeof (used ) );
		rep (i, h ){
			rep (j, w ){
				scanf (" %d", Map[i]+j );
			} // end rep
		} // end rep
		int cnt = 0;
		rep (i, h ){
			rep (j, w ){
				if (Map[i][j] == 1 && !used[i][j] ){
					bfs (i, j );
					cnt++;
				} // end if
			} // end rep
		} // end rep
		printf ("%d\n", cnt );
	} // end loop
		
	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;
		if (a == b == 0)break;
		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<<endl;
	}
	return 0;
}