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 <stdio.h>
int m[51][51]={};
void dfs(int i,int j){
 m[i][j]=1;
 if(m[i+1][j]==2)dfs(i+1,j);
 if(m[i+1][j+1]==2)dfs(i+1,j+1);
 if(m[i][j+1]==2)dfs(i,j+1);
 if(m[i-1][j+1]==2)dfs(i-1,j+1);
 if(m[i-1][j]==2)dfs(i-1,j);
 if(m[i-1][j-1]==2)dfs(i-1,j-1);
 if(m[i][j-1]==2)dfs(i,j-1);
 if(m[i+1][j-1]==2)dfs(i+1,j-1);
}

int main(){
  int cou,i,j,y,t,a;
 while(1){
   cou=0;
   scanf("%d %d",&y,&t);
   if(y==0&&t==0)break;
   for(i=1;i<=t;i++){
     for(j=1;j<=y;j++){
	scanf("%d",&a);
	a++;
	m[i][j]=a;
     }
   }
   for(i=1;i<=t;i++){
     for(j=1;j<=y;j++){
	if(m[i][j]==2){
	  cou++;
	  dfs(i,j);
	}
     }
   }
   printf("%d\n",cou);
 }
 return 0;
}

C++: //INCLUDE
//------------------------------------------
#include <bits/stdc++.h>
//DEFINE
//------------------------------------------
#define ll long long
#define ld long double
#define ALLv(a)  (a).begin(),(a).end()
#define ALL(a,n)  (a),(a)+n
#define vi vector<long long>
#define vd vector<long double>
#define vs vector<string>
#define dcml(n) fixed<<setprecision(n)
//CONST
//------------------------------------------
const ll INF=1010000000000000017LL;
const ll MOD=1000000007LL;
const ld EPS=1e-12;
const ld PI=3.14159265358979323846;
//REPEAT
//------------------------------------------
#define   FOR(i,m,n) for(ll (i)=(m);   (i)<(n);  (i)++)
#define  FORS(i,m,n) for(ll (i)=(m);   (i)<=(n); (i)++)
#define  RFOR(i,m,n) for(ll (i)=(n)-1; (i)>=(n); (i)--)
#define RFORS(i,m,n) for(ll (i)=(n);   (i)>(n);  (i)--)
#define   REP(i,n)   for(ll (i)=0;     (i)<(n);  (i)++)
#define  REPS(i,x)   for(ll (i)=1;     (i)<=(x); (i)++)
#define  RREP(i,x)   for(ll (i)=(x)-1; (i)>=0;   (i)--)
#define RREPS(i,x)   for(ll (i)=(x);   (i)> 0;   (i)--)
#define  WREP(i,in,j,jn) REP(i,in)REP(j,jn)
#define WREPS(i,in,j,jn) REPS(i,in)REPS(j,jn)
//-----------------------------------------
//namespace
using namespace std;
//ライブラリはここに
//↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓
vi dx8={-1,-1,-1,0,0,1,1,1};
vi dy8={-1,0,1,-1,1,-1,0,1};
//-----------------------------------------
ll w,h;
int c[50][50];
bool is;
void dfs(int x,int y){
    if(c[x][y]==0)return;
    c[x][y]=0;
    //cout<<x<<y<<"->";
    is=true;
    REP(i,8){
        int X=x+dx8[i],Y=y+dy8[i];
        if(0<=X&&X<h and 0<=Y&&Y<w){
            if(c[X][Y]==0)continue;
            dfs(X,Y);
        }
    }
}
int main(void){
    while(cin>>w>>h and !(w==0&&h==0) ){
        //cout<<"w:"<<w<<"h:"<<h<<"\n";
        int cnt=0;
        REP(i,h)REP(j,w)cin>>c[i][j];
        REP(i,h)REP(j,w){
            is=false;
            dfs(i,j);//cout<<"\n";
            if(is)cnt++;
        }
        cout<<cnt<<"\n";
    }
}

C++: #include <iostream>
using namespace std;
int w,h;
int in[100][100];
bool seen[100][100];
int ans =0;
bool flag = false;

void solve(int i,int j){
    //cout << i << " " << j << endl;
    if(i < 0 || j < 0 || i >= h || j >= w){
        return;
    }
    if(seen[i][j]){
        return;
    }
    if(in[i][j] == 0){
        return;
    }
    seen[i][j] = true;
    solve(i-1,j-1);
    solve(i-1,j);
    solve(i-1,j+1);
    solve(i,j-1);
    solve(i,j+1);
    solve(i+1,j-1);
    solve(i+1,j);
    solve(i+1,j+1);

}
int main(){
    cin >> w >> h;
    for(int i=0;i<h;i++){
        for(int j=0;j<w;j++){
            cin >> in[i][j];
        }
    }
    for(int i=0;i<h;i++){
        for(int j=0;j<w;j++){
            if(seen[i][j] == true || in[i][j] == 0){
            }
            else{
                solve(i,j);
                ans++;
            }
            
        }
    }
    if(w == 0 || h == 0){
    }
    else{
        cout << ans << endl;
    }
    return 0;
}

C++: #include <bits/stdc++.h>
using namespace std;
#ifdef ENABLE_DEBUG_OUTPUT
#define DEBUG_LOG(s) cout << s << endl;
#else
#define DEBUG_LOG(s) void();
#endif

int h, w;
vector<vector<bool>> c;
vector<vector<bool>> v;
const int dx[8] = { 0, 1, 1,  1,  0, -1, -1, -1 };
const int dy[8] = { 1, 1, 0, -1, -1, -1,  0,  1 };

bool dfs(const int x, const int y) {
    // cout << x << " " << y << " " << c[x][y] << " " << v[x][y] << endl;
    if(not c[x][y]) return false; // sea
    if(v[x][y]) return false; // already visited
    v[x][y] = true;

    for (auto i = 0; i < 8; i++) {
        int nx = x + dx[i], ny = y + dy[i];
        if(nx < 0 or nx >= h or ny < 0 or ny >= w) continue;
        dfs(x + dx[i], y + dy[i]);
    }

    return true;
}

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

        c.assign(h, vector<bool>(w, false));
        for (auto i = 0; i < h; i++) {
            for (auto j = 0; j < w; j++) {
                int val; cin >> val;
                c[i][j] = static_cast<bool>(val); // true : land, false : sea
            }
        }

        int ans = 0;
        v.assign(h, vector<bool>(w, false));
        for (auto i = 0; i < h; i++) {
            for (auto j = 0; j < w; j++) {
                if(dfs(i, j)) ans++;
            }
        }

        cout << ans << endl;
    }

    return 0;
}

C++: #include <iostream>

using namespace std;

int w, h, cnt;
int island[51][51];
int dx[] = {1, 1, 0, -1, -1, -1,  0,  1};
int dy[] = {0, 1, 1,  1,  0, -1, -1, -1};

bool check(int nx, int ny){

  if(nx < 0 || ny < 0 || w <= nx || h <= ny)return false;
  else return true;

}

void dfs(int sy, int sx){

  island[sy][sx] = 0;

  for(int i = 0 ; i < 8 ; i++){
    int nx = sx + dx[i];
    int ny = sy + dy[i];
    if(!check(nx, ny)) continue;
    if(island[ny][nx]) dfs(ny, nx);
  }
}

int main(void){

  while(cin >> w >> h, w || h){

    for(int i = 0 ; i < h ; i++)
      for(int j = 0 ; j < w ; j++)
	cin >> island[i][j];

    int cnt = 0;
    for(int i = 0 ; i < h ; i++){
      for(int j = 0 ; j < w ; j++){
	if(island[i][j]){
	  dfs(i, j);
	  cnt++;
	}
      }
    }
    cout << cnt << endl;
  }
  return 0;
}