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;
using ull = unsigned long long;
#define REP(i,a) for(int i=0;i<(a);i++)
#define MOD 1000000007

int main(){
  while(1){
    int h,w;
    cin>>w>>h;
    if(h==0&&w==0) break;
    int a[h][w];
    bool used[h][w];
    for(int i=0;i<h;i++){
      for(int j=0;j<w;j++){
        cin>>a[i][j];
        used[i][j]=0;
      }
    }
    int ans=0;
    queue<pair<int,int>> q;
    for(int i=0;i<h;i++){
      for(int j=0;j<w;j++){
        if(a[i][j]&&!used[i][j]){
          //cout<<i<<" "<<j<<endl;
          ans++;
          q.push(make_pair(i,j));
          while(!q.empty()){
            pair<int,int> p=q.front();
            q.pop();
            int x=p.first, y=p.second;
            if(used[x][y]) continue;
            used[x][y]=1;
            for(int i=-1;i<2;i++){
              for(int j=-1;j<2;j++){
                if(x+i==-1||x+i==h) continue;
                if(y+j==-1||y+j==w) continue;
                if(a[x+i][y+j]&&!used[x+i][y+j]){
                  q.push(make_pair(x+i,y+j));
                }
              }
            }
          }
        }
      }
    }
    cout<<ans<<endl;
  }
  return 0;
}

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[100][100]={0};
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){
    if(land[i][j]==1){
    	land[i][j] = 0;
		REP(k,8){
			int newy=i+dy[k];
			int newx=j+dx[k];
			isLand(newy,newx);
		}
    }
}
???
signed main(){
	int w,h;
	while( w=read(),h=read(),w||h){
		cnt=0;
		
		REP(i,100){
			REP(j,100){
				land[i][j]=0;
			}
		}
		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]==1){
					cnt++;
					isLand(i,j);
				}
			}
		}
		
		cout << cnt << endl;
	}
	
}

C++: #include <iostream>
#include <algorithm>
#include <cstdlib>
#include <cstring>
#include <cstdio>
#include <vector>
#include <queue>
using namespace std;
int H,W;
bool fld[50][50];
int main(){
	queue<pair<int,int> > p;
	while(cin>>W>>H,W,H){
		for(int i=0;i<H;i++){
			for(int j=0;j<W;j++){
				cin>>fld[j][i];
			}
		}
		int cnt=0;
		for(int i=0;i<H;i++){
			for(int j=0;j<W;j++){
				if(fld[j][i]){
					cnt++;
					fld[j][i]=0;
					p.push(make_pair(j,i));
					while(!p.empty()){
						int x=p.front().first,y=p.front().second;
						fld[x][y]=0;
						p.pop();
						for(int dx=-1;dx<=1;dx++)for(int dy=-1;dy<=1;dy++){
							int nx=x+dx,ny=y+dy;
							if(nx>=0 && nx<W && ny>=0 && ny<H && fld[nx][ny]){
								p.push(make_pair(nx,ny));
							}
						}
					}
				}
			}
		}
		cout<<cnt<<endl;
	}
}

C++: #include<iostream>
#include<algorithm>
#include<vector>
#include<stack>
#include<map>
#include<set>
#include<queue>
#include<cstdio>
#include<climits>
#include<cmath>
#include<cstring>
#include<string>
#include<sstream>

#define f first
#define s second
#define mp make_pair

#define REP(i,n) for(int i=0; i<(int)(n); i++)
#define FOR(i,c) for(__typeof((c).begin()) i=(c).begin(); i!=(c).end(); i++)
#define ALL(c) (c).begin(), (c).end()

#define MAX 1000
#define EPS (1e-10)
using namespace std;

typedef unsigned int uint;
typedef long long ll;

int w,h;
int  b[64][64];

void clustering(int x, int y, char num){
  if(b[y][x] == 0) return;

  b[y][x] = 0;

  REP(i,3) REP(j,3){
    int xx = x + j - 1;
    int yy = y + i - 1;
    if(xx >= 0 && xx <= w-1 && yy >= 0 && yy <= h-1)
      clustering(xx, yy, num);
  }
}

int main(){
  while(scanf("%d%d",&w,&h), w+h){
    int cl = 0;
    REP(i,h) REP(j,w) scanf("%d", &b[i][j]);
    REP(i,h) REP(j,w){
      if(b[i][j] == 1){
        clustering(j,i,b[i][j]);
        cl++;
      }
    }
    printf("%d\n",cl);
  }
  return 0;
}

C++: #include<iostream>
#include<string>
#include<algorithm>
#include<vector>
#include<queue>
#include<map>
#include<math.h>
#include<iomanip>
#include<set>
#include<numeric>
#include<cstring>
#include<cstdio>
#include<functional>
#include<bitset>
#include<limits.h>
#include<cassert>
#include<iterator>
#include<complex>
#include<stack>
using namespace std;
typedef  long long int lint;
typedef pair<int, int> P;
typedef pair<lint, lint> LLP;
#define rep(i, n) for (int i = 0; i < n; i++)
#define repr(i, n) for (int i = n; i >= 0; i--)
#define sort(v) sort((v).begin(), (v).end())
#define reverse(v) reverse((v).begin(), (v).end())
/*
vector<char>al(26);
al = {'a','b','c','d','e','f','g','h','i','j','k','l','m','n','o','p','q','r','s','t','u','v','w','x','y','z'};
vector<char>AL(26);
AL = {'A','B','C','D','E','F','G','H','I','J','K','L','M','N','O','P','Q','R','S','T','U','V','W','X','Y','Z'};
*/

int main() {
	int W, H;
	vector<int>dx(8);
	vector<int>dy(8);
	dx = { 1,1,0,-1,-1,-1,0,1 };
	dy = { 0,1,1,1, 0,-1,-1,-1};
	while (cin >> W >> H, W != 0) {
		vector<vector<char>>field(H, vector<char>(W));
		rep(i, H) {
			rep(j, W) {
				cin >> field[i][j];
			}
		}
		int cnt = 0;
		rep(sy, H) {
			rep(sx, W) {
				if (field[sy][sx] == '1') {
					cnt++;
					queue<P>que;
					que.push({ sx,sy });
					field[sy][sx] = '0';
					while (!que.empty()) {
						int x = que.front().first;
						int y = que.front().second;
						que.pop();
						rep(i, 8) {
							int xx = x + dx[i];
							int yy = y + dy[i];
							if (xx >= 0 && xx < W && yy >= 0 && yy < H && field[yy][xx] == '1') {
								que.push({ xx,yy });
								field[yy][xx] = '0';
							}
						}
					}
				}
			}
		}
		cout << cnt << endl;
	}
}