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;
#define REP(i,n) for(int i=0;i<(int)(n);i++)
const int WMAX = 100, HMAX = 100;
int w, h;
int island[WMAX][HMAX];

void dfs(int x, int y) {
  if (x < 0 or w <= x) return;
  if (y < 0 or h <= y) return;
//  cout << "(" << x << " " << y << ")" << endl;
  if (!island[x][y]) return;
  island[x][y] = false;
  int dx[8] = {-1, 0, 1, -1, 1, -1, 0, 1},
      dy[8] = {-1, -1, -1, 0, 0, 1, 1, 1};

  REP(dir, 8) {
    dfs(x + dx[dir], y + dy[dir]);
  }
}

int solve() {
  int ans = 0;
  REP(y, h) {
    REP(x, w) {
      if (island[x][y]) {
        ans++;
        dfs(x, y);
      }
    }
  }
  return ans;
}

int main() {
  while (cin >> w >> h and w) {
    REP(y, h) {
      REP(x, w) {
        int c;
        cin >> c;
        island[x][y] = c;
      }
    }

    cout << solve() << endl;
  }
}

C++: #include <vector>
#include <map>
#include <set>
#include <stack>
#include <queue>
#include <deque>
#include <algorithm>
#include <utility>
#include <functional>
#include <sstream>
#include <iostream>
#include <cstdio>
#include <cmath>
#include <cstdlib>
#include <cctype>
#include <string>
#include <cstring>
#include <ctime>
#include <climits>

using namespace std;

inline int toInt(string s) { int v; istringstream sin(s); sin >> v; return v;}
template<class T> inline string toString(T x) { ostringstream sout; sout << x; return sout.str();}

typedef vector<int> vi;
typedef vector<vi>  vvi;
typedef vector<string> vs;
typedef pair<int, int> pii;
typedef long long ll;

#define ALL(a) (a).begin(),(a).end()
#define RALL(a) (a).rbegin(),(a).rend()
#define EACH(t,i,c) for(t::iretator i=(c).begin(); i!=(c).end(); ++i)
#define EXIST(s,e) ((s).find(e)!=(s).end())
#define FOR(i,a,b) for(int i=(a);i<(b);++i)
#define REP(i,n) FOR(i,0,n)

const double EPS = 1e-10;

int seamap[52][52];
int num = 0;

void dfs(int x, int y) {
	if(seamap[x-1][y-1]) {
		seamap[x-1][y-1] = 0;
		dfs(x-1, y-1);
	}
	else if(seamap[x][y-1]) {
		seamap[x][y-1] = 0;
		dfs(x, y-1);
	}
	else if(seamap[x+1][y-1]) {
		seamap[x+1][y-1] = 0;
		dfs(x+1, y-1);
	}
	else if(seamap[x-1][y]) {
		seamap[x-1][y] = 0;
		dfs(x-1, y);
	}
	else if(seamap[x+1][y]) {
		seamap[x+1][y] = 0;
		dfs(x+1, y);
	}
	else if(seamap[x-1][y+1]) {
		seamap[x-1][y+1] = 0;
		dfs(x-1, y+1);
	}
	else if(seamap[x][y+1]) {
		seamap[x][y+1] = 0;
		dfs(x, y+1);
	}
	else if(seamap[x+1][y+1]) {
		seamap[x+1][y+1] = 0;
		dfs(x+1, y+1);
	}
	else {
		num++;
	}
}


int main() {
	int w, h;

	REP(i, 52) {
		REP(j, 52) {
			seamap[i][j] = 0;
		}
	}

	while(cin >> w >> h, (w!=0 && h!=0)) {
		FOR(i, 1, h+1) {
			FOR(j, 1, w+1) {
				int p;
				cin >> p;
				seamap[i][j] = p;
			}
		}

		FOR(i, 1, h+1) {
			seamap[i][w+1] = 0;
		}

		FOR(j, 1, w+1) {
			seamap[h+1][j] = 0;
		}
	
		num = 0;

		dfs(1, 1);

		cout << num << endl;
	}
}

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 s=-1;s<=1;l++){
            nx=a+k;
            ny=b+s;
        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;
}

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

int H,W;
vector<vector<int> > field;
void dfs(int h,int w){
    field[h][w]=0;
    for(int dh=-1;dh<=1;++dh){
        for(int dw=-1;dw<=1;dw++){
            int nh=h+dh,nw=w+dw;
            if(nh<0 || nh>=H || nw<0 || nw>=W)continue;
            if(field[nh][nw]==0)continue;
            dfs(nh,nw);
        }
    }
}

int main(){
    while(cin>>W>>H){
        if(H==0 || W==0)break;
        field.assign(H,vector<int>(W,0));
        for(int h=0;h<H;h++)for(int w=0;w<W;w++)cin>>field[h][w];
        int count=0;
        for(int h=0;h<H;h++){
            for(int w=0;w<W;w++){
                if(field[h][w]==0)continue;
                dfs(h,w);
                ++count;
            }
            
        }
        cout<<count<<endl;
    }
}

C++: #include <bits/stdc++.h>
#define rep(i, n) for (int i=0; i<(int)(n); i++)
#define all(v) v.begin(), v.end()
using namespace std;
using ll = long long;
using Graph = vector<vector<int>>;
const ll MOD = 1000000007;
const ll INF = 9999999999;
template<class T> inline bool chmin(T& a, T b){if (a>b){a = b; return true;}return false;}
template<class T> inline bool chmax(T& a, T b){if (a<b){a = b; return true;}return false;}
template<class T> inline T powerM(T a,T b){if (b==0) return 1;
  T tmp = powerM(a,b/2); if (b%2==0) return tmp*tmp%MOD; else return tmp*tmp%MOD*a%MOD; }


int H,W;
vector<vector<int>> field;
vector<int> di = {0, 1, 0, -1, 1, 1, -1, -1};
vector<int> dj = {1, 0, -1, 0, 1, -1, 1, -1};

void dfs(int nowi, int nowj){
  field[nowi][nowj] = 0;
  for (int i=0; i<di.size(); i++){
    int ni = nowi + di[i];
    int nj = nowj + dj[i];
    if (ni < 0 || ni >= H || nj < 0 || nj >= W) continue;
    if (field[ni][nj]) dfs(ni,nj);
  }
}

int main() {
  cin >>W >>H;
  while (W * H){
    field.assign(H, vector<int>(W, 0));
    rep(i, H) rep(j, W) cin >>field[i][j];
    int res = 0;

    for (int i=0; i<H; i++){
      for (int j=0; j<W; j++){
        if (field[i][j]) dfs(i,j), res++;
      }
    }

    cout <<res <<endl;

    cin >>W >>H;
  }

}