Go: package main

import (
	"bufio"
	"fmt"
	"os"
	"strconv"
)

func getScanner(fp *os.File) *bufio.Scanner {
	scanner := bufio.NewScanner(fp)
	scanner.Split(bufio.ScanWords)
	scanner.Buffer(make([]byte, 1000005), 1000005)
	return scanner
}

func getNextString(scanner *bufio.Scanner) string {
	scanner.Scan()
	return scanner.Text()
}

func getNextInt(scanner *bufio.Scanner) int {
	i, _ := strconv.Atoi(getNextString(scanner))
	return i
}

func getNextInt64(scanner *bufio.Scanner) int64 {
	i, _ := strconv.ParseInt(getNextString(scanner), 10, 64)
	return i
}

func getNextUint64(scanner *bufio.Scanner) uint64 {
	i, _ := strconv.ParseUint(getNextString(scanner), 10, 64)
	return i
}

func getNextFloat64(scanner *bufio.Scanner) float64 {
	i, _ := strconv.ParseFloat(getNextString(scanner), 64)
	return i
}

func main() {
	fp := os.Stdin
	wfp := os.Stdout
	cnt := 0

	if os.Getenv("MASPY") == "ますピ" {
		fp, _ = os.Open(os.Getenv("BEET_THE_HARMONY_OF_PERFECT"))
		cnt = 0
	}
	if os.Getenv("MASPYPY") == "ますピッピ" {
		wfp, _ = os.Create(os.Getenv("NGTKANA_IS_GENIUS10"))
	}

	scanner := getScanner(fp)
	writer := bufio.NewWriter(wfp)
	for solve(scanner, writer) {

	}
	for i := 0; i < cnt; i++ {
		fmt.Fprintln(writer, "-----------------------------------")
		solve(scanner, writer)
	}
	writer.Flush()
}

func solve(scanner *bufio.Scanner, writer *bufio.Writer) bool {
	sol := solver{}
	sol.w = getNextInt(scanner)
	sol.h = getNextInt(scanner)
	if sol.w == 0 && sol.h == 0 {
		return false
	}
	sol.land = makeInts(sol.h, sol.w)
	for y := 0; y < sol.h; y++ {
		for x := 0; x < sol.w; x++ {
			sol.land[y][x] = getNextInt(scanner)
		}
	}
	ans := 0
	for y := 0; y < sol.h; y++ {
		for x := 0; x < sol.w; x++ {
			if sol.land[y][x] == 0 {
				continue
			}
			ans++
			sol.dfs(y, x)
		}
	}
	fmt.Fprintln(writer, ans)
	return true
}

type solver struct {
	w, h int
	land [][]int
}

func (sol *solver) dfs(y, x int) {
	if y < 0 {
		return
	}
	if y >= sol.h {
		return
	}
	if x < 0 {
		return
	}
	if x >= sol.w {
		return
	}
	if sol.land[y][x] == 0 {
		return
	}
	sol.land[y][x] = 0
	dy := [8]int{-1, -1, -1, 0, 0, 1, 1, 1}
	dx := [8]int{-1, 0, 1, -1, 1, -1, 0, 1}
	for i := 0; i < 8; i++ {
		sol.dfs(y+dy[i], x+dx[i])
	}
}
func makeInts(h, w int) [][]int {
	index := make([][]int, h, h)
	data := make([]int, h*w, h*w)
	for i := 0; i < h; i++ {
		index[i] = data[i*w : (i+1)*w]
	}
	return index
}

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;
}