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>
using namespace std;

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

int w, h;

void crawler(int y, int x) {
	field[y][x] = 0;
	for (int i = 0; i < 8; i++) {
		int ny = y + dy[i];
		int nx = x + dx[i];
		if (nx < 0 || ny < 0 || w <= nx || h <= ny)continue;
		if (field[ny][nx] == 1)crawler(ny, nx);
	}
}

int main() {
	while (cin >> w >> h, w | h) {
		for (int i = 0; i < h; i++) {
			for (int j = 0; j < w; j++) {
				cin >> field[i][j];
			}
		}
		int ans = 0;
		for (int i = 0; i < h; i++) {
			for (int j = 0; j < w; j++) {
				if (field[i][j] == 1) {
					crawler(i, j);
					ans++;
				}
			}
		}
		cout << ans << endl;
	}
}

C++: #include <iostream>
#include <string>
#include <algorithm>
#include <vector>
#include <cmath>
#include <cstdlib>
#include <tuple>
using namespace std;
using ll=long long;
using R=double;

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};
int field[50][50] = {0};

void dfs(int x, int y){
	field[y][x] = 3;

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

int main(){
	while(1){
		cin >> w >> h;
		if(w==0 and h==0){
			return 0;
		}
		for(int i = 0; i < h; i++){
			for(int j = 0; j < w; j++){
				cin >> field[i][j];
			}
		}

		int count = 0;

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

C++: #include <stdio.h>
#include <string.h>
#define rep(i, n) for (int i =  0; i < (int)(n); i++)

int n, m, f[64][64];

void rec(int x, int y) {
    if (f[x][y] == 0) return ;
    f[x][y] = 0;
    rep (i, 3) rep (j, 3) rec(x+i-1, y+j-1);
}

int main() {
    for (;;) {
        scanf("%d%d", &m, &n);
        if (n == 0) return 0;
        memset(f, 0, sizeof(f));
        rep (i, n) rep (j, m) scanf("%d", f[i+1]+j+1);
        int c = 0;
        rep (i, n) rep (j, m) if (f[i+1][j+1]) c++, rec(i+1, j+1);
        printf("%d\n", c);
    }
}

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

int H,W;

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

bool seen[51][51];
int board[51][51];

  void dfs(int h, int w) {
  seen[h][w] = true; 
  
  for (int dir = 0; dir < 8; ++dir) {
    int nw = w + dx[dir];
    int nh = h + dy[dir];  
    if (nw < 0 || nw >= W || nh < 0 || nh >= H) continue;
    if (seen[nh][nw]) continue; 
    if (board[nh][nw] == 1) dfs(nh,nw);
  }
} 

int main() {  
  cin >> W >> H;
  while (W >= 1 || H >= 1) {
    int ans =0;
    for (int i =0; i<H; i++) {
      for (int j =0; j<W; j++) {
        int tmp; cin >> tmp;
        board[i][j] = tmp;
      }
    }
    
    for (int i =0; i<H; i++) {
      for (int j =0; j<W; j++) {
        seen[i][j] = false;
      }
    }
  
    for (int i =0; i<H; i++) {
      for (int j =0; j<W; j++) {
        if (seen[i][j] == true || board[i][j] == 0) continue;
        dfs(i,j);
        ans++;
      }
    } 
    cout << ans << endl; 
    cin >> W >> H;
    for (int i =0; i<H; i++) {
      for (int j =0; j<W; j++) {
        board[i][j] = 0;
      }
    }
  }
}

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

int W, H;

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

void solve(int cx, int cy, vector<vector<int>>& v) {
    v[cx][cy] = 0;
    for(int i=0; i<8; ++i) {
        int nx = cx + dx[i];
        int ny = cy + dy[i];
        if(nx >= 0 && nx < W && ny >= 0 && ny < H && v[nx][ny] == 1) {
            solve(nx, ny, v);
        }
    }
}


int main() {
    while(cin >> W >> H, W) {
        vector<vector<int>> v(W, vector<int>(H));
        for(int i=0; i<H; ++i) {
            for(int j=0; j<W; ++j) {
                cin >> v[j][i]; // 0: sea, 1:island
            }
        }
        int ans=0;
        for(int i=0; i<W; ++i) {
            for(int j=0; j<H; ++j) {
                if(v[i][j] == 1) {
                    solve(i, j, v);
                    ++ans;
                }
            }
        }
        cout << ans << endl;
    }
}

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

//#define int long long
//struct Fast {Fast(){cin.tie(0);ios::sync_with_stdio(0);}} fast;
//const int MOD = 1e9 + 7;
using intpair = pair<int, int>;

#define rep(i, n) for (int i = 0; i < (int)(n); i++)
#define reps(i, n, m) for (int i = (int)(n); i <= (int)(m); i++)
#define ALL(obj) (obj).begin(), (obj).end()
#define rALL(obj) (obj).rbegin(), (obj).rend()
#define ceiv(a, b) ((int)((a) + (b) - 1)/(int)(b))

int H, W;
bool is_safe(int x, int y){
    return (0 <= x && x < W)&&(0 <= y && y < H);
}

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

void solve(){
    bool field[H][W], visited[H][W];
    rep(i,H)rep(j,W) visited[i][j] = false;
    rep(i,H)rep(j,W) cin >> field[i][j];
    
    int ans = 0;
    
    rep(y,H)rep(x,W){
        if(visited[y][x] || !field[y][x]) continue;
        stack<intpair> s;
        s.push({x,y});
        ans++;
        while(!s.empty()){
            int x2 = s.top().first, y2 = s.top().second;
            visited[y2][x2] = true;
            s.pop();
            rep(i,8){
                if(!is_safe(x2+dx[i], y2+dy[i])) continue;
                if(!field[y2+dy[i]][x2+dx[i]])   continue;
                if(visited[y2+dy[i]][x2+dx[i]])  continue;
                s.push({x2+dx[i],y2+dy[i]});
            }
        }
    }
    cout << ans << endl;
}

signed main(){
    while(true){
        cin >> W >> H;
        if(H==0 && W==0) break;
        solve();
    }
}

C++: #include <bits/stdc++.h>
#define rep(i,a,b) for(int i=int(a);i<int(b);++i)
#define SIZE 200005
#define INF 1000000005LL
#define MOD 1000000007

using namespace std;
typedef long long int ll;
typedef pair <int,int> P;

int w,h;
int c[55][55];
int d[55][55];
int a;

void dfs(int i,int j);
void dfs_(int i,int j){
  if(i>=0 && j>=0) if(c[i][j]==1) dfs(i,j);
}

void dfs(int i,int j){
  if(d[i][j]==1)return;
  d[i][j] = 1;
  rep(l,0,3) rep(r,0,3) dfs_(i-1+l,j-1+r);
}


int imain(){
  a=0;
  rep(i,0,52) rep(j,0,52) c[i][j]=d[i][j]=0;
  rep(i,0,h) rep(j,0,w) {
    cin >> c[i][j];
  }

  rep(i,0,h) rep(j,0,w) if(c[i][j]==1 && d[i][j]==0) {
    dfs(i,j);
    a++;
  }
  cout << a << endl;

  return 0;
}

int main(){
  for(;;){
    cin >> w >> h;
    if(w==0 && h==0)break;
    imain();
  }
  return 0;
}