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 <iostream>
#include <ios>
#include <iomanip>
#include <algorithm>
#include <string>
#include <vector>
#include <map>
#include <set>
#include <cstdlib>
#include <cstring>
#include <cmath>
using namespace std;

#define MOD 1e9+7
#define INF 1e9+7
#define rep(i, n) for(int i=0; i<n; ++i)
typedef long long ll;

#define MAX_N 100000
#define MAX_V 100
#define MAX_E 100

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

int ddx[] = {-1, 0, 1, 0, -1, 1, -1, 1};
int ddy[] = {0, -1, 0, 1, 1, 1, -1, -1};

//--------------------------

void dfs(int x, int y);

int w, h, cnt;

int c[50][50];

int main() {

  while(true) {
    cin >> w >> h;

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

    cnt = 0;

    rep(i, h) {
      rep(j, w) cin >> c[i][j];
    }

    rep(i, h) {
      rep(j, w) {
        if(c[i][j]==1) {
          dfs(i, j);
          ++cnt;
        }
      }
    }

    cout << cnt << endl;
  }

  return 0;
}

void dfs(int x, int y) {

  if(x<0||x>=h||y<0||y>=w||c[x][y]==0) {
    return;
  }

  c[x][y] = 0;

  rep(i, 8) {
    dfs(x+ddx[i], y+ddy[i]);
  }
}

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

int is[52][52];
int w, h, c;

void search(int x, int y){
	int i, j;
	if(!is[x][y]) return;
	is[x][y] = 0;
	for(i = -1; i <= 1; i++){
		for(j = -1; j <= 1; j++){
			search(x + i , y + j);
		}
	}
	return;
}

int main(void){
	int i, j;
	while(cin >> h >> w, w){
		memset(is, 0, sizeof(is));
		c = 0;
		for(i = 1; i <= w; i++)
			for(j = 1; j <= h; j++)
				cin >> is[i][j]; 

		while(1){
			for(i = 1; i <= w; i++){
				for(j = 1; j <= h; j++){
					if(is[i][j]) break;
				}
				if(is[i][j]) break;
			}
			if(i == w+1) break;
			search(i, j);
			c++;
		}
		cout << c << endl;
	}
	return 0;
}

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

#define	mp	make_pair

using namespace std;

typedef	pair<int,int>	pii;

const int dx[]={1,1,0,-1,-1,-1,0,1},dy[]={0,-1,-1,-1,0,1,1,1};
int field[50][50];
bool visited[50][50];

int main(){
	for(int w,h;scanf("%d%d",&w,&h),w;){
		for(int i=0;i<h;i++)for(int j=0;j<w;j++){
			scanf("%d",field[i]+j);
			visited[i][j]=false;
		}

		int cnt=0;
		for(int i=0;i<h;i++)for(int j=0;j<w;j++){
			if(field[i][j]==1 && !visited[i][j]){
				visited[i][j]=true;
				queue<pii> qu;	qu.push(mp(i,j));
				while(!qu.empty()){
					pii a=qu.front();	qu.pop();
					for(int k=0;k<8;k++){
						int xx=a.second+dx[k],yy=a.first+dy[k];
						if(0<=xx && xx<w && 0<=yy && yy<h
						&& field[yy][xx]==1 && !visited[yy][xx]){
							qu.push(mp(yy,xx));
							visited[yy][xx]=true;
						}
					}
				}
				cnt++;
			}
		}

		printf("%d\n",cnt);
	}

	return 0;
}

C++: #include<iostream>
using namespace std;
int w,h,i,j,c[50][50];

void visit(int i,int j){

  if(i<0||j<0)return;
  if(i>=50||j>=50)return;

  c[i][j]=0;
  for(int k=-1;k<=1;k++)
    for(int l=-1;l<=1;l++)
      if(c[i+k][j+l]==1)
	visit(i+k,j+l);
     
}
int main(){
  int count;
  while(1){ 
    count=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){
	  count++;
	  visit(i,j);
	}
      }
    }
    cout<<count<<endl;
  }
  return 0;
}

C++: #include "bits/stdc++.h"
using namespace std;
typedef pair<int, int> P;
int dx[8] = { -1, -1, 0, 1, 1, 1, 0, -1 };
int dy[8] = { 0, -1, -1, -1, 0, 1, 1, 1 };
void DFS(vector<vector<int>>&A, P p, int H, int W) {
	A.at(p.first).at(p.second) = 0;
	for (int i = -1; i <= 1; i++) {
		for (int j = -1; j <= 1; j++) {
			if (i == 0 && j == 0) continue;
			int nx = p.second + i;
			int ny = p.first + j;
			if (0 <= nx && nx < W && 0 <= ny && ny < H && A.at(ny).at(nx) == 1) DFS(A, make_pair(ny, nx), H, W);
		}
	}
	return;
}
int main() {
	int W, H;
	cin >> W >> H;
	while (W != 0 && H != 0) {
		vector<vector<int>>A(H, vector<int>(W));
		for (int i = 0; i < H; i++) {
			for (int j = 0; j < W; j++) {
				cin >> A.at(i).at(j);
			}
		}
		vector<vector<bool>>reach(H, vector<bool>(W, false));
		int ans = 0;
		for (int i = 0; i < H; i++) {
			for (int j = 0; j < W; j++) {
				if (A.at(i).at(j) == 1) {
					DFS(A, make_pair(i, j), H, W);
					ans++;
				}
			}
		}
		cout << ans << endl;
		cin >> W >> H;
	}
}

C++: #include <bits/stdc++.h>
#define rep(i, n) for(int i = 0; i < (n); i++)
using namespace std;
using ll = long long;
using pii = pair<int, int>;

int w, h;
vector<vector<int>> g, d;

void dfs(int y, int x) {
	d[y][x] = 1;
	for (int i = -1; i <= 1; i++) {
		for (int j = -1; j <= 1; j++) {
			int dy = y + i;
			int dx = x + j;
			if (dy < 0 || dy >= h || dx < 0 || dx >= w) continue;
			if (g[dy][dx] == 0 || d[dy][dx] != 0) continue;
			dfs(dy, dx);
		}
	}
}

int main() {
	while (cin >> w >> h) {
		if (w == 0 && h == 0) break;
		g.assign(h, vector<int>(w));
		rep(i, h) rep(j, w) cin >> g[i][j];
		d.assign(h, vector<int>(w));
		int ans = 0;
		rep(i, h) rep(j, w) {
			if (g[i][j] == 0 || d[i][j] != 0) continue;
			dfs(i, j);
			ans++;
		}
		cout << ans << endl;
	}
	return 0;
}

C++: #include <iostream>
#include <fstream>
#include <string>
#include <cstring>
#include <cmath>
#include <cstdio>
#include <algorithm>
#include <iomanip>
#include <vector>
#include <utility>
#include <numeric>
#include <stack>
#include <queue>
using namespace std;
#define ll long long
#define INF 1000000007
int h,w;
int m[50][50];
void dfs(int k,int n){
    m[k][n]=0;
    for(int i=-1;i<=1;i++){
        for(int j=-1;j<=1;j++){
            if(i==0&&j==0)continue;
            int ni=k+i;
            int nj=n+j;
            if(0<=ni&&ni<h&&0<=nj&&nj<w&&m[ni][nj]==1)dfs(ni,nj);
        }
    }
    return;
}
int count(){
    int re=0;
    for(int i=0;i<h;i++){
        for(int j=0;j<w;j++){
            if(m[i][j]==1){
                dfs(i,j);
                re++;
            }
        }
    }
    return re;
}
int main(){
    while(1){
        cin>>w>>h;
        if(h==0&&w==0)break;
        for(int i=0;i<h;i++){
            for(int j=0;j<w;j++){
                cin>>m[i][j];
            }
        }
        cout<<count()<<endl;
    }
    return 0;
}