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 <algorithm>
#include <cstdio>
#include <iostream>
#include <map>
#include <cmath>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <vector>
#include <stdlib.h>
#include <stdio.h>
#include <cstring>
using namespace std;
#define FOR(I,A,B) for(int I = (A); I < (B); ++I)
#define CLR(mat) memset(mat, 0, sizeof(mat))
typedef long long ll;
int W, H;
int c[50][50];
int vy[8] = {-1,-1,0,1,1,1,0,-1};
int vx[8] = {0,1,1,1,0,-1,-1,-1};
void calc(int y, int x) {
  if(y < 0 || y >= H || x < 0 || x >= W) return;
  if(c[y][x] == 0) return;
  c[y][x] = 0;
  FOR(i,0,8) calc(y+vy[i],x+vx[i]);
  return;
}
int main()
{
  while(cin >> W >> H, (W || H)) {
    FOR(y,0,H) FOR(x,0,W) cin >> c[y][x];
    int ans = 0;
    FOR(y,0,H) {
      FOR(x,0,W) {
        if(c[y][x] == 0) continue;
        calc(y, x);
        ans++;
      }
    }
    cout << ans << endl;
  }
  return 0;
}

C++: #include <algorithm>
#include <iostream>
#include <math.h>
#include <stdio.h>
#include <string>
#include <sstream>
#include <vector>
#include <set>
#include <stack>
#include <map>
#include <cmath>
#include <queue>
#define rep(i, n) for(int i = 0; i < (int)(n); i++)
using namespace std;
typedef unsigned long long ll;
#define MAX_N 50

int field[MAX_N][MAX_N];

void init(){
	rep(i,MAX_N){
		rep(j,MAX_N){
			field[i][j] = 0;
		}
	}
}

void rec(int x,int y,int maxW,int maxH){
	if(field[y][x] == 0){
		return;
	}
	field[y][x] = 0;
	int dx[8] = {-1,0,1,-1,1,-1,0,1};
	int dy[8] = {-1,-1,-1,0,0,1,1,1};
	rep(i,8){
		int nx,ny;
		nx = x + dx[i];
		ny = y + dy[i];
		if( 0 <= nx && nx < maxW && 0 <= ny && ny < maxH){
			rec(nx,ny,maxW,maxH);
		}
	}
	return;
}

int main()
{
	int w,h;
	cin>>w>>h;
	while( !((w == 0) && (h == 0))){
		init();
		rep(i,h){
			rep(j,w){
				cin>>field[i][j];
			}
		}
		int ans = 0;
		rep(i,h){
			rep(j,w){
				if(field[i][j] == 1){
					ans++;
					rec(j,i,w,h);
				}
			}
		}
		cout<<ans<<endl;
		cin>>w>>h;
	}

	return 0;
}

C++: #include <cstdio>
#include <utility>
#include <cmath>
#include <algorithm>
#include <iostream>
#include <sstream>
#include <iomanip>
#include <vector>
#include <string>
#define REP(i,n) for(int i=0;i<n;i++)
typedef long long ll;
using namespace std;
typedef vector<ll> vl;
typedef pair<ll,ll> pll;
typedef vector<pll> vpll;
typedef vector<string> vs;

int main() 
{
	
	int w,h;
	while(cin >> w >> h && !(w==0&&h==0))
	{
		int a[50][50]={};
		REP(i,h){REP(j,w){cin >> a[j][i];}}
		
		int id = 10; //島のid 初期値は適当
		
		//w=0の時
		if(a[0][0]!=0){a[0][0]=id;}
		for(int i=1;i<h;i++){
			if(a[0][i]!=0 && a[0][i-1]==0){id++;a[0][i]=id;}
			else if(a[0][i]!=0){a[0][i]=a[0][i-1];}
		}
		//w>0の時
		for(int i=1;i<w;i++)
		{
			//h=0の時
			if(a[i][0]!=0){
				if(a[i-1][0]!=0 || a[i-1][1]!=0){a[i][0]=max(a[i-1][0],a[i-1][1]);}
				else{id++;a[i][0]=id;}
			}
			//h>0の時
			for(int j=1;j<h;j++)
			{
				if(a[i][j]!=0)
				{
					int tmp = max(a[i][j-1],a[i-1][j-1]);
					tmp = max(tmp,a[i-1][j]);
					tmp = max(tmp,a[i-1][j+1]);
					if(tmp==0){id++;a[i][j]=id;}
					else{
						a[i][j] = tmp;
						int tmp1,tmp2,tmp3,tmp4;
						tmp1=a[i][j-1];
						tmp2=a[i-1][j-1];
						tmp3=a[i-1][j];
						tmp4=a[i-1][j+1];
						if(a[i][j-1]!=0 && a[i][j-1]!=tmp){REP(k,w){REP(l,h){if(a[k][l]==tmp1)a[k][l]=tmp;}}}
						if(a[i-1][j-1]!=0 && a[i-1][j-1]!=tmp){REP(k,w){REP(l,h){if(a[k][l]==tmp2)a[k][l]=tmp;}}}
						if(a[i-1][j]!=0 && a[i-1][j]!=tmp){REP(k,w){REP(l,h){if(a[k][l]==tmp3)a[k][l]=tmp;}}}
						if(a[i-1][j+1]!=0 && a[i-1][j+1]!=tmp){REP(k,w){REP(l,h){if(a[k][l]==tmp4)a[k][l]=tmp;}}}
					}
				}
			}
		}
		int b[2500]={};
		REP(i,w){REP(j,h){b[i+w*j]=a[i][j];}}
		sort(b,b+h*w);
		reverse(b,b+h*w);
		
		int ans(0);
		if(b[0]>1){ans++;}
		for(int i=1;i<w*h;i++){if(b[i]!=b[i-1] && b[i]>1)ans++;}
		
		cout << ans << endl;
	}
	return 0;
}

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

#define W 50
#define H 50

using namespace std;
void dfs(int w, int h);
int map[H][W];
int dy[8] = {1,1,0,-1,-1,-1,0,1}, dx[8] = {0,-1,-1,-1,0,1,1,1};
int w,h;

int main(){
  int i = 0,j = 0,is=0;
  cin >> w >> h;

  while(w != 0 && h != 0){
    for(i = 0;i < h;i++){
      for(j = 0;j<w;j++){
	cin>>map[i][j];
      }
    }
    i = j = 0;
    is = 0;
    while(i <= h-1  && j <= w-1){
      if(map[i][j] == 1){
	dfs(i,j);
	is++;
      }
      i++;
      if(i == h){
	i = 0;
	j++;
      }
    }
    cout << is << endl;
    cin >> w >> h;
  }
  return 0;
}

void dfs(int x,int y){
  int i = 0;
  map[x][y] = 0;
  for(i = 0;i < 8; i++){
    if(x + dx[i] > -1 && y + dy[i] > -1 && x + dx[i] < h && y + dy[i] < w){
      if(map[x + dx[i]][y + dy[i]] == 1){
	dfs(x + dx[i],y + dy[i]);
      }
    }
  }
}