Go: package main

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

const (
	initialBufSize = 100000
	maxBufSize     = 1000000
)

var sc *bufio.Scanner

func initScanner(r io.Reader) *bufio.Scanner {
	buf := make([]byte, initialBufSize)

	sc := bufio.NewScanner(r)
	sc.Buffer(buf, maxBufSize)
	sc.Split(bufio.ScanWords) // bufio.ScanLines
	return sc
}

func main() {
	sc = initScanner(os.Stdin)
	resolve()
}

func resolve() {
	results := []int{}
	for true {
		w, h := scanInt(sc), scanInt(sc)
		if w == 0 && h == 0 {
			break
		}
		grid := make([][]int, h)
		for i := 0; i < h; i++ {
			grid[i] = make([]int, w)
			for j := 0; j < w; j++ {
				grid[i][j] = scanInt(sc)
			}
		}
		num := 0
		for i := range loop0(h) {
			for j := range loop0(w) {
				start := pos{y: i, x: j}
				if grid[start.y][start.x] == 0 {
					continue
				}
				num++
				_dfs(grid, w, h, start)
			}
		}
		results = append(results, num)
	}
	for _, v := range results {
		fmt.Println(v)
	}
	return
}

func _dfs(grid [][]int, w, h int, start pos) {
	grid[start.y][start.x] = 0
	for _, df := range []pos{
		pos{1, 0}, pos{-1, 0},
		pos{0, -1}, pos{0, 1},
		pos{1, 1}, pos{-1, -1},
		pos{-1, 1}, pos{1, -1},
	} {
		np := addPos(start, df)
		if np.y < 0 || h <= np.y || np.x < 0 || w <= np.x {
			continue
		}
		if grid[np.y][np.x] == 0 {
			continue
		}
		_dfs(grid, w, h, np)
	}
}

type pos struct{ x, y int }

func addPos(p, d pos) pos {
	return pos{
		x: p.x + d.x,
		y: p.y + d.y,
	}
}

func loop0(n int) chan int {
	return loop(0, n-1, 1)
}

func loop1(n int) chan int {
	return loop(1, n, 1)
}

func loop(s, e, d int) chan int {
	ch := make(chan int)
	go func() {
		for i := s; i <= e; i += d {
			ch <- i
		}
		close(ch)
	}()
	return ch
}

// snip-scan-funcs
func scanInt(sc *bufio.Scanner) int {
	sc.Scan()
	i, _ := strconv.Atoi(sc.Text())
	return int(i)
}
func scanString(sc *bufio.Scanner) string {
	sc.Scan()
	return sc.Text()
}

C++: #include <iostream>
#include <string>
#include <algorithm>
using namespace std;
#define FOR(i,a,b) for(int i=a;i<b;i++)
#define REP(i,b) FOR(i,0,b)
#define INF 1e9

int w,h,c[50][50],i_num;
int dx[] = {1,0,-1}, dy[] = {1,0,-1};

void search(int x, int y){
  if(c[y][x] == 1 && x >=0 && x < w && y >=0 && y < h){
    c[y][x] = i_num;
    REP(i,3) REP(j,3){
      if(i != 0 || j != 0 || x-1 >= 0 || x+1 < w || y-1 >= 0 || y+1 < h)
      search(x+dx[i],y+dy[j]);
    }
  }
}

void solve(int X, int Y){
  i_num = 2;
  REP(i,20) REP(j,20) c[i][j] = 0;
  //0:ocean, 1:land, 2~:island_number

  REP(i,Y){
    REP(j,X){
      cin>>c[i][j];
    }
  }

  REP(i,Y){
    REP(j,X){
      if(c[i][j] == 1){
        search(j,i);
        i_num++;
      }
    }
  }

  cout<<i_num-2<<endl;
  /*REP(i,20){
     REP(j,20) cout<<c[i][j];
     cout<<endl;
  }*/
}

int main(){

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

  return 0;
}

C++: #include <iostream>
#include <fstream>
#include <cstdlib>
#include <math.h>
#include <utility>
#include <algorithm>
#include <functional>
#include <vector>
#include <numeric>
#include <bits/stdc++.h>

using namespace std;
using ll = long long;
using vi = vector<int>;
using vll = vector<ll>;
using vb = vector<bool>;
using Graph = vector<vector<int>>;

#define FOR(i, a, b) for (int i = a; i < (b); i++)
#define REP(i,n) for(int i=0;i<n;i++)

vb seen;

void dfs(const Graph &G, int v) {
    seen[v] = true;
    for(auto next_v : G[v]) {
        if (seen[next_v]) continue;
        dfs(G, next_v);
    }
}

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

    int w, h;
    while(cin >> w >> h, w*h) {
        int map[h+1][w+1];
        REP(j, w+1) {
            map[0][j] = 0;
        }
        REP(i, h) {
            map[i+1][0] = 0;
            REP(j, w) {
                cin >> map[i+1][j+1];
            }
        }
        
        Graph G(w*h);
        FOR(i, 1, h+1) {
            FOR(j, 1, w+1) {
                if (map[i][j] == 1) {
                    int p1 = (i-1)*w+(j-1);
                    int p2;
                    if (map[i][j-1] == 1) {
                        p2 = (i-1)*w+(j-2);
                        G[p1].push_back(p2);
                        G[p2].push_back(p1);
                    }
                    if (map[i-1][j-1] == 1) {
                        p2 = (i-2)*w+(j-2);
                        G[p1].push_back(p2);
                        G[p2].push_back(p1);
                    }
                    if (map[i-1][j] == 1) {
                        p2 = (i-2)*w+(j-1);
                        G[p1].push_back(p2);
                        G[p2].push_back(p1);
                    }
                    if (map[i-1][j+1] == 1) {
                        p2 = (i-2)*w+j;
                        G[p1].push_back(p2);
                        G[p2].push_back(p1);
                    }
                }
            }
        }

        seen.assign(w*h, false);
        int cnt = 0;
        REP(i, w*h) {
            if (seen[i]) continue;
            if (map[i/w+1][i%w+1] == 0) {
                seen[i] = true;
                continue;
            }
            cnt++;
            dfs(G, i);
        }
        cout << cnt << "\n";
    }
}

C++: #include<iostream>
#include<queue>

typedef std::pair<int, int> P;
int vx[] = { 0, 1, 0, -1, 1, 1, -1, -1 }, vy[] = { 1, 0, -1, 0, 1, -1, 1, -1 };
int w, h;
int map[51][51];

void bfs(int x, int y){

	map[y][x] = 0;

	std::queue<P>que;
	que.push(P(x, y));

	while (!que.empty()){

		P p = que.front();
		que.pop();

		for (int i = 0; i < 8; i++){
			int nx = p.first + vx[i], ny = p.second + vy[i];

			if (0 <= nx&&nx < w && 0 <= ny&&ny < h&&map[ny][nx] != 0){
				map[ny][nx] = 0;
				que.push(P(nx, ny));
			}
		}
	}

	return;
}

int main(){

	while (std::cin >> w >> h, w || h){

		int cnt = 0;

		memset(map, 0, sizeof(map));

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

		for (int i = 0; i < h; i++){
			for (int j = 0; j < w; j++){
				if (map[i][j] != 0){
					bfs(j, i);
					cnt++;
				}
			}
		}

		std::cout << cnt << std::endl;
	}

	return 0;
}

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

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

//動的計画法
void dfs(int h, int w){
    field[h][w] = 0;

    //八方向を１つずつ探索
    for(int dh = -1; dh <= 1; dh++){
        for(int dw = -1; dw <= 1; dw++){
            int nh = h + dh;
            int nw = w + dw;

            //場外アウトまたは０ならば、それ以降（再帰的に探索する部分）は実行せずループの最初に戻る
            if(nh < 0 || nh > H || nw < 0 || nw > W || field[nh][nw] == 0) continue;

            //再帰的に探索（探索しているマスを基準にしてその八方向にあるマスを探索していく）
            dfs(nh, nw);
        }
    }
}

int main(void){
    while(true){
        cin >> W >> H;
        if(W + H == 0) break;

        for(int i = 0; i < H; i++){
            for(int j = 0; j < W; j++){
                cin >> field[i][j];
            }
        }

        //探索開始
        int c = 0; //島の個数
        for(int i = 0; i < H; i++){
            for(int j = 0; j < W; j++){
                if(field[i][j] == 0) continue; //島の一部でないならば、それ以降スルー
                dfs(i, j); //そのマスが陸であるならば、その八方向を動的計画法で探索していく
                c++;
            }
        }
        cout << c << endl;
    }
}