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 <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;
}


}

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

using namespace std;

long long inf=1000000007;
long long h,w;

void dfs(long long y,long long x,vector<vector<bool>> &seen,vector<vector<long long>> grid){
	vector<long long> dy={1,1,1,0,0,-1,-1,-1};
	vector<long long> dx={1,0,-1,1,-1,1,0,-1};

	for(long long i=0;i<8;i++){
		long long py=y+dy.at(i);
		long long px=x+dx.at(i);

		if(py<0 || py>=h || px<0 || px>=w || grid.at(py).at(px)==0 || seen.at(py).at(px)) continue;

		seen.at(py).at(px)=true;
		dfs(py,px,seen,grid);
	}
}

int main(){
	cin>>w>>h;

	while(h!=0 || w!=0){
		vector<vector<long long>> grid(h,vector<long long>(w));
		for(long long i=0;i<h;i++){
			for(long long j=0;j<w;j++){
				cin>>grid.at(i).at(j);
			}
		}

		vector<vector<bool>> seen(h,vector<bool>(w,false));
		long long ans=0;

		for(long long i=0;i<h;i++){
			for(long long j=0;j<w;j++){
				if(grid.at(i).at(j)==0 || seen.at(i).at(j)) continue;
				ans++;
				seen.at(i).at(j)=true;
				dfs(i,j,seen,grid);
			}
		}

		cout<<ans<<endl;
		cin>>w>>h;
	}

	return 0;
}

C++: #include <iostream>
#include <algorithm>
#include <vector>
#include <stack>
#include <queue>
#include <map>
#include <cmath>
#include <string>
#include <sstream>
#include <iomanip>
#include <complex>
using namespace std;
 
#define ll long long
#define vvi vector< vector<int> >
#define All(X) X.begin(),X.end()
#define FOR(i,a,b) for(int i=(int)(a);i<(int)(b);i++)
#define REP(i,n) for(int i=0;i<(int)(n);i++)
#define pb push_back 
ll gcd(ll a,ll b){return b?gcd(b,a%b):a;}
ll lcm(ll a,ll b){return a/gcd(a,b)*b;}

int w,h;
int c[100][100];
bool notsearched[100][100];
int dx[8]={0,1,0,-1,1,1,-1,-1};
int dy[8]={1,0,-1,0,1,-1,1,-1};
int ans;

bool inrange(int x, int y){
  if(x<0||y<0||x>=h||y>=w){ 
	//cout << "false" <<endl;
	return false;
  }else{ 
	//cout << "true" << endl;
	return true;
  }
}

void search(int x, int y){

  if((notsearched[x][y])&&(c[x][y] == 1)&&(inrange(x,y))){
	notsearched[x][y]=false;

	REP(i,8) search(x+dx[i],y+dy[i]);
  }
}


int main(){
  while(1){

	REP(i,100){
	  REP(j,100){
		notsearched[i][j]=true;
	  }
	}

	cin >> w >> h;
	if (w==0&&h==0) break;
	ans=0;
	REP(i,h){
	  REP(j,w){
		cin >> c[i][j];

	  }
	}

	REP(i,h){
	  REP(j,w){
		if((notsearched[i][j])&&(c[i][j] == 1)&&(inrange(i,j))) ans++; 
		search(i,j);
	  }
	}
	cout << ans << endl;
  }
 
}