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 <bits/stdc++.h>
using namespace std;

using ll=long long;
#define int ll

#define FOR(i,a,b) for(int i=int(a);i<int(b);i++)
#define REP(i,b) FOR(i,0,b)

int read(){
	int i;
	scanf("%lld",&i);
	return i;
}

int land[1000][1000]={-1};
int flag[1000][1000]={-1};
int dy[]={-1,-1,-1,0,0,1,1,1};
int dx[]={-1,0,1,-1,1,-1,0,1};
int cnt=0;

void isLand(int i,int j){
	REP(k,8){
		int newx=i+dx[k];
		int newy=j+dy[k];
		if(land[newx][newy]){
			if(flag[newx][newy]!=-1){
				flag[i][j]=flag[newx][newy];
				return;
			}
		}
	}
	cnt++;
	flag[i][j] = cnt;
}

signed main(){
	int w,h;
	while( w=read(),h=read(),w||h){
		cnt=0;
		
		REP(i,1000){
			REP(j,1000){
				land[i][j]=-1;
				flag[i][j]=-1;
			}
		}
		
		FOR(i,1,w+1){
			FOR(j,1,h+1){
				land[i][j]=read();
			}
		}
		
		FOR(i,1,w+1){
			FOR(j,1,h+1){
				if(land[i][j])isLand(i,j);
			}
		}
		
		cout << cnt << endl;
	}
	
}

C++: #include <algorithm>
#include <bitset>
#include <cassert>
#include <cctype>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <ctime>
#include <deque>
#include <functional>
#include <iomanip>
#include <iostream>
#include <list>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <utility>
#include <vector>

/*
 * Union-Find tree
 * header requirement: vector
 */
class UnionFind {
private:
  std::vector<int> disj;
  std::vector<int> rank;
public:
  UnionFind(int n) : disj(n), rank(n) {
    for (int i = 0; i < n; ++i) {
      disj[i] = i;
      rank[i] = 0;
    }
  }
  int root(int x) {
    if (disj[x] == x) {
      return x;
    }
    return disj[x] = root(disj[x]);
  }
  void unite(int x, int y) {
    x = root(x);
    y = root(y);
    if (x == y) {
      return;
    }
    if (rank[x] < rank[y]) {
      disj[x] = y;
    } else {
      disj[y] = x;
      if (rank[x] == rank[y]) {
	++rank[x];
      }
    }
  }
  bool is_same_set(int x, int y) {
    return root(x) == root(y);
  }
};


#define REP(i,s,n) for(int i=(int)(s);i<(int)(n);i++)

using namespace std;
typedef long long int ll;
typedef vector<int> VI;
typedef vector<ll> VL;
typedef pair<int, int> PI;
const ll mod = 1e9 + 7;



int main(void){
  int w, h;
  while ((cin >> w >> h) && (w || h)) {
    vector<VI> mp(h, VI(w, 0));
    REP(i, 0, h) {
      REP(j, 0, w) {
	cin >> mp[i][j];
      }
    }
    UnionFind uf(w * h);
    REP(i, 0, h) {
      REP(j, 0, w) {
	if (mp[i][j] == 0) { continue; }
	REP(dx, -1, 2) {
	  REP(dy, -1, 2) {
	    if (dx == 0 && dy == 0) { continue; }
	    int nx = i + dx;
	    int ny = j + dy;
	    if (0 <= nx && nx < h && 0 <= ny && ny < w && mp[nx][ny]) {
	      // unite
	      uf.unite(i * w + j, nx * w + ny);
	    }
	  }
	}
      }
    }
    set<int> roots;
    REP(i, 0, h * w) {
      int x = i / w;
      int y = i % w;
      if (mp[x][y]) {
	roots.insert(uf.root(i));
      }
    }
    cout << roots.size() << endl;
  }
}

C++: #include <bits/stdc++.h>
using namespace std;
typedef long long ll;

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

void dfs(int i, int j) {
  if(i < 0 || i >= h || j < 0 || j >= w || c[i][j] == 0)
    return;

  c[i][j] = 0;
  for(int k=0;k<8;k++) {
    dfs(i+dy[k], j+dx[k]);
  }
  return;
}

int main() {
  cin.tie(0);
  ios::sync_with_stdio(false);
  while(true) {
    cin >> w >> h;
    if(w == 0 && h == 0)
      break;
    for(int i=0;i<h;i++) {
      for(int j=0;j<w;j++) {
        cin >> c[i][j];
      }
    }

    ll ans = 0;
    for(int i=0;i<h;i++) {
      for(int j=0;j<w;j++) {
        if(c[i][j] == 1) {
          ans++;
          dfs(i, j);
        }
      }
    }
    cout << ans << endl;

  }
}

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

using namespace std;

int w, h;
int maps[50][50];

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

void dfs(int x, int y)
{
  maps[y][x] = 0;
  for(int i = 0; i < 8; i++){
    int nx = x + dx[i], ny = y + dy[i];
    if(0 <= nx && nx < w && 0 <= ny && ny < h && maps[ny][nx] == 1)
      dfs(nx, ny);
  }
  return;
}

int main()
{
  while(cin >> w >> h, w || h){
    for(int i = 0; i < h; i++)
      for(int j = 0; j < w; j++)
	cin >> maps[i][j];

    int cnt = 0;
    for(int i = 0; i < h; i++)
      for(int j = 0; j < w; j++)
	if(maps[i][j] == 1)
	  cnt++, dfs(j, i);
    
    cout << cnt << endl;
  }
  return 0;
}

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

using namespace std;

bool check(bool[52][52],pair<int,int>);
void rmv(bool[52][52],int,int);

int main(void){
	for(int w,h;cin>> w >> h,w||h;){
		bool fld[52][52]={0};
		int n;

		for(int i=1;i<=h;i++)
			for(int j=1;j<=w;j++)
				cin >> fld[i][j];

		for(n=0;check(fld,make_pair(w,h));n++);
		cout << n << endl;
	}
	return 0;
}

bool check(bool fld[52][52],pair<int,int> wide){
	for(int i=1;i<=wide.second;i++)
		for(int j=1;j<=wide.first;j++)
			if(fld[i][j]){
				rmv(fld,i,j);
				return true;
			}

	return false;
}

void rmv(bool fld[52][52],int y,int x){
	if(fld[y][x]){
		fld[y][x]=false;
		for(int i=-1;i<=1;i++)
			for(int j=-1;j<=1;j++)
				rmv(fld,y+i,x+j);
	}
}

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

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

void dfs(int y, int x)
{
	f[y][x] = 0;
	for(int i=0; i<8; i++)
	{
		int ny = y+dy[i];
		int nx = x+dx[i];
		if(f[ny][nx])
		{
			dfs(ny,nx);
		}
	}
}

int main()
{
	int w,h,c,x,y;
	while(cin >> w >> h, w||h)
	{
		memset(f,0,sizeof(f));

		for(y=1; y<=h; y++)
		for(x=1; x<=w; x++)
		{
			cin >> f[y][x];
		}

		c=0;
		for(y=1; y<=h; y++)
		for(x=1; x<=w; x++)
		{
			if(f[y][x])
			{
				c++;
				dfs(y,x);
			}
		}
		cout << c << endl;
	}
}

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

int w,h;
int m[50][50];

void solve(int x,int y){
	if(m[y][x] == 1){
		m[y][x] = 0;
		for(int i=-1;i<2;i++){
			for(int j=-1;j<2;j++){
				if(i==0&&j==0){
					continue;
				}
				if(y+i >= 0 && y+i < h && x+j >= 0 && x+j < w){
					solve(x+j,y+i);
				}
			}
		}
	}
}

int main(){
	while(true){
		int count=0;

		cin >> w >> h;
		if(w==0&&h==0){
			break;
		}

		for(int i=0;i<h;i++){
			for(int j=0;j<w;j++){
				cin >> m[i][j];
			}
		}

		for(int i=0;i<h;i++){
			for(int j=0;j<w;j++){
				if(m[i][j] == 1){
					solve(j,i);
					count++;
				}
			}
		}

		cout << count << endl;
	}
}