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];
int dx[] = { -1,-1,-1,0,0,1,1,1 };
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 <bits/stdc++.h>
#include <cassert>
#define rep(i, n) for (int i = 0; i < (n); i++)
using namespace std;
using ll = long long;
using P = pair<int, int>;

const long long MOD = 1000000007;
const long long INF = 1LL << 60;

int w, h, x, y;

void dfs(vector<vector<int>> &map, int x, int y)
{
  // 今いるところを0に置き換える
  map[x][y] = 0;

  for (int dx = -1; dx <= 1; dx++)
  {
    for (int dy = -1; dy <= 1; dy++)
    {
      // x方向にdx移動したものをnx，y方向にdy移動したものをnyとおく
      int nx = x + dx, ny = y + dy;

      // nxとnyが地図の範囲内かどうかとmap[nx][ny]が陸なのかを判定
      if (0 <= nx && nx < h && 0 <= ny && ny < w && map[nx][ny] == 1)
        dfs(map, nx, ny);
    }
  }
  return;
}

void solve(vector<vector<int>> &map)
{
  int ans = 0;
  for (int i = 0; i < h; i++)
  {
    for (int j = 0; j < w; j++)
    {
      if (map[i][j] == 1)
      {
        dfs(map, i, j);
        ans++;
      }
    }
  }
  cout << ans << endl;
}

int main()
{
  ios::sync_with_stdio(false);
  cin.tie(nullptr);
  while (1)
  {
    cin >> w >> h;
    if (w == 0 && h == 0)
      break;
    vector<vector<int>> map(h, vector<int>(w));
    for (int i = 0; i < h; i++)
    {
      for (int j = 0; j < w; j++)
      {
        cin >> map[i][j];
      }
    }
    solve(map);
  }
  return 0;
}

C++: #include<iostream>
#include <cstdio>
#include<algorithm>
#include<vector>
#include<set>
#include<utility>
#include<queue>

#define REP(i,n) for(int i = 0;i < n ;i++)
	
using namespace std;

int mp[60][60],dp[60][60];
int w,h,ans;
int dx[] = {1,1,0,-1,-1,-1,0,1},dy[] = {0,1,1,1,0,-1,-1,-1};

void dfs(int x,int y)
{
	REP(i,8)
	{
		int cx = x + dx[i],cy = y + dy[i];
		if(cx >= 0 && cx < h && cy >= 0 \
				&& cy < w && dp[cx][cy] == 0 && mp[cx][cy] == 1)
				{
					dp[cx][cy] = 1;
					dfs(cx,cy);
				
				}
				
	}
}
			

int main()
{
	for(;;)
	{
		ans = 0;
		REP(i,60)REP(j,60)dp[i][j]=0;
		cin >> w >> h;
		if(w == 0 && h == 0) break;
		REP(i,h)REP(j,w)cin >> mp[i][j];
		REP(i,h)REP(j,w)
		{
			if(mp[i][j] == 1 && dp[i][j] == 0)
			{
				ans++;
				dfs(i,j);
			}
		}
		
		cout << ans << endl;
		
	}
	return 0;

}

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

constexpr int MAX_H = 50;
constexpr int MAX_W = 50;

int H, W;
int grid[MAX_H][MAX_W];
bool reached[MAX_H][MAX_W];

#define OCTAS
#ifdef OCTAS
constexpr int dh[]{-1, -1, -1, 0, 0, +1, +1, +1};
constexpr int dw[]{-1, 0, +1, -1, +1, -1, 0, +1};
#else
constexpr int dh[]{-1, 0, 0, +1};
constexpr int dw[]{0, -1, +1, 0};
#endif

void dfs(int h, int w) {
  reached[h][w] = true;

  for (int i{}; i < sizeof(dh) / sizeof(int); ++i) {
    int nh = h + dh[i];
    if (nh < 0 || nh >= H)
      continue;
    int nw = w + dw[i];
    if (nw < 0 || nw >= W)
      continue;
    if (grid[nh][nw] == 0 || reached[nh][nw])
      continue;
    dfs(nh, nw);
  }
}

int main() {
  while (true) {
    cin >> H >> W;
    if (H == 0 && W == 0)
      break;
    for (int h{}; h < H; ++h) {
      for (int w{}; w < W; ++w) {
        reached[h][w] = false;
        cin >> grid[h][w];
      }
    }

    int island{};
    for (int h{}; h < H; ++h) {
      for (int w{}; w < W; ++w) {
        if (grid[h][w] == 0 || reached[h][w])
          continue;
        dfs(h, w);
        ++island;
      }
    }

    cout << island << endl;
  }

  return 0;
}

C++: #include <iostream>
#include <iomanip>
#include <string>
#include <vector>
#include <stack>
#include <queue>
#include <deque>
#include <set>
#include <map>
#include <utility>
#include <tuple>
#include <algorithm>
#include <cstdio>
#include <cmath>
#define rep(i, n) for(int i = 0; i < (n); i++)
using namespace std;
using ll = long long;
template<class T> inline bool chmax(T& a, T b) { if (a < b) { a = b; return true; } return false; }
template<class T> inline bool chmin(T& a, T b) { if (a > b) { a = b; return true; } return false; }

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

int w, h;
vector<vector<int>> grid;

void dfs(int x, int y) {
  grid[x][y] = 0;
  rep(i, 8) {
    if(x+dx[i] >= 0 && x+dx[i] < h && y+dy[i] >= 0 && y+dy[i] < w) {
      if(grid[x+dx[i]][y+dy[i]]) {
        dfs(x+dx[i], y+dy[i]);
      }
    }
  }
}

int main() {
  while(cin >> w >> h && (w || h)) {
    grid = vector<vector<int>>(h, vector<int>(w));
    rep(i, h) rep(j, w)
      cin >> grid[i][j];
    int ans = 0;
    rep(i, h) rep(j, w) {
      if(grid[i][j]) {
        ans++;
        dfs(i, j);
      }
    }
    cout << ans << endl;
  }
  return 0;
}

C++: #include<bits/stdc++.h>
#define rep(i,n) for(int i=0; i<n; i++)
using namespace std;
int table[105][105];
int check(int i, int j){
  for(int a=i-1; a<i+2; a++){
    for(int b=j-1; b<j+2; b++){
      if(table[a][b]==1){
        table[a][b]=0;
        check(a,b);
      }
    }
  }
  return 0;
}
int main(){
  int w,h;
  while(1){
    cin >> w >> h;
    if(w==0 && h==0) break;
    table[w+2][h+2]={};
    rep(i,h) rep(j,w) cin >> table[i+1][j+1];
    rep(i,h+1){
      rep(j,w+1){
        if(table[i][j]==1){
          table[i][j]=2;
          check(i,j);
        }
      }
    }
    int count=0;
    for(int i=1; i<h+1; i++) for(int j=1; j<w+1; j++) if(table[i][j]==2) count++;
    cout << count << endl;
  }
  return 0;
}