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++: #include <iostream>

using namespace std;

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

// 0: 海, 1: 陸
int board[50][50] = {{0}};

void DFS(int i, int j, int w, int h) {
  if (board[i][j] == 0) return;
  board[i][j] = 0;  // mark as visited

  for (int d = 0; d < 8; ++d) {
    const int ny = i + dy[d];
    const int nx = j + dx[d];
    if (ny >= 0 && ny < h && nx >= 0 && nx < w) {
      DFS(ny, nx, w, h);
    }
  }
}

int main() {
  int w, h;

  for (;;) {
    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 (board[i][j] == 1) {
          DFS(i, j, w, h);
          ++ans;
        }
      }
    }
    cout << ans << endl;
  }
  return 0;
}

C++: #include <cstdio>
#define f(n)for(int i=0;i<n;i++)
#define s(w)scanf("%d",&w)
int a,n,m,d[99][99];
int u[16]={-1,-1,-1,0,0,1,1,1,-1,0,1,-1,1,-1,0,1};
void k(int x,int y){
	if(d[x+1][y+1]){f(8)k(x+u[i],y+u[i+8]);d[x+1][y+1]=0;}
}
int main(){
	a=0;
	s(n);s(m);
	f(9999)d[i/99][i%99]=0;
	f(n*m)s(d[i/n][i%n]);
	f(n*m)if(d[i/n][i%n])a++,k(i/n,i%n);
	printf("%d\n",a);
	main();
}

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

int w,h;
int field[51][51];

// »Ýn(x,y)
void dfs(int x, int y){
	// ¡¢éÆ±ëð-1Éu«·¦é
	field[y][x] = -1;

	// Ú®·é8ûüð[v
	for(int dx = -1 ; dx <= 1 ; dx++ ){
		for(int dy = -1 ; dy <= 1 ; dy++ ){
			// xûüÉdx, yûüÉdy, Ú®µ½êð(mx,my)Æ·é
			int mx = x + dx;
			int my = y + dy;
			// ÍÍOÉo½Æ«ÍÌÖ
			if( mx < 0 || my < 0 || mx >= w || my >= h ) continue;
			// ¤n¾Á½çÄAÄÑoµ
			if( field[my][mx] == 1 ){
				dfs( mx , my );
			}
		}
	}
}

int main(){
	while( cin >> w >> h , w||h ){
		for(int y=0 ; y < h ; y++ ){
			for(int x=0 ; x < w ; x++ ){
				cin >> field[y][x];
			}
		}

		int ans = 0;
		for(int y=0 ; y < h ; y++ ){
			for(int x=0 ; x < w ; x++ ){
				if( field[y][x] == 1 ){
					dfs( x , y );
					ans++;
				}
			}
		}
		cout << ans << endl;
	}
}

C++: #include <iostream>
using namespace std;
int sea[51][51], w, h;
const int di[8] = {-1, 1, 0, 0, 1, -1, -1, 1};
const int dj[8] = {0, 0, -1, 1, 1, 1, -1, -1};

void dfs(int i, int j) {
  sea[i][j] = 0;
  for (int z = 0; z < 8; z++) {
    int ti = i + di[z], tj = j+ dj[z];
    if (ti < 0 || tj < 0 || ti >= h || tj >=w) continue;
    if (sea[ti][tj]) dfs(ti, tj);
  }
}

void solve() {
  for(int i=0;i<h;i++)
  for(int j=0;j<w;j++)
  cin>>sea[i][j];
  
  int res = 0;
  for(int i=0;i<h;i++)
  for(int j=0;j<w;j++)
  if (sea[i][j] == 1) {
    dfs(i, j); res++;
  }
  
  cout<<res<<endl;
}

int main (int argc, char const* argv[]) {
  while (cin>>w>>h, w||h) {
    solve();
  }
  return 0;
}

C++: #include <cstdio>
#define f(i,n)for(int i=0;i<n;i++)
#define s (scanf("%d",&e),e)
int main(){int a=0,w,n,m,b,e,d[99];f(i,99)d[i]=0;n=s;m=s;if(n==0)return 0;f(i,m){w=b=0;f(j,n){s;if(e)f(k,3){e=d[j+k];if(e&&e-w){if(w){f(l,n+2)d[l]=d[l]==e?w:d[l];a--;}else w=e;}if(w==0)a++,w=i*n+j+1;}d[j]=b;b=w;}d[n]=b;}printf("%d\n",a);main();}

C++: #include <bits/stdc++.h>
#include <math.h>
#define ALL(a)    a.begin(), a.end()
using namespace std;

vector<vector<int>> V;
vector<int> dx{0, 0, 1, 1, 1, -1, -1, -1};
vector<int> dy{1, -1, 0, 1, -1, 0, 1, -1};
vector<vector<bool>> seen;
int W = 1, H = 1, Ans = 0;


void dfs(int y, int x){
seen[y][x] = 1;

for(int i = 0; i < 8; i++){
int ny = y + dy[i], nx = x + dx[i];

if(ny < 0 || ny >= H || nx < 0 || nx >= W)continue;

if(V[ny][nx] == 0)continue;

if(seen[ny][nx])continue;

dfs(ny, nx);
}



}


int main() {


while(1){

cin >> W >> H;
if(H == 0 && W == 0)break;

V.resize(H);
seen.resize(H);

for(int i = 0; i < H; i++){
V[i].resize(W);seen[i].resize(W);}

for(int i = 0; i < H; i++){
for(int j = 0; j < W; j++){
cin >> V[i][j];
//cout << V.at(i).at(j);
seen[i][j] = 0;
}}

for(int i = 0; i < H; i++){
for(int j = 0; j < W; j++){
if(V[i][j] == 1 && seen[i][j] == 0){dfs(i, j);Ans++;}
}}
cout << Ans << endl;
Ans = 0;
}


}