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>

void dfs(std::vector<std::vector<int> > &map, std::stack<std::pair<int, int> > &st, int &number) {
    int dy[8] = {-1, -1, -1, 0, 0, 1, 1, 1};
    int dx[8] = {-1, 0, 1, -1, 1, -1, 0, 1};
    std::pair<int, int>  coord;
    coord.first = st.top().first;
    coord.second = st.top().second;
    map[coord.first][coord.second] = 0;
    st.pop();
    for (int i = 0; i < 8; i++) {
        if (map[coord.first + dy[i]][coord.second + dx[i]] == 1) {
            st.push(std::make_pair(coord.first + dy[i], coord.second + dx[i]));
            dfs(map, st, number);
        }
    }
    return;
}

int main(void) {
    int w;
    int h;
    int number;
    while (std::cin >> w >> h) {
        if (w == 0 && h == 0) {
            break;
        }
        std::stack<std::pair<int, int> > st;
        number = 0;
        std::vector<std::vector<int> > map(h + 2, std::vector<int>(w + 2, 0));
        for (int i = 1; i <= h; i++) {
            for (int j = 1; j <= w; j++) {
                std::cin >> map[i][j];
            }
        }
        for (int i = 1; i <= h; i++) {
            for (int j = 1; j <= w; j++) {
                if (map[i][j] == 1) {
                    st.push(std::make_pair(i, j));
                    map[i][j] = 0;
                    dfs(map, st, number);
                    number++;
                }
            }
        }
        std::cout << number << std::endl;
    }
    return 0;
}

C++: #include <iostream>
#include <vector>
using namespace std;
int w, h;
vector<int> ans;
char c[55][55];

void dfs(int x, int y) {
    c[x][y] = '0';
    for (int dh = -1; dh <= 1; dh++) {
        for (int dw = -1; dw <= 1; dw++) {
            int nh = x + dh, nw = y + dw;
            if (0 <= nh && 0 <= nw && nh < h && nw < w && c[nh][nw] == '1') dfs(nh, nw);
        }
    }
    return;
}

int main() {
    while (1) {
        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];
        }
        int cnt = 0;
        for (int i = 0; i < h; i++) {
            for (int j = 0; j < w; j++) {
                if (c[i][j] == '1') { 
                    dfs(i, j);
                    cnt++;
                }
            }
        }
        ans.push_back(cnt);
    }
    for (int i = 0; i < ans.size(); i++) {
        cout << ans[i] << endl;
    }
}

C++: #include <iostream>
using namespace std;
int main(){
	int w,h,ct,num2;
	while(cin>>w>>h){
		ct=0;
		if(w==0&&h==0) break;
		int c[w+2][h+2];
		for(int i=0;i<w+2;i++){
			for(int j=0;j<h+2;j++){
				c[i][j]=0;
			}
		}
		for(int i=0;i<h;i++){
			for(int j=0;j<w;j++){
				cin>>c[j+1][i+1];
			}
		}
		for(int i=0;i<w+2;i++){
			for(int j=0;j<h+2;j++){
				if(c[i][j]==1){
					c[i][j]=2;
					do{
						num2=0;
						for(int ii=1;ii<w+1;ii++){
							for(int jj=1;jj<h+1;jj++){
								if(c[ii][jj]==2&&c[ii-1][jj-1]==1){
									c[ii-1][jj-1]=2;
									num2++;
								}
								if(c[ii][jj]==2&&c[ii-1][jj]==1){
									c[ii-1][jj]=2;
									num2++;
								}
								if(c[ii][jj]==2&&c[ii-1][jj+1]==1){
									c[ii-1][jj+1]=2;
									num2++;
								}
								if(c[ii][jj]==2&&c[ii][jj-1]==1){
									c[ii][jj-1]=2;
									num2++;
								}
								if(c[ii][jj]==2&&c[ii][jj+1]==1){
									c[ii][jj+1]=2;
									num2++;
								}
								if(c[ii][jj]==2&&c[ii+1][jj-1]==1){
									c[ii+1][jj-1]=2;
									num2++;
								}
								if(c[ii][jj]==2&&c[ii+1][jj]==1){
									c[ii+1][jj]=2;
									num2++;
								}
								if(c[ii][jj]==2&&c[ii+1][jj+1]==1){
									c[ii+1][jj+1]=2;
									num2++;
								}
							}
						}
					} while(num2>0);
					ct++;
					for(int ii=0;ii<w+2;ii++){
						for(int jj=0;jj<h+2;jj++){
							if(c[ii][jj]==2){
								c[ii][jj]=0;
							}
						}
					}
				}
			}
		}
		cout<<ct<<endl;		
	}
    return 0;
}

C++: #include <iostream>

using namespace std;

int w, h, cnt;
int island[50][50];
int dx[] = {1, 1, 0, -1, -1, -1, 0, 1};
int dy[] = {0, 1, 1, 1, 0, -1, -1, -1};

bool check(int nx, int ny){

  if(nx < 0 || w <= ny || ny < 0 || h < ny)return false;
  else return true;

}

void dfs(int sy, int sx){

  island[sy][sx] = 0;

  for(int i = 0 ; i < 8 ; i++){
    int nx = sx + dx[i];
    int ny = sy + dy[i];
    if(!check(nx, ny)) continue;
    if(island[ny][nx]) dfs(ny, nx);
  }
}

int main(void){

  while(cin >> w >> h, w + h){

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

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

C++: #include <iostream>
using namespace std;
void kazoeruyo(int p,int P,int box[10][10]){
  box[p][P]=0;
    if(box[p][P+1]==1) {
      box[p][P+1]=0;
      P=P+1; 
      kazoeruyo(p,P,box);
    }
    if(box[p][P-1]==1){
      box[p][P-1]=0;
      P=P-1;
      kazoeruyo(p,P,box);
    }
    if(box[p+1][P]==1){ 
      box[p+1][P]=0;
      p=p+1;
      kazoeruyo(p,P,box);
    }
    if(box[p-1][P]==1){
      box[p-1][P]=0;
      p=p-1;
      kazoeruyo(p,P,box);
    }
    if(box[p-1][P-1]==1){
      box[p-1][P-1]=0;
      p=p-1;
      P=P-1;
      kazoeruyo(p,P,box);
    }
    if(box[p-1][P+1]==1){
      box[p-1][P+1]=0;
      p=p-1;
      P=P+1;
      kazoeruyo(p,P,box);
    }
    if(box[p+1][P+1]==1){
      box[p+1][P+1]=0;
      p=p+1;
      P=P+1;
      kazoeruyo(p,P,box);
    }
    if(box[p+1][P-1]==1){
      box[p+1][P-1]=0;
      p=p+1;
      P=P-1;
      kazoeruyo(p,P,box);
    }	
}
int main(){
  int w,h,i,I,p,P,o,t,T,y,Y;
  int box[10][10];
  while(1){
    cin>>w>>h;
    if(w==0&&h==0) break;
    for(i=0;i<h;i++){
      for(I=0;I<w;I++){
	cin>>box[i][I];
      }
    }
    o=0;
    for(i=0;i<h;i++){
      for(I=0;I<w;I++){
	if(box[i][I]==1) {
	  p=i;
	  P=I;
	  kazoeruyo(p,P,box);
	  box[p][P]=1;
	}
      }
    }
    for(i=0;i<h;i++){
      for(I=0;I<w;I++){
	if(box[i][I]==1) o++;
      }
    }
    cout<<o<<endl;
    o=0;
  }
  return 0;
}