Go: package main

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

var (
	readString func() string
	readBytes  func() []byte
	stdout     *bufio.Writer
)

func init() {
	readString, readBytes = newReadString(os.Stdin)
	stdout = bufio.NewWriter(os.Stdout)
}

func newReadString(ior io.Reader) (func() string, func() []byte) {
	r := bufio.NewScanner(ior)
	r.Buffer(make([]byte, 1024), math.MaxInt32)
	r.Split(bufio.ScanWords)

	f1 := func() string {
		if !r.Scan() {
			panic("Scan failed")
		}
		return r.Text()
	}
	f2 := func() []byte {
		return []byte(f1())
	}
	return f1, f2
}

func readInt() int {
	return int(readInt64())
}

func readInt64() int64 {
	i, err := strconv.ParseInt(readString(), 10, 64)
	if err != nil {
		panic(err.Error())
	}
	return i
}

func readFloat64() float64 {
	f, err := strconv.ParseFloat(readString(), 64)
	if err != nil {
		panic(err.Error())
	}
	return f
}

func printf(f string, args ...interface{}) (int, error) {
	return fmt.Fprintf(stdout, f, args...)
}

func println(args ...interface{}) (int, error) {
	return fmt.Fprintln(stdout, args...)
}

func eprintln(args ...interface{}) (int, error) {
	return fmt.Fprintln(os.Stderr, args...)
}

func eprintf(f string, args ...interface{}) (int, error) {
	return fmt.Fprintf(os.Stderr, f, args...)
}

func min(a, b int) int {
	if a < b {
		return a
	}
	return b
}

func min64(a, b int64) int64 {
	if a < b {
		return a
	}
	return b
}

func max(a, b int) int {
	if a < b {
		return b
	}
	return a
}

func max64(a, b int64) int64 {
	if a < b {
		return b
	}
	return a
}

func abs(a int) int {
	if a < 0 {
		return -a
	}
	return a
}

func abs64(a int64) int64 {
	if a < 0 {
		return -a
	}
	return a
}

func gcd(a, b int) int {
	if b == 0 {
		return a
	}
	return gcd(b, a%b)
}

func gcd64(a, b int64) int64 {
	if b == 0 {
		return a
	}
	return gcd64(b, a%b)
}

// readString() string
// readInt() int
// readInt64() int64
// readFloat64() float64

// -----------------------------------------------------------------------------

func main() {
	defer stdout.Flush()

	for {
		ans, ok := solve()
		if !ok {
			break
		}
		println(ans)
	}

}

func solve() (int, bool) {
	w := readInt()
	h := readInt()
	if w == 0 && h == 0 {
		return 0, false
	}

	c := make([][]int, h+2)
	c[0] = make([]int, w+2)
	c[h+1] = make([]int, w+2)
	for i := 1; i-1 < h; i++ {
		c[i] = make([]int, w+2)
		for j := 1; j-1 < w; j++ {
			c[i][j] = readInt()
		}
	}

	var ans int
	for i := 1; i-1 < h; i++ {
		for j := 1; j-1 < w; j++ {
			if 0 < c[i][j] {
				ans++
				dfs(c, i, j)
			}
		}
	}
	return ans, true
}

func dfs(c [][]int, y, x int) {
	c[y][x] = 0
	dx := []int{-1, 0, 1, -1, 1, -1, 0, 1}
	dy := []int{-1, -1, -1, 0, 0, 1, 1, 1}
	for i := range dx {
		nx := x + dx[i]
		ny := y + dy[i]
		if 0 < c[ny][nx] {
			dfs(c, ny, nx)
		}
	}
}

// -----------------------------------------------------------------------------

C++: // A0J 1160 How Many Islands?

#include <iostream>

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

using namespace std;

int map[51][51];
int moumita[51][51];
int w,h;

bool check(int j, int k){
	if (j>=0 && j<h && k>=0 && k<w && map[j][k]==1 && !moumita[j][k]) return true;
	else return false;
}

int dfs(int j, int k){
	moumita[j][k] = true;
	
	if (check(j-1,k-1)) dfs(j-1,k-1);
	if (check(j-1,k)) dfs(j-1,k);
	if (check(j-1,k+1)) dfs(j-1,k+1);
	if (check(j,k-1)) dfs(j,k-1);
	if (check(j,k+1)) dfs(j,k+1);
	if (check(j+1,k-1)) dfs(j+1,k-1);
	if (check(j+1,k)) dfs(j+1,k);
	if (check(j+1,k+1)) dfs(j+1,k+1);
	
	return 1;
}

int main(){
	while (true){
		int ans;
		ans = 0;
		cin >> w >> h;
		
		if (w==0 && h==0) break;
		
		REP(j,h){
			REP(k,w){
				cin >> map[j][k];
				moumita[j][k] = false;
			}
		}
		
		REP(j,h){
			REP(k,w){
				if (check(j,k))	ans += dfs(j,k);
			}
		}
		
		cout << ans << endl;
	}
	return 0;
}

C++: #include<iostream>
#include<vector>
using namespace std;
#define rep(i,n) for(int i = 0; i < n;i++)
int w, h;
int map[50][50];
int dx[8] = { -1,0,1,-1,1,-1,0,1 }, dy[8] = { -1,-1,-1,0,0,1,1,1 };
void dfs(int y, int x) {
	map[y][x] = 0;
	for (int i = 0; i < 8;i++) {
		int nx = x + dx[i];
		int ny = y + dy[i];
		if (nx < 0 || ny >= h || ny < 0 || nx >= w) continue; 
		if(map[ny][nx] == 1) {
			dfs(ny, nx);
		}
	}
}
void dfss() {
	int count = 0;
	rep(i, h) {
		rep(j, w) {
			if (map[i][j] == 1) {
				count++; 
				dfs(i, j);
			}
		}
	}
	cout << count << endl;
}
int main() {
	for (;;) {
		cin >> w >> h;
		if (w == 0 && h == 0)
			break;
		rep(i, h) {
			rep(j, w) {
				cin >> map[i][j];
			}
		}
		dfss();
	}
}

C++: #include <iostream>
#include <algorithm>
#include <vector>
#include <string>

using namespace std;

int m[55][55]={};

void fill(int x,int y){
  m[x][y]=0;
  for(int i=-1;i<2;++i){
    for(int j=-1;j<2;++j){
      if(m[x+i][y+j]==1) fill(x+i,y+j);
    }
  }
}

int main(){
  int w,h;
  while(1){
    cin >> w >> h;
    if(w == 0 && h ==0) break;
    
    for(int i=1;i<=h;++i)
      for(int j=1;j<=w;++j)
	cin >> m[i][j];
    int cnt=0;
    for(int i=1;i<=h;++i)
      for(int j=1;j<=w;++j)
	if(m[i][j]==1){
	  fill(i,j);
	  cnt ++;
	}

    cout << cnt << endl;
  }
  return 0;
}

C++: #include<stdio.h>
#include<iostream>

int field[51][51];

void land(int x,int y){
	field[x][y]=0;

	for(int xx=-1;xx<=1;xx++){
		for(int yy=-1;yy<=1;yy++){
			if(field[x+xx][y+yy]==1) land(x+xx,y+yy);
		}
	}
	return;
}

int main(void){

	int w,h,kazu=0;

	scanf("%d %d",&w,&h);

	if(w==0||h==0) return 0;

	for(int i=0;i<h;i++){
		for(int j=0;j<w;j++){
			scanf("%d",&field[i][j]);
		}
	}

	for(int i=0;i<h;i++){
		for(int j=0;j<w;j++){

			if(field[i][j]==1){
				land(i,j);
				kazu++;
			}

		}
	}

	printf("%d",kazu);
	return 0;
}

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

using namespace std;

int solve(vector<vector<int>> area) {
	// 隣接しているマス(8近傍)を左上から時計回り
	const vector<int> nh = {-1, -1, -1, 0, 1, 1, 1, 0};
	const vector<int> nw = {-1, 0, 1, 1, 1, 0, -1, -1};

	const int h_size = area.size();
	const int w_size = area[0].size();

	int ret = 0;
	vector<vector<int>> visited(h_size, vector<int>(w_size, 0));

	for (int h = 0; h < h_size; ++h) {
		for (int w = 0; w < w_size; ++w) {
			if (area[h][w] == 0) continue;		// 海
			if (visited[h][w] == 1) continue;	// すでに見た陸

			++ret;		// まだ見ていない陸をカウントする
			visited[h][w] = 1;
			queue<pair<int,int>> que;		// pair<int, int> で ２つの int 型を一つの型にまとめられる (pair<int, double> とかもできる)
			que.push(make_pair(h, w));		// push() で que に要素を追加する

			// 幅優先探索はここを指します。 queue を while で回すのが一般的です
			while(que.size()) {
				pair<int, int> now = que.front();	// front() で que に先に入れた要素を取得する
				que.pop();							// pop() で front() で取れる要素を削除する

				for (int i = 0; i < 8; ++i) {
					// 今見ている場所の周囲の座標を計算する
					int next_h = now.first + nh[i];		// .first で pair の1つ目の要素を得る
					int next_w = now.second + nw[i];	// .second で pair の2つ目のの要素を得る

					if (next_h < 0 || next_w < 0 ||
						next_h >= h_size || next_w >= w_size) continue;		// エリアは (0 <= h < h_size), (0 <= w < w_size) の範囲
					if (area[next_h][ next_w] == 0) continue;
					if (visited[next_h][next_w] == 1) continue;

					visited[next_h][next_w] = 1;
					que.push(make_pair(next_h, next_w));
				}
			}
		}
	}

	return ret;
}

int main() {
	int w, h;
	vector<vector<vector<int>>> area;

	// 終了条件は w == 0 && h == 0
	while (cin >> w >> h, !(w == 0 && h == 0)) {
		area.push_back(vector<vector<int>>(h, vector<int>(w)));		// [h][w] になる二次元配列
		for (int i = 0; i < h; ++i) {
			for (int j = 0; j < w; ++j) {
				cin >> area.back()[i][j];		// back() は vector の一番後ろの要素を得られます。
			}
		}
	}

	for (auto check_area : area) {
		cout << solve(check_area) << endl;
	}

	return 0;
}