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>

using namespace std;

int idirs[] = {0, 0, 1, 1, 1, -1, -1, -1};
int jdirs[] = {1, -1, 0, 1, -1, 0, 1, -1};

int map[52][52];

void dfs(int i, int j) {
    if (!map[i][j]) return;
    map[i][j] = 0;
    for (int k = 0; k < 9; ++k) {
        dfs(i + idirs[k], j + jdirs[k]);
    }
}

int main() {
    while (1) {
        int w, h;

        cin >> w >> h;

        if (w == 0) break;

        for (int i = 0; i < 52; ++i)
            for (int j = 1; j < 52; ++j)
                map[i][j] = 0;

        for (int i = 1; i <= h; ++i) {
            for (int j = 1; j <= w; ++j) {
                cin >> map[i][j];
            }
        }

        int ans = 0;
        for (int i = 1; i <= h; ++i) {
            for (int j = 1; j <= w; ++j) {
                if (map[i][j] == 1) {
                    dfs(i, j);
                    ans++;
                }
            }
        }

        cout << ans << endl;
    }
}

C++: #include <bits/stdc++.h>
typedef long long ll;
using namespace std;
#define mp make_pair

const vector<int> dx = {-1, -1, -1, 0, 0, 1, 1, 1};
const vector<int> dy = {-1, 0, 1, -1, 1, -1, 0, 1};

int main() {
    while (true) {
        int w, h;
        cin >> w >> h;
        if (w == 0 && h == 0) break;
        vector<vector<int>> grid(h, vector<int>(w));
        for (int i = 0; i < h; ++i) {
            for (int j = 0; j < w; ++j) {
                cin >> grid[i][j];
            }
        }
        int ans = 0;
        while (true) {
            stack<pair<int, int>> sk;
            bool flag = false;
            for (int i = 0; i < h; ++i) {
                for (int j = 0; j < w; ++j) {
                    if (grid[i][j]) {
                        sk.push(mp(i, j));
                        flag = true;
                        break;
                    }
                }
                if (flag) break;
            }
            if (!flag) {
                cout << ans << endl;
                break;
            }
            while (!sk.empty()) {
                pair<int, int> v = sk.top();
                grid[v.first][v.second] = 0;
                sk.pop();
                for (int i = 0; i < 8; ++i) {
                    pair<int, int> nv = mp(v.first + dx[i], v.second + dy[i]);
                    if (nv.first < 0 || nv.first >= h || nv.second < 0 || nv.second >= w) continue;
                    if (grid[nv.first][nv.second] == 0) continue;
                    grid[nv.first][nv.second] = 0;
                    sk.push(mp(nv.first, nv.second));
                }
            }
            ans++;
        }
    }
}

C++: #include <bits/stdc++.h>
using namespace std;
#define FOR(i,a,b) for(int i=(a);i<(b);++i)
#define rep(i,n) FOR(i,0,n)
#define repr(i,n) for(int i=(n)-1;0<=i;--i)
#define each(e,v) for(auto&& e:(v))
#define all(v) begin(v),end(v)
#define dump(x) cerr<<#x<<": "<<(x)<<endl
#define INF 100000000
#define IINF 9999999999999;
using vint = vector<int>;
using ll = long long;
using vll = vector<ll>;
template <class T> void chmin(T& a, const T& b) { a = min(a, b); }
template <class T> void chmax(T& a, const T& b) { a = max(a, b); }

int prime[25] = {2, 3, 5, 7, 11, 13, 17, 19, 23, 29, 31, 37, 41, 43, 47, 53, 59, 61, 67, 71, 73, 79, 83, 89, 97};
long long int dp[101][20] = {0};

bool earth[51][51];

void erase_island(int h, int w) {
    earth[h][w] = false;
    //cout << "# " << h << " " << w << endl;
    if(earth[h - 1][w - 1]) erase_island(h - 1, w - 1);
    if(earth[h - 1][w]) erase_island(h - 1, w);
    if(earth[h - 1][w + 1]) erase_island(h - 1, w + 1);
    if(earth[h][w + 1]) erase_island(h, w + 1);
    if(earth[h + 1][w + 1]) erase_island(h + 1, w + 1);
    if(earth[h + 1][w]) erase_island(h + 1, w);
    if(earth[h + 1][w - 1]) erase_island(h + 1, w - 1);
    if(earth[h][w - 1]) erase_island(h, w - 1);
}

int main() {
    while(true) {
        int w, h;
        cin >> w >> h;
        if(w == 0 && w == 0) break;
        rep(i, h) rep(j, w) {
                int temp;
                cin >> temp;
                if(temp == 0) earth[i][j] = false;
                else earth[i][j] = true;
            }

        int ans = 0;
        rep(i, h) rep(j, w) {
                if(earth[i][j]) erase_island(i , j), ++ans;
            }

        cout << ans << endl;
    }

    return 0;
}

C++: #include<bits/stdc++.h>
#define _ ios_base::sync_with_stdio(0);cin.tie(0);
#define REP(i,n) for(int i=0;i<(int)(n);i++)

using namespace std;

int c[52][52];

bool dfs(int x,int y){
  if(c[x][y]==0) return false;
  c[x][y]=0;
  for(int i=-1;i<=1;i++){
    for(int j=-1;j<=1;j++){
      if((i|j)!=0&&c[x+i][y+j]!=0)
	dfs(x+i,y+j);
    }
  }
  return true;
}

int main(){ _;
  int w,h;
  while(cin>>w>>h,!(w==0&&h==0)){
    int count=0;
    REP(j,h+2)REP(i,w+2) c[i][j]=0;
    REP(j,h)REP(i,w) cin>>c[i+1][j+1];
    REP(j,h)REP(i,w)
      if(dfs(i+1,j+1))
	count++;
    cout<<count<<endl;
  }
}

C++: #include<iostream>
#include<vector>
using namespace std;
int dx[8]={0,1,0,-1,1,1,-1,-1};
int dy[8]={1,0,-1,0,1,-1,1,-1};

bool dfs(int sx,int sy,vector<vector<bool> >&c){
    if(c[sx][sy]==false) return false;
    c[sx][sy]=false;
    for(int i=0;i<8;i++){
        int nx=sx+dx[i];
        int ny=sy+dy[i];
        bool flag=dfs(nx,ny,c);
    }
    return true;
}

int main(void){
    int h,w,temp;
    while(1){
        cin>>w>>h;
        if(w==0) break;
        vector<vector<bool> > c(h+2,vector<bool>(w+2,false)) ;
        for(int i=1;i<=h;i++){
            for(int j=1;j<=w;j++){
                cin>>temp;
                if(temp==1) c[i][j]=true;
            }
        }
        int t=0;
        for(int i=1;i<=h;i++){
            for(int j=1;j<=w;j++){
                bool flag=dfs(i,j,c);
                if(flag) t++;
            }
        }
        cout<<t<<endl;
    }
    return 0;
}