Go: package main

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

func getScanner(fp *os.File) *bufio.Scanner {
	scanner := bufio.NewScanner(fp)
	scanner.Split(bufio.ScanWords)
	scanner.Buffer(make([]byte, 1000005), 1000005)
	return scanner
}

func getNextString(scanner *bufio.Scanner) string {
	scanner.Scan()
	return scanner.Text()
}

func getNextInt(scanner *bufio.Scanner) int {
	i, _ := strconv.Atoi(getNextString(scanner))
	return i
}

func getNextInt64(scanner *bufio.Scanner) int64 {
	i, _ := strconv.ParseInt(getNextString(scanner), 10, 64)
	return i
}

func getNextUint64(scanner *bufio.Scanner) uint64 {
	i, _ := strconv.ParseUint(getNextString(scanner), 10, 64)
	return i
}

func getNextFloat64(scanner *bufio.Scanner) float64 {
	i, _ := strconv.ParseFloat(getNextString(scanner), 64)
	return i
}

func main() {
	fp := os.Stdin
	wfp := os.Stdout
	cnt := 0

	if os.Getenv("MASPY") == "ますピ" {
		fp, _ = os.Open(os.Getenv("BEET_THE_HARMONY_OF_PERFECT"))
		cnt = 0
	}
	if os.Getenv("MASPYPY") == "ますピッピ" {
		wfp, _ = os.Create(os.Getenv("NGTKANA_IS_GENIUS10"))
	}

	scanner := getScanner(fp)
	writer := bufio.NewWriter(wfp)
	for solve(scanner, writer) {

	}
	for i := 0; i < cnt; i++ {
		fmt.Fprintln(writer, "-----------------------------------")
		solve(scanner, writer)
	}
	writer.Flush()
}

func solve(scanner *bufio.Scanner, writer *bufio.Writer) bool {
	sol := solver{}
	sol.w = getNextInt(scanner)
	sol.h = getNextInt(scanner)
	if sol.w == 0 && sol.h == 0 {
		return false
	}
	sol.land = makeInts(sol.h, sol.w)
	for y := 0; y < sol.h; y++ {
		for x := 0; x < sol.w; x++ {
			sol.land[y][x] = getNextInt(scanner)
		}
	}
	ans := 0
	for y := 0; y < sol.h; y++ {
		for x := 0; x < sol.w; x++ {
			if sol.land[y][x] == 0 {
				continue
			}
			ans++
			sol.dfs(y, x)
		}
	}
	fmt.Fprintln(writer, ans)
	return true
}

type solver struct {
	w, h int
	land [][]int
}

func (sol *solver) dfs(y, x int) {
	if y < 0 {
		return
	}
	if y >= sol.h {
		return
	}
	if x < 0 {
		return
	}
	if x >= sol.w {
		return
	}
	if sol.land[y][x] == 0 {
		return
	}
	sol.land[y][x] = 0
	dy := [8]int{-1, -1, -1, 0, 0, 1, 1, 1}
	dx := [8]int{-1, 0, 1, -1, 1, -1, 0, 1}
	for i := 0; i < 8; i++ {
		sol.dfs(y+dy[i], x+dx[i])
	}
}
func makeInts(h, w int) [][]int {
	index := make([][]int, h, h)
	data := make([]int, h*w, h*w)
	for i := 0; i < h; i++ {
		index[i] = data[i*w : (i+1)*w]
	}
	return index
}

C++: #include<bits/stdc++.h>
using namespace std;
using ll=long long;
using ld=long double;

#define rep(i,N) for(int i=0;i<(int)(N);++i)
#define rrep(i,N) for(int i=(int)(N)-1;i>=0;--i)
#define debug(x) cout<<#x<<"="<<x<<endl;
constexpr ll MOD=1000000007;
constexpr ll INF=1LL<<60;
template<class T> inline bool chmin(T& a,T b){if(a>b){a=b;return true;}return false;}
template<class T> inline bool chmax(T& a,T b){if(a<b){a=b;return true;}return false;}
template<typename T> void fail(T v){cout << v << endl;exit(0);}
//template end
int W,H;
bool inside(int h,int w){return 0<=h&&h<H&&0<=w&&w<W;}
int gh[]={1,-1,0,0,1,1,-1,-1},gw[]={0,0,1,-1,-1,1,-1,1},use[51][51],G[51][51];
void dfs(int h,int w){
    rep(k,8){
        int i=gh[k]+h,j=gw[k]+w;
        if(!inside(i,j)||use[i][j]||G[i][j]==0)continue;
        use[i][j]=1;
        dfs(i,j);
    }
}
void solve(){
    while((cin>>W>>H,W|H)){
        rep(i,51)rep(j,51)use[i][j]=0;
        rep(i,H)rep(j,W)cin>>G[i][j];
        int res=0;
        rep(i,H)rep(j,W)if(!use[i][j]&&G[i][j]){dfs(i,j);res++;}
        cout<<res<<endl;
    }
}

int main(){
    cin.tie(0);
    ios::sync_with_stdio(false);
    cout << fixed << setprecision(20);
    solve();
    return 0;
}

C++: #include<bits/stdc++.h>
using namespace std ;
int H , W ;
int board[550][550] ;

void dfs (int x , int y ){
  board[x][y] = 0 ;
  for(int i = -1 ; i < 2 ; i++ )
    for(int j = -1 ; j < 2 ; j++ ){
      int nx = x + i , ny = y + j ;
      if( nx >= 0 && ny >= 0 && nx < H && ny < W &&board[nx][ny] == 1) dfs(nx,ny);
    }
}

int main () {
  while(1){
    int ans = 0 ;
    cin >> W >> H ;
    if(H==0&&W==0) break ;
    for(int i = 0 ; i < H ; i++ ){
      for(int j = 0 ; j < W ; j++ ){
        cin >> board[i][j] ;
      }
    }
    for(int i = 0 ; i < H ; i++ ){
      for(int j = 0 ; j < W ; j++ ){
        if(board[i][j]==1){
          dfs(i,j) ;
          ans ++ ;
        }
      }
    }
    cout << ans << endl ;
  }
}

C++: #include<bits/stdc++.h>
using namespace std;
#define OUT(x) cout<<x<<endl
#define DEBUG(x) cout<<#x<<": "<<x
const int BA = 1000;
const int INFTY = (1<<21);
int c[51][51];
int w,h;
int del(int x,int y){
  if(x<0)return 0;
  if(c[x][y]){
    c[x][y] = 0;
    return del(x+1,y)+del(x,y+1)+del(x+1,y+1)+
      del(x-1,y-1)+del(x-1,y)+del(x,y-1)+del(x+1,y-1)+del(x-1,y+1);
    
  }
  else return 0;
  
}
int solve(int x,int y){
  if(x==0 && y == h)return 0;
  else if(x==w){
    return solve(0,y+1);
  }
  else if(c[x][y]==1){
    del(x,y);
    return solve(x+1,y)+1;
  }
  else return solve(x+1,y);
}
int main(){
  while(cin>>w>>h&&w&&h){
    for(int i=0;i<h;i++){
      for(int j=0;j<w;j++){
	cin >> c[i][j];
      }
    }
    
    cout << solve(0,0) << endl;
  }
}

C++: #include <iostream>
#include <cstdio>
#include <cstring>
#define rep(i,n) for(int i=0;i<(n);i++)

using namespace std;

typedef long long ll;
typedef pair<int, int> P;
int Map[52][52];
bool used[52][52];
const int dir[][2] = { {-1, 0 }, { -1, 1 }, { 0, 1 }, { 1, 1 }, { 1, 0 }, { 1, -1 }, {0, -1 }, {-1, -1 } };
int w, h;

void dfs (int row, int col )
{
	used[row][col] = true;
	rep (k, 8 ){
		int nr = row + dir[k][0];
		int nc = col + dir[k][1];
		if (nr < 0 || nc < 0 || nr >= h || nc >= w || Map[nr][nc] == 0 || used[nr][nc] ) continue;
		dfs (nr, nc );
	} // end rep
}

int main()
{
	while (scanf ("%d %d", &w, &h ) && w && h ){
		memset (used, false, sizeof (used ) );
		rep (i, h ){
			rep (j, w ){
				scanf (" %d", Map[i]+j );
			} // end rep
		} // end rep
		int cnt = 0;
		rep (i, h ){
			rep (j, w ){
				if (Map[i][j] == 1 && !used[i][j] ){
					dfs (i, j );
					cnt++;
				} // end if
			} // end rep
		} // end rep
		printf ("%d\n", cnt );
	} // end loop
		
	return 0;
}