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<queue>
using namespace std;
int dx[8]={0,1,1,1,0,-1,-1,-1};
int dy[8]={-1,-1,0,1,1,1,0,-1};
int main()
{
	int n,m;
	while(cin>>n>>m,n) 
	{
		int f[m][n];
		for(int i = 0; i < m; ++i)
		{
			for(int j = 0; j < n; ++j)
			{
				cin>>f[i][j];
			}
		}

		int ans=0;
		for(int i = 0; i < m; ++i)
		{
			for(int j = 0; j < n; ++j)
			{
				if(f[i][j]==1)
				{
					ans++;
					queue<int> xs,ys;
					xs.push(j); ys.push(i);
					while(!xs.empty()) 
					{
						int x=xs.front(),y=ys.front();
						xs.pop(); ys.pop();
						f[y][x]=0;
						for(int k = 0; k < 8; ++k)
						{
							int nx=x+dx[k],ny=y+dy[k];
							if(0<=nx&&nx<n&&0<=y&&y<m&&f[ny][nx]==1)
							{
								xs.push(nx); ys.push(ny);
							}
						}
					}
				}
			}
		}
		cout<<ans<<endl;
	}
  return 0;
}

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

using namespace std;
typedef long long int ll;
vector<vector<int>>v;
int w,h;

int dh[8] = {1,1,0,-1,-1,-1,0,1};
int dw[8] = {0,1,1,1,0,-1,-1,-1};

void dfs(int i, int j, int num)
{
    v[i][j] = 0;
    for(int k = 0; k < 8; k++)
    {
        int ni = i + dh[k];
        int nj = j + dw[k];
        if(ni >= h || ni < 0 || nj >= w || nj < 0) continue;
        if(!v[ni][nj]) continue;
        dfs(ni,nj,num);
    }
}

int main()
{
    while(cin >> w >> h)
    {
        if(w == 0 && h == 0) break;
        v.resize(h);
        for(int i = 0; i < h; i++)
        {
            v[i].resize(w);
            for(int j = 0; j < w; j++) cin >> v[i][j];
        }
        int num = 0;
        for(int i = 0; i < h; i++) for(int j = 0; j < w; j++) if(v[i][j]) num++, dfs(i,j,num);
        cout << num << 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;
}

C++: #include <iostream>
#include <vector>
#include <string>
#include <sstream>
#include <algorithm>
#include <map>
#include <set>
#include <cstdio>
#include <cmath>
#define rep(i,l,n) for(lint i=l;i<n;i++)
#define rer(i,l,n) for(lint i=l;i<=n;i++)
#define all(a) a.begin(),a.end()
#define o(a) cout<<a<<endl
#define pb(a) push_back(a)
#define mk(a,b) make_pair(a,b)
#define fi first
#define se second
using namespace std;
typedef long long lint;
typedef vector<int> vi;
typedef vector<lint> vli;
typedef vector<vi> vvi;
typedef pair<int, int> pii;

int w, h;
char s[55][55] = {};
int dy[8] = { 1, 1, 1, 0, 0, -1, -1, -1 };
int dx[8] = { -1, 0, 1, -1, 1, -1, 0, 1 };
int f[55][55] = {};

void dfs(int y, int x){
	if (y < 0 || h <= y || x < 0 || w <= x || f[y][x] || s[y][x] == '0') return;
	f[y][x] = 1;
	rep(i, 0, 8){
		int ny = y + dy[i], nx = x + dx[i];
		dfs(ny, nx);
	}
	return;
}

int main(){
	while (1){
		cin >> w >> h;
		if (w == 0 && h == 0) break;
		rep(i, 0, h){
			rep(j, 0, w){
				cin >> s[i][j];
			}
		}
		rep(i, 0, 55){
			rep(j, 0, 55){
				f[i][j] = 0;
			}
		}
		int c = 0;
		rep(i, 0, h){
			rep(j, 0, w){
				if (s[i][j] == '1' && f[i][j]==0){
					dfs(i, j);
					c++;
				}
			}
		}
		o(c);
	}
}

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

using namespace std;

	ifstream fin;
	ofstream fout;

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

	int gomi = 0;

	int a[52][52] = {};
	int check(int, int);

	int main(){

		fin.open("input.txt");
		fout.open("output.txt");

		//		int w = 0, h = 0;
		int width = 0, height = 0;
		int count = 0;


		while (1){
			for (int i = 0; i < 50; i++)
				for (int j = 0; j < 50; j++)
					a[i + 1][j + 1] = 0;

			cin >> width >> height;
			if (width == 0 && height == 0)break;

			for (int i = 0; i < height; i++){
				for (int j = 0; j < width; j++){
					//初期化
					gomi = a[i + 1][j + 1] = 0;

					cin >> a[i + 1][j + 1];
				}
			}

			for (int i = 0; i < height; i++){
				for (int j = 0; j < width; j++){
					if (a[i + 1][j + 1] == 1){
						check(i + 1, j + 1);
//						a[i + 1][j + 1] = 1;
						count++;
					//	cout << i + 1 << "  " << j + 1 << endl;
					}
				}
			}
//			cout << count << endl;
			fout << count << endl;

			//初期化
			count = 0;
			for (int i = 0; i < height; i++){
				for (int j = 0; j < width; j++){
					a[i + 1][j + 1] = 0;
				}
			}
		}
	}

	int check(int y, int x){
		for (int i = -1; i <= 1; i++)
			for (int j = -1; j <= 1; j++)
				check(y + i, x + j);
	return 0;
	}

/*	int check(int y, int x){
		if (a[y + 1][x] == 1){
			a[y + 1][x] = 0;
			check(y + 1, x);
		}
		//-----------------------------
		if (a[y][x + 1] == 1){
			a[y][x + 1] = 0;
			check(y, x + 1);
		}
		//-----------------------------
		if (a[y - 1][x] == 1){
			a[y - 1][x] = 0;
			check(y - 1, x);
		}
		//-----------------------------
		if (a[y][x - 1] == 1){
			a[y][x - 1] = 0;
			check(y, x - 1);
		}
		//-----------------------------
		if (a[y + 1][x + 1] == 1){
			a[y + 1][x + 1] = 0;
			check(y + 1, x + 1);
		}
		//-----------------------------
		if (a[y + 1][x - 1] == 1){
			a[y + 1][x - 1] = 0;
			check(y + 1, x - 1);
		}
		//-----------------------------
		if (a[y - 1][x + 1] == 1){
			a[y - 1][x + 1] = 0;
			check(y - 1, x + 1);
		}
		//-----------------------------
		if (a[y - 1][x - 1] == 1){
			a[y - 1][x - 1] = 0;
			check(y - 1, x - 1);
		}
		//-----------------------------
		return 0;
	}*/