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 H, W, C[50][50];

void dfs(int a, int b) {
    C[a][b] = 0;
    for (int i = -1; i <= 1; ++i) {
        for (int j = -1; j <= 1; ++j) {
            int na = a + i, nb = b + j;
            if (na >= 0 && na < H && nb >= 0 && nb < W && C[na][nb] == 1) dfs(na, nb);
        }
    }
    return;
}

int main() {
    while (true) {
        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 ans = 0;
        for (int i = 0; i < H; ++i) {
            for (int j = 0; j < W; ++j) {
                if (C[i][j]) {
                    dfs(i, j);
                    ++ans;
                }
            }
        }

        cout << ans << endl;
    }
    
    return 0;
}

C++: #include<cstdio>
#include<math.h>
#include<algorithm>
#include<vector>
#include<queue>
 
using namespace std;
#define rep(i,n) for(int i=0;i<n;i++)
#define INF 1001001001
#define LLINF 1001001001001001001
#define mp make_pair
#define LLIandI pair<long long int , int>
#define ll long long

int main(){
    while(1){
    int W,H;
    scanf("%d %d\n",&W,&H);
    if(W==0)break;
    int c[100][100];//W,H<=50
    bool f[100][100];
    rep(i,H){
        rep(j,W){
            scanf("%d",&c[i][j]);
            f[i][j]=false;
        }
    }
    queue< pair<int,int> > q;
    int count=0;
    rep(i,H)rep(j,W){
        if(f[i][j])continue;
        if(c[i][j]==0)continue;
        count++;
        //printf("x=%d y=%d",i,j);
        q.push( mp(i,j) );
        while(!q.empty()){
            pair<int,int> p=q.front();
            q.pop();
            int py=p.first;int px=p.second;
            if(f[py][px])continue;
            if(c[py][px]==0)continue;
            f[py][px]=true;
            if(py>0)q.push( mp(py-1,px) );
            if(py<H-1)q.push( mp(py+1,px) );
            if(px>0)q.push( mp(py,px-1) );
            if(px<W-1)q.push( mp(py,px+1) );
            if(py>0&&px>0)q.push( mp(py-1,px-1));
            if(py>0&&px<W-1)q.push( mp(py-1,px+1));
            if(py<H-1&&px>0)q.push( mp(py+1,px-1));
            if(py<H-1&&px<W-1)q.push( mp(py+1,px+1));

        }
    }
    
    printf("%d\n",count);
    }
}

C++: #include <bits/stdc++.h>
#include<unordered_map>
#include<unordered_set>
using namespace std;
using ld = long double;
template<class T>
using Table = vector<vector<T>>;

int dx8[8] = { -1,-1,-1, 0, 1, 1, 1, 0 };
int dy8[8] = { -1, 0, 1, 1, 1, 0,-1,-1 };

vector<vector<int>>as;
int w,h;

void check(int y, int x) {
    for (int i = 0; i < 8; ++i) {
        int nx = x + dx8[i];
        int ny = y + dy8[i];
        if (as[ny][nx]) {
            as[ny][nx] = 0;
            check(ny, nx);
        }
    }
}

int main() {
    while (1) {
        cin >> w >> h;
        if (!w)break;
        vector<vector<int>>as2(h+2);
        as = as2;
        for (int i = 0; i <= h+1; ++i) {
            as[i].emplace_back(0);
            if (i != 0 && i != h + 1) {
                for (int j = 0; j < w; ++j) {
                    int c; cin >> c;
                    as[i ].emplace_back(c);
                }
            }
            else {
                for (int j = 0; j < w; ++j) {
                    as[i].emplace_back(0);
                }
            }
            
            as[i].emplace_back(0);
        }
        int ans = 0;
        for (int i = 1; i <= h; ++i) {
            for (int j = 1; j <= w; ++j) {
                if (as[i][j]) {
                    ans++;
                    check(i, j);
                }
            }
        }
        cout << ans << endl;
    }
}

C++: #include <algorithm>
#include <complex>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <cctype>
#include <cmath>
#include <cassert>
#include <iostream>
#include <map>
#include <queue>
#include <sstream>
#include <string>
#include <stack>
#include <set>
#include <vector>

using namespace std;

#define all(c) ((c).begin()), ((c).end())
#define dump(c) cerr << "> " << #c << " = " << (c) << endl;
#define iter(c) __typeof((c).begin())
#define tr(i, c) for (iter(c) i = (c).begin(); i != (c).end(); i++)
#define REP(i, a, b) for (int i = a; i < (int)(b); i++)
#define rep(i, n) REP(i, 0, n)
#define mp make_pair
#define fst first
#define snd second
#define pb push_back

typedef long long ll;
typedef vector<int> vi;
typedef vector<double> vd;
typedef pair<int,int> pii;

const int INF = 1 << 29;
const double EPS = 1e-10;

int W, H;
int field[52][52];

void dfs(int x, int y) {
  field[y][x] = 0;

  REP(dx, -1, 2) REP(dy, -1, 2) {
    int tx = x + dx;
    int ty = y + dy;
    if (tx < 0 || tx >= W || ty < 0 || ty >= H) continue;
    if (field[ty][tx]) dfs(tx, ty);
  }
}

int main() {
  for(; cin >> W >> H, (W | H) != 0; ) {

    rep(i, H) rep(j, W) cin >> field[i][j];

    int ans = 0;
    rep(i, H) rep(j, W) if (field[i][j]) {
      dfs(j, i);
      ans++;
    }

    cout << ans << endl;
  }
  
  
  return 0;
}

C++: #include <bits/stdc++.h>
using namespace std;
using ll=long long;
typedef pair<int,int> P;
template<class T> inline bool chmax(T& a, T b) { if (a < b) { a = b; return 1; } return 0; }
template<class T> inline bool chmin(T& a, T b) { if (a > b) { a = b; return 1; } return 0; }
int w,h;
int nx,ny;
void dfs(vector<vector<int>> &G,int a,int b){
    G.at(a).at(b)=0;
    
    for(int k=-1;k<=1;k++){
        for(int l=-1;l<=1;l++){
            nx=a+k;
            ny=b+l;
        if(0<=nx&& nx<h && 0<=ny&& ny<w && G.at(nx).at(ny)==1) dfs(G,nx,ny);
        }
    }
    return ;
}



int main(){
    int w,h;
    cin>>w>>h;
    vector<vector<int>>G(h,vector<int>(w));
    for(int i=0;i<h;i++){
        for(int j=0;j<w;j++){
            cin>>G.at(i).at(j);
        }
    }
    int count=0;
    for(int i=0;i<h;i++){
        for(int j=0;j<w;j++){
            if(G.at(i).at(j)==1){
                dfs(G,i,j);
                count++;
            }
        }
    }
cout<<count<<endl;
}