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>

int field[52][52];

void clear();

bool walk(int x, int y, int& a);

int main(){

	int w,h,ans;
	
	const int dif = 2;
	
	while(true){

		std::cin >> w >> h;
		if(w==0 && h==0)
			return 0;

		clear();
		for(int i=1; i<=h; i++)
			for(int j=1; j<=w; j++)
				std::cin >> field[i][j];

		ans = 2;
		for(int i=1; i<=h; i++)
			for(int j=1; j<=w; j++)
				if(walk(i, j, ans))
					ans++;

		std::cout << ans-dif << std::endl;
	}

	return 0;
}

void clear(){
	for(int i=0; i<52; i++)
		for(int j=0; j<52; j++)
			field[i][j] = 0;
}

bool walk(int x, int y, int& a){
	if(field[y][x] != 1)
		return false;

	field[y][x] = a;
	
	for(int i=-1; i<=1; i++)
		for(int j=-1; j<=1; j++)
			walk(x+i, y+j, a);

	return true;
}

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

using namespace std;

int dx[8] = { 1, 1, 0, -1, -1, -1, 0, 1 };
int dy[8] = { 0, -1, -1, -1, 0, 1, 1, 1 };
int w, h, c[50][50];
void dfs(int c[50][50], int x, int y);

int main() {
	while (true) {
		cin >> w >> h;
		if (w == 0 && h == 0)	break;
		int counter = 0;
		for (int i = 0; i < h; i++) {
			for (int j = 0; j < w; j++) {
				cin >> c[j][i];
			}
		}

		for (int i = 0; i < w; i++) {
			for (int j = 0; j < h; j++) {
				if (c[i][j] == 1) {
					dfs(c, i, j);
					counter++;
				}
			}
		}
		cout << counter << endl;

	}
	return 0;
}

void dfs(int c[50][50], int x, int y) {	//(x, y) = 1ということを発見済み
	c[x][y] = 0;
	for (int i = 0; i < 8; i++) {
		if (0 <= x + dx[i] && x + dx[i] < w && 0 <= y + dy[i] && y + dy[i] < h) {
			if (c[x + dx[i]][y + dy[i]] == 1) {
				dfs(c, x + dx[i], y + dy[i]);
			}
		}
	}
}

C++: #include <iostream>

#define W_MAX 55
#define H_MAX 55

int f[H_MAX][W_MAX];

void dfs(int y,int x);


using namespace std;

int main(void)
{
	int w;
	int h;
	int i1,i2;
	int count;
	int a;

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

		//cout << "a";

		for(i1=0;i1<H_MAX;i1++){
			for(i2=0;i2<W_MAX;i1++){
				f[i1][i2]=0;

			}
		}

		for(i1=1;i1<=h;i1++){
			for(i2=1;i2<=w;i2++){
				cin >> a;
				//f[i1][i2] = a;
			}
		}


		count=0;
		for(i1=1;i1<=h;i1++){
			for(i2=1;i2<=w;i2++){
				if (f[i1][i2] == 1){
					count++;
					dfs(i1,i2);
				}
			}
		}

		cout << count << endl;

	}
	return 0;
}


void dfs(int y,int x)
{
	int i1,i2;

	f[y][x]=0;
	for(i1=-1;i1<2;i1++){
		for(i2=-1;i2<2;i2++){

			if(f[y+i1][x+i2]==1) dfs(y+i1,x+i2);
		}
	}

	return;
}

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

int w, h, x, y, ans;
int mp[52][52];
int visited[52][52];
int dx[8] = {0, 0, 1, 1, 1, -1, -1, -1};
int dy[8] = {-1, 1, 1, -1, 0, 1, -1, 0};


void bfs(int x, int y){
  int i;
  visited[y][x] = 1;
  for(i = 0; i < 8; i++){
    if(visited[y + dy[i]][x + dx[i]] == 0 && mp[y + dy[i]][x + dx[i]] == 1){
      bfs(x + dx[i], y + dy[i]);
    }
  }
}

int main(void){
  while(1){
    cin >> w >> h;
    if(w == 0) break;

    for(y = 0; y < h + 2; y++){
      if(y == 0 || y == h + 1){
        for(x = 0; x < w + 2; x++) mp[y][x] = 0;
      }else{
        for(x = 0; x < w + 2; x++){
          if(x == 0 || x == w + 1){
            mp[y][x] = 0;
          }else{
            cin >> mp[y][x];
          }
        }
      }
    }
    for(y = 0; y < h + 2; y++){
      for(x = 0; x < w + 2; x++){
        visited[y][x] = 0;
      }
    }
    ans = 0;
    for(y = 1; y < h + 1; y++){
      for(x = 1; x < w + 1; x++){
        if(mp[y][x] == 1 && visited[y][x] == 0){
          ans++;
          bfs(x, y);
        }
      }
    }
    cout << ans << endl;
  }
  return 0;
}

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

using namespace std;

vector<vector<int> > imap(52,vector<int>(52,0));
int w,h;

void dfs(int nowx,int nowy){
  imap[nowx][nowy]=0;
  for(int i=-1;i<2;++i){
    for(int j=-1;j<2;++j){
      if(imap[nowx+i][nowy+j]==1) dfs(nowx+i,nowy+j);
    }
  }
}

int main(){
  while(cin>> w>>h&&(w||h)){
    for(int i=1;i<h+1;++i)
      for(int j=1;j<w+1;++j)
	cin >> imap[i][j];

    int cnt=0;
    for(int i=1;i<h+1;++i)
      for(int j=1;j<w+1;++j)
	if(imap[i][j]==1){
	  cnt++;
	  dfs(i,j);
	}
    cout << cnt <<endl;
  }
  
  return 0;
}

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

int h , w , count ;
int dx[] = { -1,0,1,-1,1,-1,0,1 } ;
int dy[] = { -1,-1,-1,0,0,1,1,1 } ;
int map[51][51] = {} ;

void destroy( int ny , int nx ){
	map[ny][nx] = 0 ;
	
	for( int k=0 ; k<8 ; k++ ){
		if( ny+dy[k] >= 0 && ny+dy[k] < h &&
			nx+dx[k] >= 0 && nx+dx[k] < w &&
			map[ny+dy[k]][nx+dx[k]] == 1 ){
			destroy( ny+dy[k] , nx+dx[k] ) ;
		}
	}
}

int main(){
	
	while(1){
		cin >> w >> h ;
		if( h==0 && w==0 ) break ;
		
		count = 0 ;
		for( int i=0 ; i<h ; i++ ){
			for( int j=0 ; j<w ; j++ ){
				cin >> map[i][j] ;
			}
		}
		
		for( int i=0 ; i<h ; i++ ){
			for( int j=0 ; j<w ; j++ ){
				if( map[i][j] == 1 ){
					destroy(i,j) ;
					count++;
				}
			}
		}
		
		cout << count << endl ;
	}
	
return 0;
}

C++: #include <bits/stdc++.h>
using namespace std;

int state[100][100];
bool reached[100][100];
int w,h;

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

void dfs(int x, int y) {

	if(x==0 || w<x || y==0 || h<y || state[x][y] == 0)
		return;
	if(reached[x][y] == true)
		return;

	reached[x][y]=true;

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

int main() {

	memset(state, 0, sizeof(state));
	memset(reached, false, sizeof(reached));

	cin >> w >> h;
	for(int i=1; i<=w; i++) {
		for(int j=1; j<=h; j++) {
			cin >> state[i][j];
		}
	}
	int count=0;
	for(int i=1; i<=w; i++) {
		for(int j=1; j<=h; j++) {
			if(state[i][j]==0)
				continue;
			else{
				dfs(i,j);
				count++;
			}
		}
	}
	cout << count << endl;
	return 0;
}