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 <string>
#include <cstring>
#include <fstream>
#include <random>
#include <cmath>
#include <iomanip>
#include <climits>
#include <cstdlib>
#include <algorithm>
#include <vector>
#include <map>
#include <deque>
#include <map>
#include <set>
#include <queue>
#include <stack>
#include <list>
#include <unordered_map>
#include <bitset>
#include <sstream>
#include <new>
#include <typeinfo>
#include <iterator>

typedef long long ll;
typedef unsigned long long ull;
constexpr ll mop = 1000000007;
constexpr ll mop2 = 998244353;

using namespace std;

void dfs(const ll& x, const ll& y, vector<vector<bool>>& grid, const ll& h, const ll & w) {
	grid[x][y] = false;
	for (ll dx = -1; dx <= 1; dx++) {
		for (ll dy = -1; dy <= 1; dy++) {
			ll nextx = x + dx;
			ll nexty = y + dy;
			if (0 <= nextx && nextx < h && 0 <= nexty && nexty < w && grid[nextx][nexty] == true) {
				dfs(nextx, nexty, grid, h, w);
			}
		}
	}
}

int main() {
	while (true) {
		ll w, h;
		cin >> w >> h;
		if (w == 0 && h == 0) {
			break;
		}
		vector<vector<bool>> grid(h);
		for (ll i = 0; i < h; i++) {
			grid[i].resize(w);
			for (ll j = 0; j < w; j++) {
				ll tmp;
				cin >> tmp;
				grid[i][j] = (tmp == 1 ? true : false);
			}
		}
		ll ans = 0;
		for (ll i = 0; i < h; i++) {
			for (ll j = 0; j < w; j++) {
				if (grid[i][j] == true) {
					ans++;
					dfs(i, j, grid, h, w);
				}
			}
		}
		cout << ans << endl;
	}
}

C++: #include <iostream>
#include <string>
#include <utility>
#include <stack>
#include <vector>
#include <queue>
#include <algorithm>
#include <map>
#include <climits>
#include <set>
#include <numeric>
#include <iomanip>
#include <cmath>
#include <cstring>
using namespace std;
int grid[50][50];
bool used[50][50];
int move_h[8] = {-1, 1, 0, 0, -1, 1, -1, 1};
int move_w[8] = {0, 0, -1, 1, 1, -1, -1, 1};

void dfs(int cur_h, int cur_w, int h, int w){
  used[cur_h][cur_w] = true;
  for(int i = 0; i < 8; i++){
    int next_h = cur_h + move_h[i];
    int next_w = cur_w + move_w[i];

    if(0 <= next_h && next_h < h && 0 <= next_w && next_w < w 
        && !used[next_h][next_w] && grid[next_h][next_w] == 1){
      dfs(next_h, next_w, h, w);
    }
  }
}
int main(){
  while(true){
    int w;
    int h;
    cin >> w >> h;
    if(w == 0 && h == 0){
      break;
    }
    for(int i = 0; i < h; i++){
      for(int j = 0; j < w; j++){
        cin >> grid[i][j];
        used[i][j] = false;
      }
    }

    int ans = 0;
    for(int i = 0; i < h; i++){
      for(int j = 0; j < w; j++){
        if(!used[i][j] && grid[i][j] == 1){
          ans ++;
          dfs(i, j, h, w);
        }
      }
    }
    cout << ans << endl;
  }
  return 0;
}

C++: #include<iostream>
using namespace std;
void dfs(int,int);
int box[51][51];
int main(){
  int w,h,i,I,a;
  while(1){
    a=0;
    cin>>w>>h;
    if(w==0&&h==0) break;
    for(i=0;i<=h+1;i++){
      for(I=0;I<=w+1;I++){
	box[i][I]==0;
      }
    }
    for(i=1;i<=h;i++){
      for(I=1;I<=w;I++){
	cin>>box[i][I];
      }
    }
    for(i=1;i<=h;i++){
      for(I=1;I<=w;I++){
	if(box[i][I]==1){
	  dfs(i,I);
	  a++;
	}
      }
    }
    cout<<a<<endl;
  }
  return 0;
}
  void dfs(int i,int I){
  if(box[i][I]==0) return;
  box[i][I]=0;
  dfs(i,I+1);
  dfs(i+1,I+1);
  dfs(i+1,I);
  dfs(i+1,I-1);
  dfs(i,I-1);
  dfs(i-1,I-1);
  dfs(i-1,I);
  dfs(i-1,I+1);
  }

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

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

bool ok=true;
int c[60][60];
int w,h;
bool seen[60][60];
int dx[8]={1,-1,0,0,1,-1,1,-1};
int dy[8]={1,-1,1,-1,0,0,-1,1};

void dfs(int v,int u){
	if(!seen[v][u]){
		seen[v][u]=true;
		for(int i=0;i<8;i++){
			if(v+dx[i]>=0&&v+dx[i]<h&&u+dy[i]>=0&&u+dy[i]<w&&c[v+dx[i]][u+dy[i]]==1)dfs(v+dx[i],u+dy[i]);
		}
	}
	return;
}

int main(){
	while(ok){
		cin>>w>>h;
		if(w==0||h==0){
			ok=false;
			return 0;
		}
		for(int i=0;i<h;i++){
			for(int j=0;j<w;j++){
				cin>>c[i][j];
			}
		}
		for(int i=0;i<60;i++){
			for(int j=0;j<60;j++)seen[i][j]=false;
		}
		int ans=0;
		for(int i=0;i<h;i++){
			for(int j=0;j<w;j++){
				if(c[i][j]==1&&!seen[i][j]){
					ans++;
					dfs(i,j);
				}
			}
		}
		cout<<ans<<endl;
	}
	return 0;
}