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 <algorithm>
#include <bitset>
#include <cassert>
#include <cctype>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <ctime>
#include <deque>
#include <functional>
#include <iomanip>
#include <iostream>
#include <list>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <utility>
#include <vector>

/*
 * Union-Find tree
 * header requirement: vector
 */
class UnionFind {
private:
  std::vector<int> disj;
  std::vector<int> rank;
public:
  UnionFind(int n) : disj(n), rank(n) {
    for (int i = 0; i < n; ++i) {
      disj[i] = i;
      rank[i] = 0;
    }
  }
  int root(int x) {
    if (disj[x] == x) {
      return x;
    }
    return disj[x] = root(disj[x]);
  }
  void unite(int x, int y) {
    x = root(x);
    y = root(y);
    if (x == y) {
      return;
    }
    if (rank[x] < rank[y]) {
      disj[x] = y;
    } else {
      disj[y] = x;
      if (rank[x] == rank[y]) {
	++rank[x];
      }
    }
  }
  bool is_same_set(int x, int y) {
    return root(x) == root(y);
  }
};


#define REP(i,s,n) for(int i=(int)(s);i<(int)(n);i++)

using namespace std;
typedef long long int ll;
typedef vector<int> VI;
typedef vector<ll> VL;
typedef pair<int, int> PI;
const ll mod = 1e9 + 7;



int main(void){
  int w, h;
  while ((cin >> w >> h) && (w || h)) {
    vector<VI> mp(h, VI(w, 0));
    REP(i, 0, h) {
      REP(j, 0, w) {
	cin >> mp[i][j];
      }
    }
    UnionFind uf(w * h);
    REP(i, 0, h) {
      REP(j, 0, w) {
	if (mp[i][j] == 0) { continue; }
	REP(dx, -1, 2) {
	  REP(dy, -1, 2) {
	    if (dx == 0 && dy == 0) { continue; }
	    int nx = i + dx;
	    int ny = j + dy;
	    if (0 <= nx && nx < h && 0 <= ny && ny < w && mp[nx][ny]) {
	      // unite
	      uf.unite(i * w + j, nx * w + ny);
	    }
	  }
	}
      }
    }
    set<int> roots;
    REP(i, 0, h * w) {
      int x = i / w;
      int y = i % w;
      if (mp[x][y]) {
	roots.insert(uf.root(i));
      }
    }
    cout << roots.size() << endl;
  }
}

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

int c[51][51];
int w, h;
int dx[8] = { 0,1,1,1,0,-1,-1,-1 };
int dy[8] = { -1,-1,0,1,1,1,0,-1 };

void dfs(int i, int j) {
  if(i < 0 || i >= h || j < 0 || j >= w || c[i][j] == 0)
    return;

  c[i][j] = 0;
  for(int k=0;k<8;k++) {
    dfs(i+dy[k], j+dx[k]);
  }
  return;
}

int main() {
  cin.tie(0);
  ios::sync_with_stdio(false);
  while(true) {
    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];
      }
    }

    ll 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;

  }
}

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

using namespace std;

int w, h;
int maps[50][50];

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

void dfs(int x, int y)
{
  maps[y][x] = 0;
  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 && maps[ny][nx] == 1)
      dfs(nx, ny);
  }
  return;
}

int main()
{
  while(cin >> w >> h, w || h){
    for(int i = 0; i < h; i++)
      for(int j = 0; j < w; j++)
	cin >> maps[i][j];

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

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

using namespace std;

bool check(bool[52][52],pair<int,int>);
void rmv(bool[52][52],int,int);

int main(void){
	for(int w,h;cin>> w >> h,w||h;){
		bool fld[52][52]={0};
		int n;

		for(int i=1;i<=h;i++)
			for(int j=1;j<=w;j++)
				cin >> fld[i][j];

		for(n=0;check(fld,make_pair(w,h));n++);
		cout << n << endl;
	}
	return 0;
}

bool check(bool fld[52][52],pair<int,int> wide){
	for(int i=1;i<=wide.second;i++)
		for(int j=1;j<=wide.first;j++)
			if(fld[i][j]){
				rmv(fld,i,j);
				return true;
			}

	return false;
}

void rmv(bool fld[52][52],int y,int x){
	if(fld[y][x]){
		fld[y][x]=false;
		for(int i=-1;i<=1;i++)
			for(int j=-1;j<=1;j++)
				rmv(fld,y+i,x+j);
	}
}

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

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

void dfs(int y, int x)
{
	f[y][x] = 0;
	for(int i=0; i<8; i++)
	{
		int ny = y+dy[i];
		int nx = x+dx[i];
		if(f[ny][nx])
		{
			dfs(ny,nx);
		}
	}
}

int main()
{
	int w,h,c,x,y;
	while(cin >> w >> h, w||h)
	{
		memset(f,0,sizeof(f));

		for(y=1; y<=h; y++)
		for(x=1; x<=w; x++)
		{
			cin >> f[y][x];
		}

		c=0;
		for(y=1; y<=h; y++)
		for(x=1; x<=w; x++)
		{
			if(f[y][x])
			{
				c++;
				dfs(y,x);
			}
		}
		cout << c << endl;
	}
}

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

int w,h;
int m[50][50];

void solve(int x,int y){
	if(m[y][x] == 1){
		m[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){
					solve(x+j,y+i);
				}
			}
		}
	}
}

int main(){
	while(true){
		int count=0;

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

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

		for(int i=0;i<h;i++){
			for(int j=0;j<w;j++){
				if(m[i][j] == 1){
					solve(j,i);
					count++;
				}
			}
		}

		cout << count << endl;
	}
}