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<vector>
#include<stack>
#include<queue>
#include<utility>
using namespace std;
bool find1(vector< vector<int> >& C,vector< vector<int> >& check,int* findh,int* findw)
{
  for(int i = 0;i < C.size();++i)
    for(int j = 0;j < C[0].size();++j)
      if(C[i][j] == 1 && check[i][j] == 0){
	*findh = i;
	*findw = j;
	return true;
      }
  return false;
}
bool find2(vector< vector<int> >& C,vector< vector<int> >& check,int* findh,int* findw)
{
  if(C[*findh - 1][*findw - 1] == 1 && check[*findh - 1][*findw - 1] == 0){
    *findh -= 1;
    *findw -= 1;
    return true;
  }
  else if(C[*findh - 1][*findw] == 1 && check[*findh - 1][*findw] == 0){
    *findh -= 1;
    return true;
  }
  else if(C[*findh - 1][*findw + 1] == 1  && check[*findh - 1][*findw + 1] == 0){
    *findh -= 1;
    *findw += 1;
    return true;
  }
  else if(C[*findh][*findw - 1] == 1 && check[*findh][*findw - 1] == 0){
    *findw -= 1;
    return true;
  }
  else if(C[*findh][*findw + 1] == 1 && check[*findh][*findw + 1] == 0){
    *findw += 1;
    return true;
  }
  else if(C[*findh + 1][*findw - 1] == 1 && check[*findh + 1][*findw - 1] == 0){
    *findh += 1;
    *findw -= 1;
    return true;
  }
  else if(C[*findh + 1][*findw] == 1 && check[*findh + 1][*findw] == 0){
    *findh += 1;
    return true;
  }
  else if(C[*findh + 1][*findw + 1] == 1 && check[*findh + 1][*findw + 1] == 0){
    *findh += 1;
    *findw += 1;
    return true;
  }
  else return false;
}
int main()
{
  int w,h,findh,findw;
  stack< pair<int,int> > finish;
  queue<int> answer;
  while(cin >> w >> h && !(w == 0 && h == 0)){
    int howmany = 0;
    vector< vector<int> > C(h + 2,vector<int>(w + 2,0)),check(h + 2,vector<int>(w + 2,0)); 
    for(int i = 1;i <= h;++i)
      for(int j = 1;j <= w;++j)
	cin >> C[i][j];
    while(find1(C,check,&findh,&findw)){
      check[findh][findw] = 1;
      finish.push(make_pair(findh,findw));
      while(!finish.empty()){
	findh = finish.top().first;
	findw = finish.top().second;
	while(find2(C,check,&findh,&findw)){
	  finish.push(make_pair(findh,findw));
	  check[findh][findw] = 1;
	}
	finish.pop();
      }
      ++howmany;
    }
    answer.push(howmany);
  }
  for(;!answer.empty();answer.pop())
    cout << answer.front() << endl;
}

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

using namespace std;

void del(int x, int y);

vector< vector<int> > arr;
int w, h;

int main(){
	
	while(cin >> w >> h, w | h){
		int ans = 0;
		arr.resize(h);
		for(int i = 0; i < h; i++){
			arr[i].resize(w);
		}
		
		for(int i = 0; i < h; i++){
			for(int j = 0; j < w; j++){
				cin >> arr[i][j];
			}
		}
		
		for(int i = 0; i < h; i++){
			for(int j = 0; j < w; j++){
				if(arr[i][j] == 1){
					del(i, j);
					ans++;
				}
			}
		}
				
		
		cout << ans << endl;
		arr.clear();
	}
	
	return 0;
	
}

void del(int x, int y){
	arr[x][y] = 0;	
	
	if(x + 1 < h && arr[x + 1][y] == 1){
		del(x + 1, y);
	}
	if(y + 1 < w && arr[x][y + 1] == 1){
		del(x, y + 1);
	}
	if(x - 1 > -1 && arr[x - 1][y] == 1){
		del(x - 1, y);
	}
	if(y - 1 > -1 && arr[x][y - 1] == 1){
		del(x, y - 1);
	}
	if(x + 1 < h && y + 1 < w && arr[x + 1][y + 1] == 1){
		del(x + 1, y + 1);
	}
	if(x - 1 > -1 && y + 1 < w && arr[x - 1][y + 1] == 1){
		del(x - 1, y + 1);
	}
	if(x - 1 > -1 && y - 1 > -1 && arr[x - 1][y - 1] == 1){
		del(x - 1, y - 1);
	}
	if(x + 1 < h && y - 1 > -1 && arr[x + 1][y - 1] == 1){
		del(x + 1, y - 1);
	}
	
	return;
}

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

int field[52][52];
int vw[8] = {-1,0,1,-1,1,-1,0,1};
int vh[8] = {-1,-1,-1,0,0,1,1,1};
void search(int i,int j)
{
	field[i][j] = 0;
	for(int k=0;k<8;k++){
		if(field[i+vw[k]][j+vh[k]] == 1)
			search(i+vw[k],j+vh[k]);
	}
}

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

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

C++: #include<cstdio>
using namespace std;
void func(int,int);
int a,b;
int sima[51][51];
int cnt;
int main(void)
{
	int i,j;
	scanf("%d",&a);
	scanf("%d",&b);
	for(i=0;i<a;i++){
		for(j=0;j<b;j++){
			scanf("%d",&sima[i][j]);
		}
	}
	for(i=0;i<a;i++){
		for(j=0;j<b;j++){
			if(sima[i][j]==1){
				func(i,j);
				cnt++;
			}
		}
	}
	printf("%d\n",cnt);
	return 0;
}
void func(int y,int x)
{
	if(y-1>=0){
		if(sima[y-1][x]==1){
			func(y-1,x);
		}
	}
	if(x-1>=0){
		if(sima[y][x-1]==1){
			func(y,x-1);
		}
	}
	if(y+1<=a){
		if(sima[y+1][x]==1){
			func(y+1,x);
		}
	}
	if(x+1<=b){
		if(sima[y][x+1]==1){
			func(y,x+1);
		}
	}
	if(y-1>=0 && x+1<=b){
		if(sima[y-1][x+1]==1){
			func(y-1,x+1);
		}
	}
	if(y-1>=0 && x-1>=0){
		if(sima[y-1][x-1]==1){
			func(y-1,x-1);
		}
	}
	if(y+1<=a && x+1<=b){
		if(sima[y+1][x+1]){
			func(y+1,x+1);
		}
	}
	if(y+1<=a && x-1>=0){
		if(sima[y+1][x-1]){
			func(y+1,x-1);
		}
	}
}

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

int w, h;
vector< vector<int> > c;

void Dfs(int i, int j) {
  c[i][j] = 0;
  for (int di = -1; di <= 1; ++di) {
    for (int dj = -1; dj <= 1; ++dj) {
      if (di == 0 && dj == 0)
        continue;

      int ni = i + di, nj = j + dj;
      if (ni < 0 || ni >= h || nj < 0 || nj >= w) continue;
      if (c[ni][nj]) Dfs(ni, nj);
    }
  }
}

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

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

    c.resize(h, vector<int>(w));
    for (int i = 0; i < h; ++i)
      for (int j = 0; j < w; ++j)
        cin >> c[i][j];

    int ans = 0;
    for (int i = 0; i < h; ++i) {
      for (int j = 0; j < w; ++j) {
        if (c[i][j]) {
          Dfs(i, j);
          ++ans;
        }
      }
    }
    c.clear();

    cout << ans << endl;
  }
}