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[111][111];

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

bool used[111][111];
void solve(int x,int y){
  if( used[x][y] ) return;
  used[x][y] = true;
  for(int i=0;i<8;i++){
    int nx = x + dx[i], ny = y + dy[i];
    if( nx < 0 || ny < 0 || nx >= W || ny >= H ) continue;
    if( C[nx][ny] == 1 )
      solve( nx, ny );
  }
}

int main(){

  while(  cin >> W >> H && (W||H) ){
    for(int i=0;i<H;i++){
      for(int j=0;j<W;j++){
	cin >> C[j][i];
      }
    }
    
    int res = 0;
    memset(used,0,sizeof(used));
    for(int i=0;i<H;i++){
      for(int j=0;j<W;j++){
	if( !used[j][i] && C[j][i] == 1 )
	  {
	    res++;
	    solve(j,i);
	  }
      }
    }
    cout << res << endl;
  }


}

C++: #include <iostream>

using namespace std;
int field[50][50];
int w, h, ans;
int dx[8] = {-1, 0, 1, -1, 1, -1, 0, 1}, dy[8] = {-1, -1, -1, 0,  0, 1, 1, 1};

void check(int x, int y)
{
    if (field[x][y])
        field[x][y] = 0;
    else
        return;
        
    for (int i=0; i<8; i++) {
        int nx = x+dx[i], ny = y+dy[i];
        if (0<= nx && nx<w && 0<=ny&&ny<h) {
            check(nx, ny);
        }
    }
}

int main()
{
    while (cin>>w>>h, w||h) {
        for (int y=0; y<h; y++) {
            for (int x=0; x<w; x++) {
                int i; cin >> i;
                field[x][y] = i;
                }
           }
       
       ans = 0;
       for (int y=0; y<h; y++) {
          for (int x=0; x<w; x++) {
               if (field[x][y]) {
                       ans++;
                       check(x, y);
               }  
            }   
        }

        cout << ans << endl;
   }           
    
    return 0;
}

C++: #include <math.h>
#include <stdlib.h>
#include <string.h>
#include <algorithm>
#include <deque>
#include <iomanip>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <vector>

using namespace std;

#define ll long long
#define INF 1e9
#define MOD 1e9 + 7
#define rep(i, n) for (int i = 0; i < n; i++)
#define loop(i, a, n) for (int i = a; i < n; i++)
#define all(in) in.begin(), in.end()
int c[51][51];
int w, h;
void dfs(int x, int y) {
    c[x][y] = 0;
    for (int i = -1; i <= 1; i++) {
        for (int k = -1; k <= 1; k++) {
            int new_x = x + i, new_y = y + k;
            if (0 <= new_x && new_x < h && 0 <= new_y && new_y < w &&
                c[new_x][new_y] == 1) {
                dfs(new_x, new_y);
            }
        }
    }
}

int main() {
    while (true) {
        int ans = 0;
        cin >> w >> h;
        if (w == 0 && h == 0) break;
        rep(i, h) rep(k, w) cin >> c[i][k];
        for (int x = 0; x < h; x++) {
            for (int y = 0; y < w; y++) {
                if (c[x][y] == 1) {
                    ans++;
                    dfs(x, y);
                }
            }
        }
        cout << ans << endl;
    }
    return 0;
}

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

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

int mesh[50][50];
int n, m;

struct Point
{
	int x;
	int y;
};

int main()
{
	deque<Point> stack;
	while (true)
	{
		scanf("%d %d", &n, &m);
		if (n == 0 && m == 0) break;
		for (int i = 0; i < m; i++)
		{
			for (int j = 0; j < n; j++)
			{
				int a;
				scanf("%d", &a);
				mesh[i][j] = a;
			}
		}
		int count = 0;
		for (int i = 0; i < m; i++)
		{
			for (int j = 0; j < n; j++)
			{
				if (mesh[i][j] == 1)
				{
					Point pt = {j, i};
					stack.push_front(pt);
					mesh[i][j] = 0;
					while (!stack.empty())
					{
						Point point = stack.front();
						stack.pop_front();
						for (int k = 0; k < 8; k++)
						{
							if (mesh[point.y + dy[k]][point.x + dx[k]] == 1)
							{
								Point pt = {point.x + dx[k], point.y+dy[k]};
								stack.push_front(pt);
								mesh[point.y + dy[k]][point.x + dx[k]] = 0;
							}
						}
					}
					count++;
				}
			}
		}
		printf("%d\n", count);
	}
}

C++: #include<iostream>
#include<sstream>
#include<fstream>
#include<string>
#include<vector>
#include<deque>
#include<queue>
#include<stack>
#include<set>
#include<map>
#include<algorithm>
#include<functional>
#include<utility>
#include<bitset>
#include<cmath>
#include<cstdlib>
#include<ctime>
#include<cstdio>

using namespace std;

#define REP(i,n) for(int i=0;i<int(n);i++)
#define foreach(c,itr) for(__typeof((c).begin()) itr=(c).begin();itr!=(c).end();itr++)
typedef long long ll;
typedef pair<int,int> P;

int L[55][55];
int w,h;
bool inside(int i,int j){
	return (1<=i && i<=h && 1<=j && j<=w);
}

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

void dfs(int i,int j){
	L[i][j]=0;
    REP(k,8){
    int x=i+dx[k],y=j+dy[k];
	if(inside(x,y)){
		if(L[x][y]==1) dfs(x,y);
	}
    }
}

int main(){
	  int i,j;

	  while(1){
		  cin >> w >> h;
		  if(w==0 && h==0) break;
		  for(i=1;i<=h;i++) for(j=1;j<=w;j++) cin >> L[i][j];

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

		  cout << ans << endl;

	  }

	  return 0;
}