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 <sstream>
#include <string>
#include <vector>
#include <stack>
#include <queue>
#include <deque>
#include <set>
#include <map>
#include <algorithm>
#include <functional>
#include <numeric>
#include <utility>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <cmath>
#include <complex>
using namespace std;

typedef long long ll;
typedef pair<int,int> P;

#define all(c) (c).begin(),(c).end()
#define pb push_back
#define mp make_pair
#define ERASE(v,i) (v).erase(remove(all(v),i),(v).end())
#define rep(i,n) for(int i=0;i<(int)n;++i)
#define each(it,c) for(typeof((c).begin()) it=(c).begin();it!=(c).end();++it)
#define debug(x) cerr<<#x<<" = "<<(x)<<endl;
#define LINE cerr<<"LINE: "<<__LINE__<<endl;

inline int toInt(string s){int v;istringstream sin(s);sin>>v;return v;}
template<class T> inline string toString(T x){ostringstream sout;sout<<x;return sout.str();}
template<class T> void preview(T a,T b){for(T it=a;it!=b;++it)cerr<<*it<<" ";cerr<<endl;}

const int INF = 100000000;
const double PI = acos(-1.0), EPS = 1e-10;

int field[50][50];
int W, H;

void dfs(int y, int x) {

	queue<P> que;
	que.push(mp(y,x));
	while(!que.empty()) {
		P here = que.front();
		que.pop();
		for(int dy = -1; dy <= 1; dy++) for(int dx = -1; dx <= 1; dx++) {
			int ny = here.first + dy;
			int nx = here.second + dx;
			if(0 <= ny && ny < H && 0 <= nx && nx < W && field[ny][nx] == 1) {
				field[ny][nx] = 0;
				que.push(mp(ny, nx));
			}
		}
	}
}

int main() {
	for(; cin >> W >> H, W || H;) {

		memset(field, 0, sizeof field);
		rep(i,H) rep(j,W) cin >> field[i][j];
		int ans = 0;
		rep(i,H) rep(j,W) if(field[i][j]) {
			dfs(i,j);
			ans++;
		}
		cout << ans << endl;
	}
	return 0;
}

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

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

bool in(int x,int y) {
	return 0<=x&&x<w&&0<=y&&y<h;
}

void dfs(int x,int y) {
	m[x][y]=0;
	for(int i=1;i>=-1;i--) {
		for(int j=1;j>=-1;j--) {
			int nx=x+i,ny=y+j;
			if(in(nx,ny)&&m[nx][ny]) dfs(nx,ny);
		}
	}
}

int main() {
	while(scanf("%d %d",&w,&h),w||h) {
		for(int j=0;j<h;j++) {
			for(int i=0;i<w;i++) {
				scanf("%d",&m[i][j]);
			}
		}
		int count=0;
		for(int j=0;j<h;j++) {
			for(int i=0;i<w;i++) {
				if(m[i][j]) count++,dfs(i,j);
			}
		}
		printf("%d\n",count);
	}
}

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

#define SEA     ( 0)
#define LAND    ( 1)
#define VISITED (-1)

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

int dfs(std::vector<std::vector<int> >& field, const int width, const int height, const int x, const int y) {
  if (field[y][x] == VISITED || field[y][x] == SEA)
    return 0;
  field[y][x] = VISITED;
  for (int i = 0; i < 8; ++i) {
    int cx = x + dx[i], cy = y + dy[i];
    if (cx < 0 || cy < 0 || width <= cx || height <= cy)
      continue;
    dfs(field, width, height, cx, cy);
  }
  return 1;
}

int main(int argc, char const* argv[])
{
  while (true) {
    int w, h;
    std::cin >> w >> h;
    if (w == 0 && h == 0)
      break;
    std::vector<std::pair<int, int> > lands;
    int n_lands = 0;
    std::vector<std::vector<int> > field(h, std::vector<int>(w, 0));
    for (int y = 0; y < h; ++y) {
      for (int x = 0; x < w; ++x) {
        std::cin >> field[y][x];
        if (field[y][x] == LAND) {
          lands.push_back(std::make_pair<int, int>(x, y));
          ++n_lands;
        }
      }
    }
    int ans = 0;
    for (int i = 0; i < n_lands; ++i) {
      ans += dfs(field, w, h, lands[i].first, lands[i].second);
    }
    std::cout << ans << std::endl;
  }
  return 0;
}

C++: #include <bits/stdc++.h>
#define REP(i, n) for(int i = 0;i < n;i++)
#define REPR(i, n) for(int i = n;i >= 0;i--)
#define FOR(i, m, n) for(int i = m;i < n;i++)
#define FORR(i, m, n) for(int i = m;i >= n;i--)
#define SORT(v, n) sort(v, v+n);
#define VSORT(v) sort(v.begin(), v.end());
#define ll long long
#define pb(a) push_back(a)
#define INF 1000000000
using namespace std;
typedef pair<int, int> P;
typedef pair<ll, ll> LP;
typedef pair<int, P> PP;
typedef pair<ll, LP> LPP;

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

int g[50][50];
bool reach[50][50]={0};
int w,h;

void dfs(int x, int y){
 
  reach[x][y] = true;
  
  REP(i,8){
    int nx = x + dx[i], ny = y + dy[i];
    if(0<=nx && nx < h && 0<=ny && ny < w && !reach[nx][ny] && g[nx][ny] == 1) dfs(nx,ny);
  }
  return;
}

int main(){
  cin.tie(0);
  ios::sync_with_stdio(false);

  while(1){
    cin >> w >> h;
    if(!w && !h) return 0;
    
    REP(i,h){
      REP(j,w){
        reach[i][j] = false;
        cin >> g[i][j];
      }
    }
  
    int ans=0;

    REP(i,h){
      REP(j,w){
        if(g[i][j] == 1 && !reach[i][j]){
          dfs(i,j);
          ans++;
        }
      
      }
    }
    cout << ans << endl;
  }
    return 0;
}

C++: #include<stdio.h>
void printmap();
void draw(int x,int y);
int w,h,c[50][50];

int main(){
	int i,j;
	int count;


	scanf("%d %d",&w,&h);


	while((w!=0)&&(h!=0)){
		count=0;
	
		for(j=0;j<h;j++){
			for(i=0;i<w;i++){
				scanf("%d",&c[j][i]);
			}
		}
		//printf("\n");
		
		for(i=0;i<w;i++){
			c[h][i]=0;
		}
		for(i=0;i<h;i++){
			c[i][w]=0;
		}

		for(i=0;i<w;i++){
			for(j=0;j<h;j++){
				if(c[j][i]==1){
					count++;
					//printf("count up count=%d\n",count);
					draw(j,i);
					//printmap();
				}
			}
		}

		printf("%d\n",count);
		scanf("%d %d",&w,&h);
	}
	return 0;
}

void draw(int x,int y){
	//printf("delete %d %d\n",x,y);
	c[x][y]=0;
	if(c[x+1][y]==1){
		draw(x+1,y);
	}
	if(c[x-1][y]==1){
		draw(x-1,y);
	}
	if(c[x][y-1]==1){
		draw(x,y-1);
	}
	if(c[x][y+1]==1){
		draw(x,y+1);
	}

}

void printmap(){
	int i,j;
	for(j=0;j<h;j++){
		for(i=0;i<w;i++){
			printf("%d ",c[j][i]);
		}
		printf("\n");
	}
}