Go: ///
// File:  1160.go
// Author: ymiyamoto
//
// Created on Sun Mar  4 20:45:57 2018
//
package main

import (
	"fmt"
)

var w, h int
var graph [][]string

func dfs(visited [][]bool, x, y int) {
	visited[x][y] = true

	for i := -1; i <= 1; i++ {
		for j := -1; j <= 1; j++ {
			dx := x + i
			dy := y + j
			if 0 <= dx && dx < h && 0 <= dy && dy < w && graph[dx][dy] == "1" && !visited[dx][dy] {
				dfs(visited, dx, dy)
			}
		}
	}
}

func main() {
	for {
		fmt.Scan(&w, &h)
		if w == 0 && h == 0 {
			break
		}

		graph = make([][]string, h)
		for i := range graph {
			graph[i] = make([]string, w)
		}

		for i := 0; i < h; i++ {
			for j := 0; j < w; j++ {
				var s string
				fmt.Scan(&s)
				graph[i][j] = s
			}
		}

		visited := make([][]bool, h)
		for i := range visited {
			visited[i] = make([]bool, w)
		}

		count := 0
		for i := range graph {
			for j := range graph[i] {
				if graph[i][j] == "1" && !visited[i][j] {
					dfs(visited, i, j)
					count++
				}
			}
		}

		fmt.Println(count)
	}
}

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

/*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 == 0&&b == 0)break;
		XMax = a; YMax = b;

		for (int Lone = 0;  Lone < YMax; ++Lone){
			for (int Ltwo = 0; Ltwo < XMax; ++Ltwo){
				cin >> c;
				NodeCol[Ltwo][Lone] = 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;
}

C++: #include <bits/stdc++.h>

using namespace std;

int w, h;
vector< vector<int> > c, island;

int dx[] = {1, -1, 0, 0, 1, -1, 1, -1};
int dy[] = {0, 0, 1, -1, 1, -1, -1, 1};

void dfs(int x, int y, int id) {

    island[y][x] = id;

    for(int i = 0; i < 8; i++) {

        int nx = x + dx[i];
        int ny = y + dy[i];

        if(nx < 0 || nx >= w || ny < 0 || ny >= h)
            continue;

        if(c[ny][nx] == 1 && island[ny][nx] != id)
            dfs(nx, ny, id);

    }

}

int main(){

    cin.tie(0);
    ios::sync_with_stdio(false);

    int ans;

    while(cin >> w >> h, w) {

        c.assign(h, vector<int>(w));
        island.assign(h, vector<int>(w, -1));
        ans = 0;

        for(int y = 0; y < h; y++)
            for(int x = 0; x < w; x++)
                cin >> c[y][x];

        for(int x = 0; x < w; x++)
            for(int y = 0; y < h; y++)
                if(c[y][x] == 1 && island[y][x] == -1)
                    dfs(x, y, ans++);

        cout << ans << endl;

    }

    return 0;

}

C++: #include<iostream>
#include<vector>
#include<algorithm>
#include<iomanip>
#include<stack>
#include<queue>
#include<string>
#include<set>
#include<map>
using namespace std;
#define rep(i,n) for(int i=0;(i)<(n);(i)++)
#define FOR(i,m,n) for(int i=(m);(i)<(n);(i)++)
typedef long long ll;
const int dx[8] = {0,1,1,1,0,-1,-1,-1};
const int dy[8] = {1,1,0,-1,-1,-1,0,1};
int w,h,res;

void dfs(int x,int y,int f,vector<vector<int> >& mp){
    if(mp[y][x]==1){
        if(f==0){
            f=1;
            res++;
        }
        mp[y][x]=0;
        rep(i,8){
            int nx=x+dx[i],ny=y+dy[i];
            if(nx>=0&&nx<w&&ny>=0&&ny<h){
                dfs(nx,ny,f,mp);
            }
        }
    }

}

int main(){
    while(cin >> w >> h,w|h){
        vector<vector<int> > c(h,vector<int>(w));
        
        rep(i,h)rep(j,w)cin >> c[i][j];
        res=0;
        rep(i,h)rep(j,w)dfs(j,i,0,c);
        cout << res << endl;
    }   
    return 0;
}

C++: #include<stdio.h>
void printmap();
void draw(int x,int y);
int w,h,c[50][50];

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


	scanf("%d %d",&w,&h);


	while((w!=0)&&(h!=0)){
		count=0;
	
		for(j=0;j<h;j++){
			for(i=0;i<w;i++){
				scanf("%d",&c[j][i]);
			}
		}
		//printf("\n");
		
		for(i=0;i<w;i++){
			c[h][i]=0;
		}
		for(i=0;i<h;i++){
			c[i][w]=0;
		}

		for(i=0;i<w;i++){
			for(j=0;j<h;j++){
				if(c[j][i]==1){
					count++;
					//printf("count up count=%d\n",count);
					draw(j,i);
					//printmap();
				}
			}
		}

		printf("%d\n",count);
		scanf("%d %d",&w,&h);
	}
	return 0;
}

void draw(int x,int y){
	//printf("delete %d %d\n",x,y);
	int i,j;
	c[x][y]=0;
	for(i=0;i<3;i++){
		for(j=0;j<3;j++){
			if(c[x-1+i][y-1+j]==1){
				draw(x-1+i,y-1+j);
			}
		}
	}


}

void printmap(){
	int i,j;
	for(j=0;j<h;j++){
		for(i=0;i<w;i++){
			printf("%d ",c[j][i]);
		}
		printf("\n");
	}
}