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<bits/stdc++.h>
#define rep(i,n)for(int i=0;i<n;i++)
using namespace std;

int w, h;
char c[51][51];

void dfs(int x, int y) {
	c[x][y] = 0;
	for (int i = -1; i <= 1; i++) {
		for (int j = -1; j <= 1; j++) {
			int nx = x + i, ny = y + j;
			if (0 <= nx&&nx < h && 0 <= ny&&ny < w&&c[nx][ny])
				dfs(nx, ny);
		}
	}
}
int main() {
	while (scanf("%d%d", &w, &h), w) {
		rep(i, h)rep(j, w)scanf("%d", &c[i][j]);
		int cnt = 0;
		rep(i, h)rep(j, w) {
			if (c[i][j]) {
				dfs(i, j);
				cnt++;
			}
		}
		printf("%d\n", cnt);
	}
}

C++: #include <iostream>
using namespace std;
int W,H,c[50][50];
void island(int h, int w){
    c[h][w]=0;
    for(int i=h-1; i<h+2; i++){
        for(int j=w-1; j<w+2; j++){
            if(c[i][j]==1 && i>=0 && j>=0 && i<H && j<W) island(i,j);
        }
    }
}

int main(void){
    int count;
    while(1){
        cin>>W>>H;
        count=0;
        if(W==0 && H==0) break;
        for(int i=0; i<H; i++){
            for(int j=0; j<W; j++){
                cin>>c[i][j];
            }
        }
        for(int i=0; i<H; i++){
            for(int j=0; j<W; j++){
                if(c[i][j]==1){
                    count++;
                    island(i,j);
                }
            }
        }
        cout<<count<<endl;
    }
}

C++: #include<iostream>
#include<cstdio>
#include<algorithm>
using namespace std;
int island[52][52];
void func(int,int);

int main(){
  int w,h;
  int ans;
  
  while(1){
    cin>>w>>h;
    if(w==0 && h==0) break;
    ans=0;
    fill(island[0],island[52],0);
    
    for(int i=1;i<=h;i++){
      for(int l=1;l<=w;l++){
        cin>>island[i][l];
      }
    }
    
    for(int i=1;i<=h;i++){
      for(int l=1;l<=w;l++){
        if(island[i][l]==1){
          func(i,l);
          ans++;
        }
      }
    }
    printf("%d\n",ans);
  }
  return 0;
}

void func(int x,int y){
  island[x][y]=0;
  for(int i=x-1;i<x+2;i++){
    for(int l=y-1;l<y+2;l++){
      if(island[i][l]==1)
        func(i,l);
    }
  }
}

C++: #include <stdio.h>
int m[51][51]={};
void dfs(int i,int j){
 m[i][j]=1;
 if(m[i+1][j]==2)dfs(i+1,j);
 if(m[i+1][j+1]==2)dfs(i+1,j+1);
 if(m[i][j+1]==2)dfs(i,j+1);
 if(m[i-1][j+1]==2)dfs(i-1,j+1);
 if(m[i-1][j]==2)dfs(i-1,j);
 if(m[i-1][j-1]==2)dfs(i-1,j-1);
 if(m[i][j-1]==2)dfs(i,j-1);
 if(m[i+1][j-1]==2)dfs(i+1,j-1);
}

int main(){
  int cou,i,j,y,t,a;
 while(1){
   cou=0;
   scanf("%d %d",&y,&t);
   if(y==0&&t==0)break;
   for(i=1;i<=t;i++){
     for(j=1;j<=y;j++){
	scanf("%d",&a);
	a++;
	m[i][j]=a;
     }
   }
   for(i=1;i<=t;i++){
     for(j=1;j<=y;j++){
	if(m[i][j]==2){
	  cou++;
	  dfs(i,j);
	}
     }
   }
   printf("%d\n",cou);
 }
 return 0;
}

C++: //INCLUDE
//------------------------------------------
#include <bits/stdc++.h>
//DEFINE
//------------------------------------------
#define ll long long
#define ld long double
#define ALLv(a)  (a).begin(),(a).end()
#define ALL(a,n)  (a),(a)+n
#define vi vector<long long>
#define vd vector<long double>
#define vs vector<string>
#define dcml(n) fixed<<setprecision(n)
//CONST
//------------------------------------------
const ll INF=1010000000000000017LL;
const ll MOD=1000000007LL;
const ld EPS=1e-12;
const ld PI=3.14159265358979323846;
//REPEAT
//------------------------------------------
#define   FOR(i,m,n) for(ll (i)=(m);   (i)<(n);  (i)++)
#define  FORS(i,m,n) for(ll (i)=(m);   (i)<=(n); (i)++)
#define  RFOR(i,m,n) for(ll (i)=(n)-1; (i)>=(n); (i)--)
#define RFORS(i,m,n) for(ll (i)=(n);   (i)>(n);  (i)--)
#define   REP(i,n)   for(ll (i)=0;     (i)<(n);  (i)++)
#define  REPS(i,x)   for(ll (i)=1;     (i)<=(x); (i)++)
#define  RREP(i,x)   for(ll (i)=(x)-1; (i)>=0;   (i)--)
#define RREPS(i,x)   for(ll (i)=(x);   (i)> 0;   (i)--)
#define  WREP(i,in,j,jn) REP(i,in)REP(j,jn)
#define WREPS(i,in,j,jn) REPS(i,in)REPS(j,jn)
//-----------------------------------------
//namespace
using namespace std;
//ライブラリはここに
//↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓
vi dx8={-1,-1,-1,0,0,1,1,1};
vi dy8={-1,0,1,-1,1,-1,0,1};
//-----------------------------------------
ll w,h;
int c[50][50];
bool is;
void dfs(int x,int y){
    if(c[x][y]==0)return;
    c[x][y]=0;
    //cout<<x<<y<<"->";
    is=true;
    REP(i,8){
        int X=x+dx8[i],Y=y+dy8[i];
        if(0<=X&&X<h and 0<=Y&&Y<w){
            if(c[X][Y]==0)continue;
            dfs(X,Y);
        }
    }
}
int main(void){
    while(cin>>w>>h and !(w==0&&h==0) ){
        //cout<<"w:"<<w<<"h:"<<h<<"\n";
        int cnt=0;
        REP(i,h)REP(j,w)cin>>c[i][j];
        REP(i,h)REP(j,w){
            is=false;
            dfs(i,j);//cout<<"\n";
            if(is)cnt++;
        }
        cout<<cnt<<"\n";
    }
}

C++: #include <iostream>
using namespace std;
int w,h;
int in[100][100];
bool seen[100][100];
int ans =0;
bool flag = false;

void solve(int i,int j){
    //cout << i << " " << j << endl;
    if(i < 0 || j < 0 || i >= h || j >= w){
        return;
    }
    if(seen[i][j]){
        return;
    }
    if(in[i][j] == 0){
        return;
    }
    seen[i][j] = true;
    solve(i-1,j-1);
    solve(i-1,j);
    solve(i-1,j+1);
    solve(i,j-1);
    solve(i,j+1);
    solve(i+1,j-1);
    solve(i+1,j);
    solve(i+1,j+1);

}
int main(){
    cin >> w >> h;
    for(int i=0;i<h;i++){
        for(int j=0;j<w;j++){
            cin >> in[i][j];
        }
    }
    for(int i=0;i<h;i++){
        for(int j=0;j<w;j++){
            if(seen[i][j] == true || in[i][j] == 0){
            }
            else{
                solve(i,j);
                ans++;
            }
            
        }
    }
    if(w == 0 || h == 0){
    }
    else{
        cout << ans << endl;
    }
    return 0;
}

C++: #include <bits/stdc++.h>
using namespace std;
#ifdef ENABLE_DEBUG_OUTPUT
#define DEBUG_LOG(s) cout << s << endl;
#else
#define DEBUG_LOG(s) void();
#endif

int h, w;
vector<vector<bool>> c;
vector<vector<bool>> v;
const int dx[8] = { 0, 1, 1,  1,  0, -1, -1, -1 };
const int dy[8] = { 1, 1, 0, -1, -1, -1,  0,  1 };

bool dfs(const int x, const int y) {
    // cout << x << " " << y << " " << c[x][y] << " " << v[x][y] << endl;
    if(not c[x][y]) return false; // sea
    if(v[x][y]) return false; // already visited
    v[x][y] = true;

    for (auto i = 0; i < 8; i++) {
        int nx = x + dx[i], ny = y + dy[i];
        if(nx < 0 or nx >= h or ny < 0 or ny >= w) continue;
        dfs(x + dx[i], y + dy[i]);
    }

    return true;
}

int main(){
    while(true) {
        cin >> w >> h;
        if(w == 0 and h == 0) break;

        c.assign(h, vector<bool>(w, false));
        for (auto i = 0; i < h; i++) {
            for (auto j = 0; j < w; j++) {
                int val; cin >> val;
                c[i][j] = static_cast<bool>(val); // true : land, false : sea
            }
        }

        int ans = 0;
        v.assign(h, vector<bool>(w, false));
        for (auto i = 0; i < h; i++) {
            for (auto j = 0; j < w; j++) {
                if(dfs(i, j)) ans++;
            }
        }

        cout << ans << endl;
    }

    return 0;
}