Go: package main

import (
	"bufio"
	"fmt"
	"os"
	"strconv"
)

func getScanner(fp *os.File) *bufio.Scanner {
	scanner := bufio.NewScanner(fp)
	scanner.Split(bufio.ScanWords)
	scanner.Buffer(make([]byte, 1000005), 1000005)
	return scanner
}

func getNextString(scanner *bufio.Scanner) string {
	scanner.Scan()
	return scanner.Text()
}

func getNextInt(scanner *bufio.Scanner) int {
	i, _ := strconv.Atoi(getNextString(scanner))
	return i
}

func getNextInt64(scanner *bufio.Scanner) int64 {
	i, _ := strconv.ParseInt(getNextString(scanner), 10, 64)
	return i
}

func getNextUint64(scanner *bufio.Scanner) uint64 {
	i, _ := strconv.ParseUint(getNextString(scanner), 10, 64)
	return i
}

func getNextFloat64(scanner *bufio.Scanner) float64 {
	i, _ := strconv.ParseFloat(getNextString(scanner), 64)
	return i
}

func main() {
	fp := os.Stdin
	wfp := os.Stdout
	cnt := 0

	if os.Getenv("MASPY") == "ますピ" {
		fp, _ = os.Open(os.Getenv("BEET_THE_HARMONY_OF_PERFECT"))
		cnt = 0
	}
	if os.Getenv("MASPYPY") == "ますピッピ" {
		wfp, _ = os.Create(os.Getenv("NGTKANA_IS_GENIUS10"))
	}

	scanner := getScanner(fp)
	writer := bufio.NewWriter(wfp)
	for solve(scanner, writer) {

	}
	for i := 0; i < cnt; i++ {
		fmt.Fprintln(writer, "-----------------------------------")
		solve(scanner, writer)
	}
	writer.Flush()
}

func solve(scanner *bufio.Scanner, writer *bufio.Writer) bool {
	sol := solver{}
	sol.w = getNextInt(scanner)
	sol.h = getNextInt(scanner)
	if sol.w == 0 && sol.h == 0 {
		return false
	}
	sol.land = makeInts(sol.h, sol.w)
	for y := 0; y < sol.h; y++ {
		for x := 0; x < sol.w; x++ {
			sol.land[y][x] = getNextInt(scanner)
		}
	}
	ans := 0
	for y := 0; y < sol.h; y++ {
		for x := 0; x < sol.w; x++ {
			if sol.land[y][x] == 0 {
				continue
			}
			ans++
			sol.dfs(y, x)
		}
	}
	fmt.Fprintln(writer, ans)
	return true
}

type solver struct {
	w, h int
	land [][]int
}

func (sol *solver) dfs(y, x int) {
	if y < 0 {
		return
	}
	if y >= sol.h {
		return
	}
	if x < 0 {
		return
	}
	if x >= sol.w {
		return
	}
	if sol.land[y][x] == 0 {
		return
	}
	sol.land[y][x] = 0
	dy := [8]int{-1, -1, -1, 0, 0, 1, 1, 1}
	dx := [8]int{-1, 0, 1, -1, 1, -1, 0, 1}
	for i := 0; i < 8; i++ {
		sol.dfs(y+dy[i], x+dx[i])
	}
}
func makeInts(h, w int) [][]int {
	index := make([][]int, h, h)
	data := make([]int, h*w, h*w)
	for i := 0; i < h; i++ {
		index[i] = data[i*w : (i+1)*w]
	}
	return index
}

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

int field[50+2][50+2];
int dx[8]={0,1,1,1,0,-1,-1,-1};
int dy[8]={-1,-1,0,1,1,1,0,-1};

int rec(int x, int y)
{
  int i;

  if(field[x][y]!=1) return 0;
  field[x][y] = 0;
  for(i=0;i<8;i++) rec(x+dx[i],y+dy[i]);
  return 1;
}

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

    n = 0;
    for(j=1;j<=h;j++) for(i=1;i<=w;i++) n += rec(i,j);

    cout<<n<<endl;
  }
}

C++: #include <bits/stdc++.h>
using namespace std;
#define int long long
#define ll long long
#define fi first
#define se second
#define pb push_back
#define rep(i, s, n) for (int i = s; i < n; i++)
#define rrep(i, s, n) for (int i = (n)-1; i >= (s); i--)
#define all(a) a.begin(),a.end()
typedef pair<int,int>pint;
typedef vector<int>vint;
typedef vector<pint>vpint;
const long long MOD = 1000000007, INF = 1e17;
 
#define endl '\n'
#define IOS() ios_base::sync_with_stdio(0);cin.tie(0)
 
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;}

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

int a[55][55],H,W;

void dfs(int i,int j,vector<vector<int>>&used){
	used[i][j] = true;
	rep(k,0,8){
		int ni = i + dy[k],nj = j + dx[k];
		if(ni<0||nj<0||H<=ni||W<=nj)continue;
		if(a[ni][nj]==0)continue;
		if(used[ni][nj])continue;
		dfs(ni,nj,used);
	}
}

signed main()
{
	//IOS();
	vector<int>ans;
	while(1){
		cin>>W>>H;
		if(W==0&&H==0)break;
		for(int i = 0;i < H;i++){
			for(int j = 0;j < W;j++){
				cin>>a[i][j];
			}
		}
		vector<vector<int>>used(H,vector<int>(W,0));
		int cnt = 0;
		for(int i = 0;i < H;i++){
			for(int j = 0;j < W;j++){
				if(used[i][j])continue;
				if(a[i][j]==0)continue;
				dfs(i,j,used);
				cnt++;
			}
		}
		ans.push_back(cnt);
	}
	for(auto e:ans){
		cout<<e<<endl;
	}
}

C++: 
/*
Problem

Category
DFS

URL:
http://judge.u-aizu.ac.jp/onlinejudge/description.jsp?id=1160&lang=jp

考察:


Date: 2020/04/14
*/

#include <bits/stdc++.h>
using namespace std;
using ll = long long;
#define int long long

/* ==== declarations ====== */
// functions
void solve();
void getInput();

// global variables
int w, h;
vector<vector<bool>> C;
vector<vector<int>> color;
int currentColor = 1;


/* ======================== */

signed main(){
    while(true){
        getInput();
        solve();
    }
    return 0;
}

void getInput() {
    cin>>w>>h;
    if(!w and !h) {exit(0);}
    C = vector<vector<bool>>(h, vector<bool>(w, false));
    color = vector<vector<int>>(h, vector<int>(w, 0));
    for(int i=0; i<h; i++) {
        for(int j=0; j<w; ++j) {
            bool c;
            cin>>c;
            C[i][j] = c;
        }
    }
}

void dfs(int i, int j) {
    if (i < 0 or j < 0 or i >= h or j >= w) return;
    if (!C[i][j]) return;
    if (color[i][j]) return;
    color[i][j] = currentColor;
    
    for(int a: {i-1, i, i+1})
        for(int b: {j-1, j, j+1})
            dfs(a, b);
    
}


void solve() {
    for(int i=0; i<h; i++) {
        for(int j=0; j<w; ++j) {
            dfs(i, j);
            currentColor++;
        }
    }
    set<int> colorSet {0};
    for(int i=0; i<h; i++) {
        for(int j=0; j<w; ++j) {
            colorSet.insert(color[i][j]);
        }
    }
    cout<<colorSet.size()-1<<endl;
}

C++: #include <iostream>

using namespace std;

// 0: 海, 1: 陸
int board[50][50] = {{0}};

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

bool DFS(int i, int j, int w, int h) {
  if (i < 0 || i >= h || j < 0 || j >= w) return false;
  if (!board[i][j]) return false;

  board[i][j] = 0;
  for (int d = 0; d < 8; ++d)
    DFS(i + dy[d], j + dx[d], w, h);

  return true;
}

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

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

    int ans = 0;
    for (int i = 0; i < h; ++i) {
      for (int j = 0; j < w; ++j) {
        if (DFS(i, j, w, h)) ++ans;
      }
    }
    cout << ans << endl;
  }
  return 0;
}