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

int field[64][64];
bool visited[64][64];
int dx[] = {0, 0, 1, -1, 1, 1, -1, -1}, dy[] = {1, -1, 0, 0, 1, -1, 1, -1};

void rec(int y, int x)
{
	if (visited[y][x] == false){
		return;
	}
	visited[y][x] = false;
	for (int i = 0; i < 8; i++){
		rec(y + dy[i], x + dx[i]);
	}
	return;
}

int main(void)
{
	int w, h;
	while (cin >> w >> h, w && h){
		for (int i = 0; i < 64; i++){
			for (int j = 0; j < 64; j++){
				field[i][j] = 0;
				visited[i][j] = false;
			}
		}
		for (int i = 1; i <= h; i++){
			for (int j = 1; j <= w; j++){
				cin >> field[i][j];
				if (field[i][j] == 1){
					visited[i][j] = true;
				}
			}
		}
		int ret = 0;
		for (int i = 0; i < 64; i++){
			for (int j = 0; j < 64; j++){
				if (visited[i][j] == true){
					rec(i, j);
					ret++;
				}
			}
		}
		cout << ret << endl;
	}
	
	return 0;
}

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

int main()
{
	while (true)
	{
		int w, h;
		int num = 0;
		cin >> w >> h;
		if (w == 0 && h == 0)break;

		int **c;
		c = new int*[h + 2];
		for (int i = 0; i < h + 2; i++)	c[i] = new int[w + 2]; 
		for (int i = 0; i < h + 2; i++)				  
		{
			for (int l = 0; l < w + 2; l++)	c[i][l] = 0;
		}

		for (int i = 1; i < h + 1; i++)
		{
			for (int l = 1; l < w + 1; l++)	cin >> (c[i])[l];
		}
		for (int i = 1; i < h + 1; i++)
		{
			for (int l = 1; l < w + 1; l++)
			{
				if (c[i][l] == 1)		     
				{
					if (c[i + 1][l - 1] == 0 && c[i + 1][l] == 0 && c[i + 1][l + 1] == 0 && c[i][l + 1] == 0)
					{
						num++; 
					}

					else if ((c[i + 1][l - 1] == 1 && c[i + 1][l] == 0) && (c[i + 1][l + 1] == 1 || c[i][l + 1] == 1))	   
					{
						c[i][l] = 0;
						c[i + 1][l] = 1; 
					}
					else
					{
						c[i][l] = 0; 
					}

				}
			}
		}
		cout << num << endl;
	}
	return 0;
}

C++: #include <iostream>
#include <cstdio>
#include <queue>
#include <cstring>

using namespace std;

typedef pair<int, int> p;

int t_x[] = {1, 1, 1, 0, -1, -1, -1, 0};
int t_y[] = {1, 0, -1, -1, -1, 0, 1, 1};

int field[52][52];  

int main()
{
  while (true) {
    memset(field, 0, sizeof field);
    int width, height;
    scanf("%d %d", &width, &height);
    
    if (width == 0 && height == 0) break;
    
    for (int i = 1; i <= height; i++) {
      for (int j = 1; j <= width; j++) {
	scanf(" %d", &field[i][j]);
      }
    }
    
    queue<p> que;
    int c = 0;

    for (int i = 1; i <= height; i++) {
      for (int j = 1; j <= width; j++) {
	if (field[i][j] == 1) {
	  c++;
	  que.push(p(i, j));
	  field[i][j] = 0;
	  while (!que.empty()) {
	    p tmp = que.front();
	    que.pop();
	    //field[tmp.second][tmp.first] = 0;
	    //cout << tmp.first << ", " << tmp.second << endl;
	    for (int k = 0; k < 8; k++) {
	      int tx = tmp.second + t_x[k];
	      int ty = tmp.first + t_y[k];
	      //cout << ty << ", "<< tx << " =  "<< field[ty][tx] << endl;
	      if (field[ty][tx] == 1) {
		field[ty][tx] = 0;
		que.push(p(ty, tx));
	      }
	    }
	    //cout << endl;
	    //cout << i << " " << j << endl;
	    for (int k = 1; k <= height; k++) {
	      for (int l = 1; l <= width; l++) {
		//cout << field[k][l] << " ";
	      }
	      //cout << endl;
	    }
	    //cout << endl;
	  }
	}
      }     
    }
    cout << c << endl;
  }
  return 0;
}

C++: #include <bits/stdc++.h>
#include <math.h>
using namespace std;
int W,H;
char field[60][60];
int dx[8]{-1,0,1,-1,1,-1,0,1};
int dy[8]{1,1,1,0,0,-1,-1,-1};

void dfs(int x,int y){
    field[x][y] = '0';
    for(int i = 0;i < 8;i++){
        int nx = x + dy[i];
        int ny = y + dx[i];
        if(0 <= nx && nx < H && 0 <= ny && ny < W && field[nx][ny] == '1'){
            dfs(nx,ny);
        }
    }
    return;
}
int main(){
    for(int k = 0;1;k++){
    cin >> W >> H;
    if(H == 0 && W == 0){
        return 0;
    }
    for(int i = 0;i < H;i++){
        for(int j = 0;j < W;j++){
            cin >> field[i][j];
        }
    }
    int ans = 0;
    bool frag = 0;
    for(int i = 0;i < H;i++){
        for(int j = 0;j < W;j++){
            if(field[i][j] == '1'){
                ans++;
                dfs(i,j);
            }
        }
    }
    cout << ans  << endl;
    }
}