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>
#include<algorithm>
#include<stack>
#include<queue>
#include<string.h>
#include<string>
#include<functional>
#include<set>
using namespace std;

#define INF 1<<21
#define DIV 1000000007
#define MAX 250000

int table[51][51];
int W, H;

void dfs(int x, int y) {
  table[y][x] = 0;

  for (int dx = -1; dx <= 1; dx++) {
	for (int dy = -1; dy <= 1; dy++) {
	  int nx = x + dx, ny = y + dy;
	  if (0 <= nx&&nx < W && 0 <= ny&&ny < H&&table[ny][nx] == 1)dfs(nx, ny);
	}
  }
  return;
}

int main(){
  while (cin >> W >> H, W || H) {
	int ans = 0;
	memset(table, 0, sizeof(table));
	int t;
	for (int y = 0; y < H; y++) {
	  for (int x = 0; x < W; x++) {
		cin >> table[y][x];
	  }
	}

	for (int y = 0; y < H; y++) {
	  for (int x = 0; x < W; x++) {
		if (table[y][x] == 1) {
		  dfs(x, y);
		  ans++;
		}
	  }
	}
	cout << ans << endl;
  }

}

C++: #include <bits/stdc++.h>

#define REP(i, n) for(int i = 0; i < (int)(n); i++)
#define INF  numeric_limits<int>::max()
#define NINF numeric_limits<int>::min()

using namespace std;
using ll = long long;

int W, H;
bool sea_map[50][50];
bool is_reached[50][50];

void dfs(int s_wi, int s_hi) {
    if(s_wi < 0 || W <= s_wi ||
       s_hi < 0 || H <= s_hi ||
       !sea_map[s_hi][s_wi]  ||
       is_reached[s_hi][s_wi]) {
        return;
    }

    is_reached[s_hi][s_wi] = true;

    dfs(s_wi + 1, s_hi + 1);
    dfs(s_wi + 1, s_hi    );
    dfs(s_wi + 1, s_hi - 1);
    dfs(s_wi    , s_hi - 1);
    dfs(s_wi - 1, s_hi - 1);
    dfs(s_wi - 1, s_hi    );
    dfs(s_wi - 1, s_hi + 1);
    dfs(s_wi    , s_hi + 1);
}

bool search_start_point(int& s_wi, int& s_hi) {
    REP(hi, H) {
        REP(wi, W) {
            if(sea_map[hi][wi] && !is_reached[hi][wi]) {
                s_wi = wi;
                s_hi = hi;
                return true;
            }
        }
    }
    return false;
}

int main() {
    cin.tie(0);
    ios::sync_with_stdio(false);

    while(true) {
        std::cin >> W >> H;
        if(W == 0 && H == 0) {
            break;
        }

        REP(hi, H) {
            REP(wi, W) {
                std::cin >> sea_map[hi][wi];
                is_reached[hi][wi] = false;
            }
        }

        int ans = 0;
        while(true) {
            int s_wi, s_hi;
            if(!search_start_point(s_wi, s_hi)) {
                break;
            }
            else {
                dfs(s_wi, s_hi);
                ans++;
            }
        }

        std::cout << ans << std::endl;
    }

    return 0;
}

C++: #include <bits/stdc++.h>
using namespace std;
#define int long long
#define all(v) (v).begin(), (v).end()
#define rep(i, n) for (int i = 0; i < (int)(n); i++)
#define endl '\n'

const int N = 51;
int w,h;
int board[N][N];

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

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

void solve(){
    rep(i,h){
        rep(j,w) {
            cin >> board[i][j];
        }
    }

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

signed main() {
    while(true){
        cin >> w >> h;
        if(w == 0 && h == 0) return 0;
        solve();
    }
}

C++: #include <bits/stdc++.h>
using namespace std;
using ll = long long;
using P = pair<int, int>;
using PL = pair<ll, ll>;
// Welcome to my source code!

int w, h;
int c[50][50];

void dfs(int cx, int cy) {
    for (int dx = -1; dx <= 1; dx++) {
        for (int dy = -1; dy <= 1; dy++) {
            int nx = cx + dx, ny = cy + dy;
            if (0 <= nx && nx < h && 0 <= ny && ny < w && c[nx][ny] != 0) {
                c[nx][ny] = 0;
                dfs(nx, ny);
            }
        }
    }
}

int main() {
    while (cin >> w >> h && w && h) {
        for (int i = 0; i < h; i++) for (int j = 0; j < w; j++) cin >> c[i][j];
        int ans = 0;
        for (int i = 0; i < h; i++) {
            for (int j = 0; j < w; j++) {
                if (c[i][j] == 0) continue;
                dfs(i, j);
                ans++;
            }
        }
        cout << ans << endl;
    }
}