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;

int w, h;
int c[55][55];

void dfs(int y, int x)
{
	if(c[y][x]==0) return;
	c[y][x] = 0;
	for (int i = -1; i < 2; ++i)
	{
		for (int j = -1; j < 2; ++j)
		{
			if(i==0&&j==0) continue;
			if(y+i>=0&&y+i<h&&x+j>=0&&x+j<w) dfs(y+i, x+j);
		}
	}
	return;
}

int main(int argc, char const *argv[])
{
	while(1)
	{
		cin >> w >> h;
		if(w==0&&h==0) break;
		for (int i = 0; i < h; ++i) for (int j = 0; j < w; ++j) cin >> c[i][j];

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

C++: #include<iostream>
#include<vector>
#include<algorithm>
#include<string>
#include<queue>
#include<utility>
int dx[4] = {0,1,0,-1};
int dy[4] = {1,0,-1,0};
int gx[8] = {1,1,0,-1,-1,-1,0,1};
int gy[8] = {0,1,1,1,0,-1,-1,-1};
#define REP(i,n) for(int i=0;i<(n);i++)

using namespace std;

int nextInt(){int a;cin>>a;return a;}

int main(){
  int w,h,ct;
  
  queue<pair<int,int>> que;
  while(cin>>h>>w){
    if(!h&&!w)break;
    vector<vector<int>> d(w,vector<int>(h,-1));
    REP(i,w){
      REP(j,h){
	d[i][j] = nextInt();
      }
    }
    ct = 0;
    REP(i,w){
      REP(j,h){
	int def_i = i;
	int def_j = j;
	if(d[def_i][def_j] !=1)continue;
	else ct++;
	  que.push(make_pair(def_i,def_j));
	  while(que.size()){
	    pair<int,int> p = que.front();
	    que.pop();
	    REP(k,8){
	      def_i = gy[k] + p.first;def_j = gx[k] + p.second;
	      if(def_i>=0 && def_i<w && def_j>=0 && def_j<h){
		if(d[def_i][def_j] !=1)continue;
		else {
		  d[def_i][def_j] = -1;
		  que.push(make_pair(def_i,def_j));
		}
	      }
	    }
	  }
      }
    }
    cout << ct << endl;
    
  }
	
	return 0;
}

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

const int MAX = 50;
int dx[8] = { 1,0,-1,0,1,1,-1,-1 };
int dy[8] = { 0,1,0,-1,1,-1,1,-1 };
int map[MAX][MAX];
int color[MAX][MAX];

void dfs(int x, int y, int c)
{
	color[x][y] = c;

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

		if (nx >= 0 && nx < MAX&&ny >= 0 && ny < MAX) {
			if (map[nx][ny] == 1) {
				if (color[nx][ny] == 0) {
					dfs(nx, ny, c);
				}
			}
		}
	}
}

int main()
{
	while (true) {
		int w, h;
		cin >> w >> h;

		if (w == 0 && h == 0) {
			break;
		}

		for (int i = 0; i < MAX; i++) {
			for (int j = 0; j < MAX; j++) {
				map[i][j] = 0;
				color[i][j] = 0;
			}
		}

		for (int i = 0; i < h; i++) {
			for (int j = 0; j < w; j++) {
				cin >> map[i][j];
			}
		}

		int ans = 0;

		for (int i = 0; i < h; i++) {
			for (int j = 0; j < w; j++) {
				if (map[i][j] == 1) {
					if (color[i][j] == 0) {
						ans++;
						dfs(i, j, ans);
					}
				}
			}
		}

		cout << ans << endl;
	}

	return 0;
}

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

int w,h;
int f[51][51];
int d[3] = {0,-1,1};

void dfs(int x, int y) {
    int i,j,nx,ny;
    f[x][y] = 0;
    for(i=0; i<3; i++)
        for(j=0; j<3; j++) {
            if(i+j == 0)
                continue;
            nx=x+d[i], ny=y+d[j];
            if(nx<0 || nx>=h || ny<0 || ny>=w)
                continue;
            if(f[nx][ny] == 1)
                dfs(nx,ny);
        }
}

int main() {
    int i,j,r;
    while(1) {
        r=0;
        cin >> w >> h;
        if(w+h == 0)
            break;

        for(i=0; i<h; i++)
            for(j=0; j<w; j++)
                cin >> f[i][j];
        
        for(i=0; i<h; i++)
            for(j=0; j<w; j++)
                if(f[i][j] == 1) {
                    dfs(i, j);
                    r++;
                }
        cout << r << endl;
    }
}

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

int a[52][52];
int w, h;
int yy[8] = {-1, 0, 0, 1, -1, -1, 1, 1};
int xx[8] = {0, 1, -1, 0, -1, 1, -1, 1};
int solve(int y, int x){
	if(a[y][x] == 0)return 0;
	a[y][x] = 0;
	int ans = 0;
	for(int i = 0;i < 8;i++){
		ans += solve(y+yy[i], x+xx[i]);
	}
	return 1;
}

int main(){
	int y, x;
	while(cin >> w >> h, w){
		int ans = 0;
		fill((int*)a, (int*)(a+52), 0);
		for(int i = 1;i <= h;i++)
		for(int j = 1;j <= w;j++)cin >> a[i][j];
		for(int i = 1;i <= h;i++){
			for(int j = 1;j <= w;j++){
				if(a[i][j])ans += solve(i, j);
			}
		}
		cout << ans << endl;
	}
	return 0;
}

C++: #define	_USE_MATH_DEFINES
#include <iostream>
#include <algorithm>
#include <functional>
#include <vector>
#include <cstdio>
#include <string>
#include <cmath>
#include <cfloat>
#include <map>
#include <queue>
#include <stack>
#include <list>
using namespace std;
int x,y;
int o[52][52];
queue<int> q;
void tansaku(int j,int i){
	if(j<1||j>x||i<1||i>y||o[i][j]==0||o[i][j]==2){
		return ;
	}
	if(o[i][j]==1){
		if(q.empty())
		q.push(1);
		o[i][j]=2;
	}
	
	tansaku(j+1,i);
	tansaku(j-1,i);
	tansaku(j,i+1);
	tansaku(j,i-1);
	tansaku(j+1,i+1);
	tansaku(j-1,i-1);
	tansaku(j+1,i-1);
	tansaku(j-1,i+1);
}
int main(){

	while(cin>>x>>y,x!=0||y!=0){
		int count=0;
		for(int i=1;i<=y;i++){
			for(int j=1;j<=x;j++){
				cin>>o[i][j];
			}
		}
		for(int i=1;i<=y;i++){
			for(int j=1;j<=x;j++){
				tansaku(j,i);
				if(!q.empty()){
				count+=q.front();
				}
				q.pop();
			}
		}
		
		cout<<count<<endl;
	}

}