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<bits/stdc++.h>
using namespace std;
int m[51][51];
void del(int a,int b){
	m[a][b]=0;
	if(m[a-1][b]==1)del(a-1,b);
	if(m[a][b-1]==1)del(a,b-1);
	if(m[a+1][b]==1)del(a+1,b);
	if(m[a][b+1]==1)del(a,b+1);
	if(m[a-1][b-1]==1)del(a-1,b-1);
	if(m[a-1][b+1]==1)del(a-1,b+1);
	if(m[a+1][b-1]==1)del(a+1,b-1);
	if(m[a+1][b+1]==1)del(a+1,b+1);
}
int main(){
	int a,b,i,j,w,q;
	while(cin>>a>>b,a!=0&&b!=0){
		int c=0;
		for(i=0;i<b;i++)for(j=0;j<a;j++)cin>>m[i][j];
        for(i=0;i<b;i++)for(j=0;j<a;j++)if(m[i][j]==1){del(i,j);c++;}
        	cout<<c<<endl;
    }
}

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

using namespace std;

// 入力
int H, W;

// MAP
vector<vector<int>> field;

// 左、上、右、下、左上、右上、右下、左下
vector<int> dx = {-1, 0, 1, 0, -1, 1, 1, -1};
vector<int> dy = {0, 1, 0, -1, 1, 1, -1, -1};

// 探索しない条件
// 1, 範囲外の場合
// 2, 海の場合

void dfs(int h, int w) {
    // 探索実行
    field.at(h).at(w) = 0;

    for (int i = 0; i < 8; i++) {  // 行先
        int nx = w + dx.at(i);
        int ny = h + dy.at(i);

        // 1, 範囲外の場合
        if (nx < 0 || W <= nx || ny < 0 || H <= ny) continue;

        // 2, 海の場合 + 探索済みの場合
        if (field.at(ny).at(nx) == 0) continue;

        dfs(ny, nx);
    }
}

int main() {
    while (cin >> W >> H) {
        if (H == 0 || W == 0) break;
        field.assign(H, vector<int>(W, 0));
        for (int h = 0; h < H; h++) {
            for (int w = 0; w < W; w++) {
                cin >> field.at(h).at(w);
            }
        }

        // 探索開始
        int count = 0;
        for (int h = 0; h < H; h++) {
            for (int w = 0; w < W; w++) {
                if (field.at(h).at(w) == 0) continue;
                dfs(h, w);
                count++;
            }
        }

        cout << count << endl;
    }

    return 0;
}

C++: #include <iostream>
#include <algorithm>
using namespace std;
int c[51][51],w,h;
void func(int i,int j){
  c[i][j]=0;
  if(i+1<h&&c[i+1][j]==1)
    func(i+1,j);
  if(i-1>=0&&c[i-1][j]==1)
    func(i-1,j);
  if(j+1<w&&c[i][j+1]==1)
    func(i,j+1);
  if(j-1>=0&&c[i][j-1]==1)
    func(i,j-1);
  if(i+1<h&&j+1<w&&c[i+1][j+1]==1)
    func(i+1,j+1);
  if(i+1<h&&j-1>=0&&c[i+1][j-1]==1)
    func(i+1,j-1);
  if(i-1>=0&&j+1<w&&c[i-1][j+1]==1)
    func(i-1,j+1);
  if(i-1>=0&&j-1>=0&&c[i-1][j-1]==1)
    func(i-1,j-1);
}
int main(){
  while(1){
    int i,j,ans=0;
    cin>>w>>h;
    if(w==0&&h==0)
      break;
    for(i=0;i<h;i++)
      for(j=0;j<w;j++)
	cin>>c[i][j];
    for(i=0;i<h;i++){
      for(j=0;j<w;j++){
	if(c[i][j]==1){
	  func(i,j);
	  ans++;
	}
      }
    }
    cout<<ans<<endl;
  }
}

C++: #include<bits/stdc++.h>
using namespace std;
using ll=long long;
#define fi first
#define se second
using Graph=vector<vector<int>>;
using P=pair<int,int>;
#define mp make_pair

void dfs(int u,int v,Graph graph,Graph &d,int flag)
{
	int h,w;
	h=graph.size();
	w=graph[0].size();
	int n=graph.size();
	d[u][v]=flag;
	vector<P> around={mp(u-1,v),mp(u+1,v),mp(u-1,v-1),mp(u-1,v+1),mp(u,v+1),mp(u,v-1),mp(u+1,v+1),mp(u+1,v-1)};
	for (int i=0;i<8;i++)
	{
		int x=around[i].fi,y=around[i].se;
		if (0<=x&&x<h && 0<=y&&y<w)
		{
			if (graph[x][y]==1 && d[x][y]==-1)
			{
				//cout << x << y << endl;
				dfs(x,y,graph,d,flag);
			}

		}
	}
}

int main()
{
	vector<int> ans;
	while (true)
	{
		int w,h;cin >> h >> w;
		if (w==0 && h==0) break;
		Graph graph(w,vector<int> (h,0)),d(w,vector<int> (h,0));
		for (int i=0;i<w;i++)
		{
			for (int j=0;j<h;j++)
			{
				cin >> graph[i][j];
				if (graph[i][j]==1) d[i][j]=-1;
			}
		}
		int flag=1;
		for (int i=0;i<w;i++)
		{
			for (int j=0;j<h;j++)
			{
				if (d[i][j]==-1)
				{
					dfs(i,j,graph,d,flag);
					flag++;
				}
			}
		}
		ans.push_back(flag-1);
	}
	for (int i=0;i<ans.size();i++) cout << ans[i] << endl;
	return 0;
}

C++: #include <cstdio>
#define f(n)for(int i=0;i<n;i++)
#define s(w)scanf("%d",&w);
int a,n,m,d[99][99],u[16]={-1,-1,-1,0,0,1,1,1,-1,0,1,-1,1,-1,0,1};void k(int x,int y){if(d[x][y]){d[x][y]=0;f(8)k(x+u[i],y+u[i+8]);}}int main(){a=0;s(n)s(m)if(n==0)return 0;f(9801)d[0][i]=0;f(n*m)s(d[i/n+1][i%n+1])f(n*m)if(d[i/n+1][i%n+1])a++,k(i/n+1,i%n+1);printf("%d\n",a);main();}

C++: #include <iostream>
#include <string>
#include <algorithm>
#include <vector>
#include <cmath>
#include <cstdlib>
#include <tuple>
using namespace std;
using ll=long long;
using R=double;

int W,H;

int t[50][50];

void visit(int h,int w){
  t[h][w]=0;
  for(int nh=h-1;nh<h+2;nh++){
    for(int nw=w-1;nw<w+2;nw++){
      if(t[nh][nw]==1&&0<=nh&&0<=nw&&nh<H&&nw<W)visit(nh,nw);
    }
  }
}

int main(void){
  int a,b,c,n;
  while(1){
    cin>>W>>H;
    c=0;
    if(W==0&&H==0)break;
    for(int i=0;i<H;i++){
      for(int j=0;j<W;j++){
    cin>>t[i][j];
      }
    }
    for(int i=0;i<H;i++){
      for(int j=0;j<W;j++){
    if(t[i][j]==1){
      c++;
      visit(i,j);
    }
      }
    }
    cout<<c<<endl;
  }
}

C++: #include <iostream>
#define REP(i,a,b) for(int i = (a); i < (b); i++)
#define rep(i,a) REP(i,0,a)
using namespace std;

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

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

void dfs(int x,int y) {
    c[y][x] = 0;
    
    rep(i,8) {
        int nx = x+dx[i], ny = y+dy[i];
        if (0 <= nx && nx < w && 0 <= ny && ny < h && c[ny][nx] == 1) dfs(nx,ny);
    }
}
    
int main(void){
    while (1) {
        cin >> w >> h;
        if (w == 0 && h == 0) break;
        rep(y,h) rep(x,w) cin >> c[y][x];
        
        int ans = 0;        
        rep(y,h)
            rep(x,w)
                if (c[y][x] == 1) {
                    dfs(x,y);
                    ans++;
                }

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