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>
#include <iostream>
#include <fstream>
#include <vector>
#include <string>
#include <cmath>
#include <algorithm>
#include <map>
#include <iomanip>
#include <stdlib.h>
#include <stdio.h>
#include <queue>
#include <deque>
#include <set>
#include <stack>
#include <time.h>
 
using namespace std;
 
typedef long long ll;
typedef long double ld;
typedef pair<int, int> Pii;
typedef pair<int, ll> Pil;
typedef pair<ll, ll> Pll;
typedef pair<ll, int> Pli;
 
const ll MOD = 1e9 + 7;
const ll MOD2 = 998244353;
const ll INF = 1ll << 60;
const double PI = 2 * asin(1);

void yes() {printf("yes\n");}
void no() {printf("no\n");}
void Yes() {printf("Yes\n");}
void No() {printf("No\n");}
void YES() {printf("YES\n");}
void NO() {printf("NO\n");}

int W, H, C[100][100];
const int di[3] = {-1, 0, 1}, dj[3] = {-1, 0, 1};
bool visited[100][100];

int Solve(){
  fill((bool*)visited, (bool*)(visited + H), false);
  for (int i = 0; i < H; i++){
    for (int j = 0; j < W; j++){
      cin >> C[i][j];
    }
  }
  
  int nowi = 0, ans = 0;
  queue <Pii> que;
  for (int i = 0; i < H; i++){
    for (int j = 0; j < W; j++){
      if (C[i][j] == 1){
        que.push(make_pair(i, j));
        nowi = i; visited[i][j] = true;
        ans++;
        break;
      }
    }
    if (!que.empty()) break;
  }
  
  while (!que.empty()){
    Pii Q = que.front(); que.pop();
    int I = Q.first, J = Q.second;

    for (int i = 0; i < 3; i++){
      for (int j = 0; j < 3; j++){
        if (0 <= I + di[i] && I + di[i] < H && 0 <= J + dj[j] && J + dj[j] < W){
          if (C[I + di[i]][J + dj[j]] == 1 && visited[I + di[i]][J + dj[j]] == false){
            que.push(make_pair(I + di[i], J + dj[j]));
            visited[I + di[i]][J + dj[j]] = true;
          }
        }
      }
    }

    if (que.empty()){
      for (int i = nowi; i < H; i++){
        for (int j = 0; j < W; j++){
          if (C[i][j] == 1 && visited[i][j] == false){
            que.push(make_pair(i, j)); ans++; nowi = i;
            visited[i][j] = true; break;
          }
        }
        if (!que.empty()) break;
      }
    }
  }
  cout << ans << endl;
  
  return 0;
}

int main(){
  while (cin >> W >> H){
    if (W == 0 && H == 0) return 0;
    Solve();
  }
  
  return 0;
}

C++: #include <iostream>
#include <vector>
#include <string>

using namespace std;

void fill(vector< vector<int> >& table, int i, int j, int w, int h, int e){
    if(i < 0 || j < 0) return;
    if(i >= h || j >= w) return;
    for(int k=-1; k <= 1; ++k){
        for(int l=-1; l <= 1; ++l){
            if(i+k < 0 || j+l < 0) continue;
            if(i+k >= h || j+l >= w) continue;
            if(table[i+k][j+l] == 1) {
                table[i+k][j+l] = e;
                fill(table, i+k, j+l, w, h, e);
            }
        }
    }
    return;
}

int main(){
    while(true){
        int w, h;
        int cnt = 1;
        cin >> w;
        cin >> h;
        if(w == 0 && h == 0) break;
        vector< vector<int> > table(h, vector <int>(w));
        for(int i=0; i<h; ++i){
            for(int j=0; j<w; ++j){
                cin >> table[i][j];
            }
        }
        for(int i=0; i<h; ++i){
            for(int j=0; j<w; ++j){
                int e = table[i][j];
                if(e == 0) continue;
                else if(e == 1) {
                    cnt++; e = cnt; table[i][j] = e;
                    fill(table, i, j, w, h, e);
                }
            }
        }
        /*
        for(int i=0; i<h; ++i){
            for(int j=0; j<w; ++j){
                cout << table[i][j] << " ";
            }
            cout << endl;
        }*/
        cout << (cnt-1) << endl;
    }
}

C++: #include<stdio.h>
#include<iostream>

int field[51][51];

void land(int x,int y){
	field[x][y]=0;

	for(int xx=-1;xx<=1;xx++){
		for(int yy=-1;yy<=1;yy++){

			if(field[x+xx][y+yy]==1){
				land(x+xx,y+yy);
			}

		}
	}
	return;
}

int main(void){

	int w,h,kazu=0;

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

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

	for(int i=0;i<h;i++){
		for(int j=0;j<w;j++){

			if(field[i][j]==1){
				land(i,j);
				kazu++;
			}

		}
	}

	printf("%d",kazu);

}

C++: #include <algorithm>
#include <climits>
#include <cmath>
#include <cstdio>
#include <cstring>
#include <iostream>
#include <queue>
#include <stack>
#include <string>
#include <vector>
#define INF 10000000
#define PI 3.14159265358979323846
using namespace std;
typedef long long ll;
typedef pair<int, int> p;
typedef vector<int> v;
typedef vector<int, int> v2;
typedef vector< pair<int, int> > vp;
int dx[4] = { 0, 1, 0, -1 };
int dy[4] = { 1, 0, -1, 0 };

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

bool dfs(int y, int x){
	if (c[y][x] == 0) return false;
	if (f[y][x] == 1) return false;
	
	f[y][x] = 1;
	if (y > 0) dfs(y - 1, x);
	if (x > 0) dfs(y, x - 1);
	
	if (y < h) dfs(y + 1, x);
	if (x < w) dfs(y, x + 1);

	if (y > 0 && x > 0) dfs(y - 1, x - 1);
	if (y < h && x > 0) dfs(y + 1, x - 1);
	if (y > 0 && x < w) dfs(y - 1, x + 1);
	if (y < h && x < w) dfs(y + 1, x + 1);

	return true;
}

int main(){
	ios::sync_with_stdio(false);

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

		int ans = 0;

		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++)
				f[i][j] = -1;
		
		for (int i = 0; i < h; i++)
			for (int j = 0; j < w; j++)
				if(dfs(i, j)) ans++;

		cout << ans << endl;

	}

	return 0;
}

C++: #include<stdio.h>
#include<string.h>

int main(){
	int zu[52][52],w,h,i,j,flag,land;
	
	while(0<=scanf("%d%d",&h,&w)){
		if(w==0&&h==0)break;
		
		memset(zu,0,sizeof(zu));
		
		for(i=1;i<=w;i++){
			for(j=1;j<=h;j++){
				scanf("%d",&zu[i][j]);
			}
		}
		
		flag=1;
		land=1;
		while(flag!=0){
			++land;
			flag=0;
			for(i=1;i<=w;i++){
				for(j=1;j<=h;j++){
					if(zu[i][j]==1){
						zu[i][j]=land;
						flag=1;
						break;
					}
				}
				if(flag==1)break;
			}
			
			while(flag==1){
				flag=2;
				for(i=1;i<=w;i++){
					for(j=1;j<=h;j++){
						if(zu[i][j]!=1)continue;
						if(zu[i+1][j]==land || zu[i-1][j]==land || zu[i][j+1]==land || zu[i][j-1]==land ||
							zu[i+1][j+1]==land|| zu[i+1][j-1]==land||zu[i-1][j+1]==land||zu[i-1][j-1]==land){
							zu[i][j]=land;
							flag=1;
						}
					}
				}
			}
		}
		for(i=1;i<=w;i++){
			for(j=1;j<=h;j++){
//				printf("%d ",zu[i][j]);
			}
//			printf("\n");
		}
		
		
		printf("%d\n",land-2);
	}
	return 0;
}