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>
using namespace std;

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

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

void dfs(int x, int y){
	fd[y][x] = 0;
	for(int i=0; i<8; i++){
		int mx = x+dx[i];
		int my = y+dy[i];
		if(0<=mx && mx<w && 0<=my && my<h && fd[my][mx])
			dfs(mx, my);
	}
}

int main(void){
	while(cin >> w >> h, w|h){
		for(int y=0; y<h; y++)
			for(int x=0; x<w; x++)
				cin >> fd[y][x];

		int count=0;
		for(int y=0; y<h; y++)
			for(int x=0; x<w; x++)
				if(fd[y][x]){
					count++;
					dfs(x,y);
				}
		
		cout << count << endl;
	}

	return 0;
}

C++: #include <bits/stdc++.h>
#define rep(i,n) for(int i=0;i<n;i++)
#define all(x) (x).begin(),(x).end()
using namespace std;
const int INF=1145141919,MOD=1e9+7;
const long long LINF=8101145141919364364;
const int dx[]={1,1,0,-1,-1,-1,0,1},dy[]={0,-1,-1,-1,0,1,1,1};

int main(){
    int w,h;
    while(cin>>w>>h&&w){
        int c[h][w]; rep(i,h)rep(j,w) cin>>c[i][j];
        int cnt=1;
        for(int i=0;i<h;i++){
            for(int j=0;j<w;j++){
                if(c[i][j]==1){
                    cnt++;
                    queue<pair<int,int>> q;
                    c[i][j]=cnt;
                    q.push({i,j});
                    while(q.size()){
                        pair<int,int> p=q.front(); q.pop();
                        int cy=p.first,cx=p.second;
                        rep(k,8){
                            int ny=cy+dy[k],nx=cx+dx[k];
                            if(0<=ny&&ny<h&&0<=nx&&nx<w&&c[ny][nx]==1){
                                c[ny][nx]=cnt;
                                q.push({ny,nx});
                            }
                        }
                    }
                }
            }
        }
        cout<<cnt-1<<endl;
    }
    return 0;
}

C++: #include <iostream>
#include <vector>
#include <algorithm>
#include <cmath>
#define rep(i, n) for(int i = 0; i < (int)(n); ++i)
#define REP(i, n) for(int i = 1; i < (int)(n); ++i)
using namespace std;

int w, h;
vector<vector<int>> m(50, vector<int>(50));
vector<vector<bool>> searched(50, vector<bool>(50, false));
vector<int> dx = {1, 1, 1, 0, -1, -1, -1, 0};
vector<int> dy = {-1, 0, 1, 1, 1, 0, -1, -1};

void DFS(int i, int j) {
    if (i < 0 || h <= i || j < 0 || w <= j) return;
    if (m[i][j] == 0) return;
    if (searched[i][j]) return;
    
    searched[i][j] = true;
    rep(k, 8) DFS(i + dx[k], j + dy[k]);
    return;
}

int main() {
    while (true) {
        cin >> w >> h;
        if (w == 0 && h == 0) break;
        rep(i, h) rep(j, w) cin >> m[i][j];
        rep(i, h) rep(j, w) searched[i][j] = false;
        
        int cnt = 0;
        rep(i, h) rep(j, w) {
            if (m[i][j] == 0) continue;
            if (searched[i][j]) continue;
            ++cnt;
            DFS(i, j);
        }
        
        cout << cnt << endl;
    }
    return 0;
}

C++: #include<iostream>
using namespace std;
void dfs(int,int);
int a[50][50]={};

int main(){
  while(1){
    int count=0,w,h;
    cin>>w>>h;
    if(w+h==0)break;
    
    for(int i=0;i<h;i++)
      for(int j=0;j<w;j++)
	cin>>a[i][j];

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

  void dfs(int x,int y){
    a[y][x]=0;
    int nx,ny;
    int dx[8]={1,1,1,0,-1,-1,-1,0};
    int dy[8]={1,0,-1,-1,-1,0,1,1};
    for(int k=0;k<8;k++){
	nx=x+dx[k];
	ny=y+dy[k];

	if(nx>=0&&nx<50&&ny>=0&&ny<50&&a[ny][nx]==1)
	  dfs(nx,ny);
    }
  }

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 dx[8] = {0, 1, 1, 1, 0, -1, -1, -1};
int dy[8] = {1, 1, 0, -1, -1, -1, 0, 1};
int field[50][50] = {0};

void dfs(int x, int y){
	field[y][x] = 3;

	for(int i = 0; i < 8; i++){
		int nx = x + dx[i];
		int ny = y + dy[i];
		if(nx >= 0 and nx <= w-1 and ny >= 0 and ny <= h and field[ny][nx]==1){
			dfs(nx, ny);
		}
	}
	return;
}

int main(){
	while(1){
		cin >> w >> h;
		if(w==0 and h==0){
			return 0;
		}
		for(int i = 0; i < h; i++){
			for(int j = 0; j < w; j++){
				cin >> field[i][j];
			}
		}

		int count = 0;

		for(int i = 0; i < h; i++){
			for(int j = 0; j < w; j++){
				if(field[i][j]==1){
					dfs(i,j);
					count++;
				}
			}
		}
		cout << count << endl;
	}
}

C++: #include<iostream>
#include<algorithm>
#include<set>
#include<map>
#include<vector>
#include<queue>
#include<cstring>
#include<cmath>
#include<cstdio>
#include<cstdlib>
using namespace std;
#define REP(i,a,n) for(int i = a ; i < n ; i++)
#define rep(i,n) REP(i,0,n)

typedef long long ll;

int w,h;
int t[52][52];
int dx[] = {-1,0,1,-1,1,-1,0,1};
int dy[] = {-1,-1,-1,0,0,1,1,1};


void solve(int x,int y){
  t[y][x] = 0;
  rep(i,8){
    int nx = x + dx[i];
    int ny = y + dy[i];
    if(w>nx && nx>=0 && h>ny && ny>=0 && t[ny][nx]){
      solve(nx,ny);
    }
  }
}

int main(){
  while(cin>>w>>h,w||h){
    rep(i,h){
      rep(j,w){
        cin>>t[i][j];
      }
    }

    int cnt = 0;
    rep(i,h){
      rep(j,w){
        if(t[i][j]){
          solve(j,i);
          cnt++;
        }
      }
    }
    cout<<cnt<<endl;
  }
}