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 <algorithm>
#include <cmath>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <string>
#include <vector>
using namespace std;
typedef long long int ll;
typedef vector<int> vi;
struct pos {
  int row;
  int column;
};
#define rep(i, n) for (int i = 0; i < (n); ++i)
#define Sort(a) sort(a.begin(), a.end())
const int INF = 1 << 30;
const ll MOD = 1000000007;
vector<pair<int, int>> directions = {
    make_pair(0, 1),  make_pair(1, 0),   make_pair(1, 1),  make_pair(0, -1),
    make_pair(-1, 0), make_pair(-1, -1), make_pair(-1, 1), make_pair(1, -1),
};
void traverse(int grid[55][55], bool dp[55][55], int row, int column,
              int maxRow, int maxColumn) {
  dp[row][column] = true;

  for (auto direction : directions) {
    int newRow = row + direction.first;
    int newColumn = column + direction.second;

    if (newRow < 0 || newRow >= maxRow || newColumn < 0 ||
        newColumn >= maxColumn) {
      continue;
    }

    if (grid[newRow][newColumn] == 0) continue;
    if (dp[newRow][newColumn]) continue;

    traverse(grid, dp, newRow, newColumn, maxRow, maxColumn);
  }
}
int main(void) {
  vector<int> results;
  while (true) {
    bool dp[55][55] = {false};
    int grid[55][55] = {0};
    int ROW, COLUMN;
    cin >> COLUMN >> ROW;
    if (ROW == 0 && COLUMN == 0) break;
    rep(row, ROW) {
      rep(column, COLUMN) { cin >> grid[row][column]; }
    }

    int count = 0;
    rep(row, ROW) {
      rep(column, COLUMN) {
        if (grid[row][column] == 0) continue;
        if (dp[row][column]) continue;
        traverse(grid, dp, row, column, ROW, COLUMN);
        count++;
      }
    }

    results.push_back(count);
  }

  for (auto result : results) {
    cout << result << endl;
  }

  return 0;
}

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

int A[51][51];
int member[51][51];
int count = 0;

void check_member(int i,int j,int h, int w){
    if(member[i][j] == 0 && A[i][j] == 1 && i <= h && j <= w){
        member[i][j] = count;
        check_member(  i,  j+1 , h , w);
        check_member(i+1 , j+1 , h , w);
        check_member(i+1 ,   j , h , w);
        check_member(i+1 , j-1 , h , w);
        check_member(  i , j-1 , h , w);
        check_member(i-1 , j-1 , h , w);
        check_member(i-1 ,   j , h , w);
        check_member(i-1 , j+1 , h , w);
    }else{
        return;
    }
}


int main(){
    vector<int> result;
    int h,w;
    while(cin >> w >> h){
        if(h == 0 && w == 0) break;
        count = 0;
        for(int i = 0; i <= h; i++){
            for(int j = 0; j <= w; j++){
                if(i > 0 && j > 0){
                    cin >> A[i][j];
                }else{
                    A[i][j] = 0;
                }
                member[i][j] = 0;
            }
        }
        for(int i = 1; i <= h; i++){
            for(int j = 1; j <= w; j++){
                if(A[i][j]==1 && member[i][j] == 0){
                    count++;
                    check_member(i,j,h,w);
                }
            }
        }
        result.push_back(count);
    }
    for(int i = 0; i < result.size(); i++){
        cout << result[i] << endl;
    }
}

C++: #include <iostream>
#include <string>
#include <vector>
#include <queue>
#include <map>
#include <cmath>
#include <set>
#include <algorithm>

typedef long long ll;
const ll MOD = 1e9 + 7;
const long long INF = 1LL<<60;
const double PI=3.14159265358979323846;
const int NMAX=100005;
using namespace std;

int done[51][51];
int c[51][51];

void dfs(int i, int j, int w, int h){
    if(i < 0 || i >= h || j < 0 || j >= w) return;
    if(done[i][j] != 0) return;

    done[i][j] = 1;
    
    if(c[i][j] == 1){
        dfs(i-1,j-1,w,h);
        dfs(i-1,j,w,h);
        dfs(i-1,j+1,w,h);
        dfs(i,j-1,w,h);
        dfs(i,j+1,w,h);
        dfs(i+1,j-1,w,h);
        dfs(i+1,j,w,h);
        dfs(i+1,j+1,w,h);
    }
}

void solve(int w, int h){
    for(int i = 0; i < h; i++){
        for(int j = 0; j < w; j++){
            cin >> c[i][j];
            done[i][j] = 0;
        }
    }
    int ans = 0;
    for(int i = 0; i < h; i++){
        for(int j = 0; j < w; j++){
            if(done[i][j] == 0 && c[i][j] == 1){
                ans += 1;
                dfs(i,j,w,h);
            }
        }
    }
    cout << ans << endl;
}

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

    return 0;
}

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

using namespace std;

int c[52][52];

void er(int i,int j){
    c[i][j]--;
    if(c[i+1][j-1])
        er(i+1,j-1);
    if(c[i][j+1])
        er(i,j+1);
    if(c[i+1][j])
        er(i+1,j);
    if(c[i+1][j+1])
        er(i+1,j+1);
    if(c[i-1][j+1])
        er(i-1,j+1);
    if(c[i-1][j])
        er(i-1,j);
    if(c[i-1][j-1])
        er(i-1,j-1);
    if(c[i][j-1])
        er(i,j-1);
}

int main(void){
    int w,h;
    int i,j;
    int a;
    while(true){
        cin >> w >> h;
        if(w == 0){
            break;
        }
        for(i = 0;i < 52;i++){
            for(j = 0;j < 52;j++){
                c[i][j] = 0;
            }
        }
        for(i = 1;i <= w;i++){
            for(j = 1;j <= h;j++){
                cin >> c[i][j];
            }
        }
        a = 0;
        for(i = 1;i <= w;i++){
            for(j = 1;j <= h;j++){
                if(c[i][j]){
                    er(i,j);
                    a++;
                }
            }
        }
        cout << a << endl;
    }
}