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