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>
#include<vector>
#include <unordered_map>
#include <algorithm>
#include <functional>
#include <map>
#include <string>
#include <sstream>
#include <cmath>
#include <float.h>
using namespace std;



bool checked[50][50];
int island[50][50];
int count1 = 0;
int w, h;
vector<int> num;
unsigned long long int mod = 1000000007;

void checkIsland(int x, int y) {
    if (x < 0 || x >= h || y >= w || y < 0) { return; }
    if (!island[x][y] || checked[x][y]) {
        checked[x][y] = true;
        return;
    }
    checked[x][y] = true;
    checkIsland(x + 1, y);
    checkIsland(x + 1, y + 1);
    checkIsland(x + 1, y - 1);
    checkIsland(x, y + 1);
}

void runtime() {
    count1 = 0;
    for (int i = 0; i < w; ++i) {
        for (int j = 0; j < h; ++j) {
            checked[i][j] = false;
            cin >> island[i][j];
        }
    }
    for (int i = 0; i < w; ++i) {
        for (int j = 0; j < h; ++j) {
            if (checked[i][j]) continue;
            if (island[i][j]) {
                count1++;
                checkIsland(i, j);
            }
            else {
                checked[i][j] = true;
            }
        }
    }
    num.push_back(count1);
}


int main() {
    cin >> w >> h;
    while (!(w == 0 && h == 0)) {
        runtime();
        cin >> w >> h;
    }
    for (auto i : num) {
        cout << i << endl;
    }
    return 0;
}

C++: #include <bits/stdc++.h>
using namespace std;
using ll=long long;
typedef pair<int,int> P;
template<class T> inline bool chmax(T& a, T b) { if (a < b) { a = b; return 1; } return 0; }
template<class T> inline bool chmin(T& a, T b) { if (a > b) { a = b; return 1; } return 0; }
int main(){
    int w,h;
    cin>>w>>h;
    vector<vector<int>>G(h,vector<int>(w));
    queue<P> Q;
    for(int i=0;i<h;i++){
        for(int j=0;j<w;j++){
            cin>>G.at(i).at(j);
        }
    }
    int count=0,x,y,nx,ny;
    P p;
    for(int i=0;i<h;i++){
        for(int j=0;j<w;j++){
            if(G.at(i).at(j)==1){
            Q.push(P(i,j));
            while(!Q.empty()){
        p=Q.front();
        Q.pop();
        x=p.first;
        y=p.second;
        G.at(x).at(y)=0;
            for(int i=-1;i<=1;i++){
        for(int j=-1;j<=1;j++){
            nx=x+i;
            ny=y+j;
        if(0<=nx&& nx<h && 0<=ny&& ny<w && G.at(nx).at(ny)==1) Q.push(P(nx,ny));
        }
    }
    }
    count++;
    }
    }
    }
cout<<count<<endl;
}

C++: #include <stdio.h>

int w, h;
int island[50][50], map[50][50], ans;

void dfs(int x, int y, int flg) {
		int i, j;
	if(x < 0 || w <= x || y < 0 || h <= y || map[x][y] == 1) {
		return;
	}
	else if(map[x][y] == 0) {
		if(island[y][x] == 0) {
			if(flg == 0) {
			 	map[x][y] = 1;
		
				for(i = -1; i <= 1; i ++) {
					for(j = -1; j <= 1; j ++) {
						dfs(x + i, y + j, 0);
					}
				}
			}
			else if(flg == 1) {
				return;
			}
		}
		else if(island[y][x] == 1) {
			map[x][y] = 1;  
					
			if(flg == 0) {
				ans ++; 
			}
			for(i = -1; i <= 1; i ++) {
				for(j = -1; j <= 1; j ++) {
					dfs(x + i, y + j, 1);
				}
			}
		}
	}
	return;
}

int main() {
	
	int i, j;
	while(scanf("%d %d", &w, &h)) {
		if(w == 0 && h == 0) { break; }
		
		ans = 0;
		for(i = 0; i < 50; i ++) {
			for(j = 0; j < 50; j ++) {
				map[i][j] = -1;
				island[i][j] = 0;
			}
		}
	
		for(i = 0; i < h; i ++) {
			for(j = 0; j < w; j ++) {
				scanf("%d", &island[i][j]);
				map[i][j] = 0;
			}
		}
		
		for(i = 0; i < h; i ++) {
			for(j = 0; j < w; j ++) {
				dfs(i, j, 0);
			}
		}
		printf("%d\n", ans);
	}

	return 0;
}

C++: #include <iostream>
#include <ios>
#include <iomanip>
#include <algorithm>
#include <string>
#include <vector>
#include <map>
#include <set>
#include <cstdlib>
#include <cstring>
#include <cmath>
using namespace std;

#define MOD 1e9+7
#define INF 1e9+7
#define rep(i, n) for(int i=0; i<n; ++i)
typedef long long ll;

#define MAX_N 100000
#define MAX_V 100
#define MAX_E 100

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

int ddx[] = {-1, 0, 1, 0, -1, 1, -1, 1};
int ddy[] = {0, -1, 0, 1, 1, 1, -1, -1};

//--------------------------

void dfs(int x, int y);

int w, h, cnt;

int c[50][50];

int main() {

  while(true) {
    cin >> w >> h;

    if(w==0&&h==0) break;

    cnt = 0;

    rep(i, h) {
      rep(j, w) cin >> c[i][j];
    }

    rep(i, h) {
      rep(j, w) {
        if(c[i][j]==1) {
          dfs(i, j);
          ++cnt;
        }
      }
    }

    cout << cnt << endl;
  }

  return 0;
}

void dfs(int x, int y) {

  if(x<0||x>=h||y<0||y>=w||c[x][y]==0) {
    return;
  }

  c[x][y] = 0;

  rep(i, 8) {
    dfs(x+ddx[i], y+ddy[i]);
  }
}

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

int is[52][52];
int w, h, c;

void search(int x, int y){
	int i, j;
	if(!is[x][y]) return;
	is[x][y] = 0;
	for(i = -1; i <= 1; i++){
		for(j = -1; j <= 1; j++){
			search(x + i , y + j);
		}
	}
	return;
}

int main(void){
	int i, j;
	while(cin >> h >> w, w){
		memset(is, 0, sizeof(is));
		c = 0;
		for(i = 1; i <= w; i++)
			for(j = 1; j <= h; j++)
				cin >> is[i][j]; 

		while(1){
			for(i = 1; i <= w; i++){
				for(j = 1; j <= h; j++){
					if(is[i][j]) break;
				}
				if(is[i][j]) break;
			}
			if(i == w+1) break;
			search(i, j);
			c++;
		}
		cout << c << endl;
	}
	return 0;
}

C++: #include<queue>
#include<cstdio>

#define	mp	make_pair

using namespace std;

typedef	pair<int,int>	pii;

const int dx[]={1,1,0,-1,-1,-1,0,1},dy[]={0,-1,-1,-1,0,1,1,1};
int field[50][50];
bool visited[50][50];

int main(){
	for(int w,h;scanf("%d%d",&w,&h),w;){
		for(int i=0;i<h;i++)for(int j=0;j<w;j++){
			scanf("%d",field[i]+j);
			visited[i][j]=false;
		}

		int cnt=0;
		for(int i=0;i<h;i++)for(int j=0;j<w;j++){
			if(field[i][j]==1 && !visited[i][j]){
				visited[i][j]=true;
				queue<pii> qu;	qu.push(mp(i,j));
				while(!qu.empty()){
					pii a=qu.front();	qu.pop();
					for(int k=0;k<8;k++){
						int xx=a.second+dx[k],yy=a.first+dy[k];
						if(0<=xx && xx<w && 0<=yy && yy<h
						&& field[yy][xx]==1 && !visited[yy][xx]){
							qu.push(mp(yy,xx));
							visited[yy][xx]=true;
						}
					}
				}
				cnt++;
			}
		}

		printf("%d\n",cnt);
	}

	return 0;
}

C++: #include<iostream>
using namespace std;
int w,h,i,j,c[50][50];

void visit(int i,int j){

  if(i<0||j<0)return;
  if(i>=50||j>=50)return;

  c[i][j]=0;
  for(int k=-1;k<=1;k++)
    for(int l=-1;l<=1;l++)
      if(c[i+k][j+l]==1)
	visit(i+k,j+l);
     
}
int main(){
  int count;
  while(1){ 
    count=0;
    cin>>w>>h;
    if(w==0&&h==0)break; 
    for(i=0;i<h;i++){
      for(j=0;j<w;j++){
	cin>>c[i][j];
      }
    }
    for(i=0;i<h;i++){
      for(j=0;j<w;j++){
	if(c[i][j]==1){
	  count++;
	  visit(i,j);
	}
      }
    }
    cout<<count<<endl;
  }
  return 0;
}