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 <stdio.h>
#include <string>
#include <sstream>
#include <vector>
#include <stdlib.h>
#include <math.h>

using namespace std;

void chkmap(int w, int h, int x, int y, int map[50][50]) {
	if(map[x][y] == 0) return;
	map[x][y] = 0;
	if(x + 1 < w) chkmap(w,h,x+1,y,map);
	if(x > 0)	  chkmap(w,h,x-1,y,map);
	if(y + 1 < h) chkmap(w,h,x,y+1,map);
	if(y > 0)	  chkmap(w,h,x,y-1,map);
	if(x + 1 < w && y + 1 < h) chkmap(w,h,x+1,y+1,map);
	if(x + 1 < w && y > 0)	   chkmap(w,h,x+1,y-1,map);
	if(x > 0 && y + 1 < h)     chkmap(w,h,x-1,y+1,map);
	if(x > 0 && y > 0)		   chkmap(w,h,x-1,y-1,map);
}

int main(void){
	//FILE* fp_in = freopen("data.txt", "r", stdin);

	while(1) {

		int w, h, ans = 0;
		int map[50][50];
		cin >> w >> h;

		if( w == 0 && h == 0 ) break;	//終了処理

		//マップ読み取り
		for(int i = 0; i < h; i++) {
			for(int j = 0; j < w; j ++) {
				int tmp;
				cin >> tmp;
				map[j][i] = tmp;
			}
		}


		for(int i = 0; i < h; i++) {
			for(int j = 0; j < w; j++) {
				if(map[j][i] == 1) ans ++;
				chkmap(w,h,j,i,map);
			}
		}

		cout << ans << endl;
	}

	
	//while(1){}
	return 0;
}

C++: #include <vector>
#include <list>
#include <map>
#include <set>
#include <deque>
#include <stack>
#include <bitset>
#include <algorithm>
#include <functional>
#include <numeric>
#include <utility>
#include <sstream>
#include <iostream>
#include <iomanip>
#include <cstdio>
#include <cmath>
#include <cstdlib>
#include <cctype>
#include <string>
#include <cstring>
#include <ctime>
#include <fstream>
#include <queue>

#pragma warning( disable: 4996 )

using namespace std;

typedef long long ll;

#define INF 100000000
#define EPS 1e-9

#define MAX_W 50
#define MAX_H 50

int w, h;
int maze[MAX_H][MAX_W];

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

vector<int> ans_list;

void solve(int x, int y){

	maze[y][x] = 0;

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

}

int main(){

	while (true){
	
		cin >> w >> h;
		if (w == 0 && h == 0)
			break;

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

		int ans = 0;

		for (int i = 0; i < h; i++){
			for (int j = 0; j < w; j++){
				if (maze[i][j] == 1){
					solve(j, i);
					ans++;
				}
			}
		}
	
		ans_list.push_back(ans);

	}

	for (int i = 0; i < ans_list.size(); i++){
		cout << ans_list[i] << endl;
	}

	return 0;

}

C++: #include <bits/stdc++.h>
#if 1
#define int long long
#define MAX LLONG_MAX
#define MIN LLONG_MIN
#define stoi stoll
#else
#define MAX INT_MAX
#define MIN INT_MIN
#endif
#define ALL(obj) (obj).begin(), (obj).end()
#define fi first
#define se second
const int NIL = -1;
const int MOD = 1000000007;
#define REP(i,n) for (int i=0; i < (n); ++i)
#define FOR(i,a,b) for (int i=(a); i < (b); ++i)
using namespace std;

int get_gcd(int a, int b){while(1){if (a < b) swap(a, b); if (b == 0) return a; a %= b;}}
int get_lcm(int a, int b){return a * b / get_gcd(a, b);}
//int dx[4]={1,0,-1,0};
//int dy[4]={0,1,0,-1};

//-------------------------テンプレここまで-------------------------//

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

int w, h;
// 0なら海、1なら陸（未探索)
bool field[50][50];
// 探索済かどうか
bool seen[50][50];

void dfs(int i, int j){
  seen[i][j] = true;

  REP(k,8) {
    int ni = i+dx[k];
    int nj = j+dy[k];
    if (ni<0 || ni>=h || nj<0 || nj>=w) continue;
    if (!field[ni][nj] || seen[ni][nj]) continue;
    dfs(ni, nj);
  }
}

signed main () {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);

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

    REP(i,h) {
      REP(j,w) {
        cin >> field[i][j];
        seen[i][j] = false;
      }
    }

    int ans = 0;
    REP(i,h) {
      REP(j,w) {
        if (!field[i][j] || seen[i][j]) continue;
        dfs(i,j);
        ans++;
        //cout << i << " " << j << endl;
      }
    }

    cout << ans << endl;
  }

  return 0;
}

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

#define rep(i,n) for (int i = 0; i < (n); ++i)

int w, h, c[55][55], ans;
bool is_search[55][55];
int vx[8] = {0, -1, -1, -1, 0, 1, 1, 1},
    vy[8] = {-1, -1, 0, 1, 1, 1, 0, -1};

struct land_or_sea {
    int x, y, num;
    land_or_sea(int x, int y, int num) : x(x), y(y), num(num) {}
};

void bfs(int x, int y) {
    queue<pair<int, int> > que;
    que.push(make_pair(x, y));
    is_search[x][y] = true;

    while (!que.empty()) {
        pair<int, int> p = que.front(); que.pop();
        rep(i,8) {
            int nx, ny;
            nx = p.first + vx[i], ny = p.second + vy[i];
            if (0 <= nx && nx < w && 0 <= ny && ny < h &&
                c[nx][ny] == 1 && is_search[nx][ny] == false) {
                   que.push(make_pair(nx, ny));
                   is_search[nx][ny] = true;
            }
        }
    }

}

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

    while (cin >> w >> h, w | h) {
        memset(c, 0, sizeof(c));
        memset(is_search, false, sizeof(is_search));
        ans = 0;
        rep(i,h) {
            rep(j,w) {
                cin >> c[i][j];
            }
        }

        rep(i,w) {
            rep(j,h) {
                if (is_search[i][j] == false && c[i][j] == 1) {
                    bfs(i,j);
                    ans++;
                }
            }
        }

        cout << ans << endl;
    }

    return 0;
}