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 <vector>
#include <cfloat>
#include <string>
#include <cmath>
#include <set>
#include <cstdlib>
#include <map>
#include <ctime>
#include <iomanip>
#include <functional>
#include <deque>
#include <iostream>
#include <cstring>
#include <queue>
#include <cstdio>
#include <stack>
#include <climits>
#include <sys/time.h>
#include <cctype>

using namespace std;

typedef long long ll;

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

    bool field[w][h];
    memset(field, false, sizeof(field));
    for (int i = 0; i < h; i++) {
      for (int j = 0; j < w; j++) {
	int t;
	cin >> t;
	if (t == 0) {
	  field[j][i] = false;
	}else {
	  field[j][i] = true;
	}
      }
    }

    int cnt = 0;
    int dx[] = {0, 0, 1, -1, 1, -1, 1, -1};
    int dy[] = {1, -1, 0, 0, 1, -1, -1, 1};
    for (int i = 0; i < h; i++) {
      for (int j = 0; j < w; j++) {
	if (field[j][i]) {
	  stack<int> st;
	  st.push(j);
	  st.push(i);
	  field[j][i] = false;
	  while (!st.empty()) {
	    int y = st.top(); st.pop();
	    int x = st.top(); st.pop();

	    for (int p = 0; p < 8; p++) {
	      int nx = x+dx[p];
	      int ny = y+dy[p];

	      if (nx < 0 || ny < 0 ||
		  nx >= w || ny >= h ||
		  !field[nx][ny]) {
		continue;
	      }
	      st.push(nx);
	      st.push(ny);
	      field[nx][ny] = false;
	    }
	  }
	  cnt++;
	}
      }
    }

    std::cout << cnt << std::endl;
  }
}

C++: #include <iostream>
#include <algorithm>
#include <vector>
#include <stack>
#include <queue>
#include <map>
#include <cmath>
#include <string>
#include <sstream>
#include <iomanip>
#include <complex>
using namespace std;

#define ll long long
#define vvi vector< vector<int> >
#define All(X) X.begin(),X.end()
#define FOR(i,a,b) for(int i=(int)(a);i<(int)(b);i++)
#define REP(i,n) for(int i=0;i<(int)(n);i++)
#define pb push_back 
ll gcd(ll a,ll b){return b?gcd(b,a%b):a;}
ll lcm(ll a,ll b){return a/gcd(a,b)*b;}

int data[51][51];
int dx[8] = {-1,-1,0,1,1,1,0,-1};
int dy[8] = {0,1,1,1,0,-1,-1,-1};

int h,w;
bool inrange(int x,int y){
	if(x<0||y<0||x>h-1||y>w-1) return false;
	else return true;
}

bool search(int x,int y){
	bool rst = false;
	if(data[x][y] == 0) return rst;
	else {
		data[x][y] = 0;
		rst = true;
	}
	REP(i,8){
		int nx = x+dx[i];
		int ny = y+dy[i];
		if(!inrange(nx,ny)) continue;
		search(nx,ny);
	}
	return rst;
}

int main(){
	while(1){
		cin >> w >> h;
		if(w==0&&h==0) break;
		int ans = 0;
		REP(i,h) REP(j,w) cin >> data[i][j];
		REP(i,h){
			REP(j,w){
				if(search(i,j)==true) ans++;
			}
		}
		cout << ans << endl;
	}
}

C++: #include <cstdio>
#define f(n)for(int i=0;i<n;i++)
#define s(w)scanf("%d",&w)
int a,n,m,d[99][99];
int u[16]={-1,-1,-1,0,0,1,1,1,-1,0,1,-1,1,-1,0,1};
void k(int x,int y){
	if(d[x][y]){f(8)k(x+u[i],y+u[i+8]);d[x][y]=0;}
}
int main(){
	a=0;
	s(n);s(m);
	f(9801)d[i/99][i%99]=0;
	f(n*m)s(d[i/n+1][i%n+1]);
	f(n*m)if(d[i/n+1][i%n+1])a++,k(i/n+1,i%n+1);
	printf("%d\n",a);
	main();
}

C++: #include <algorithm>
#include <bitset>
#include <complex>
#include <deque>
#include <exception>
#include <fstream>
#include <functional>
#include <iomanip>
#include <ios>
#include <iosfwd>
#include <iostream>
#include <istream>
#include <iterator>
#include <limits>
#include <list>
#include <locale>
#include <map>
#include <memory>
#include <new>
#include <numeric>
#include <ostream>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <stdexcept>
#include <streambuf>
#include <string>
#include <typeinfo>
#include <utility>
#include <valarray>
#include <vector>
#include <climits>

#define rep(i, m, n) for(int i=int(m);i<int(n);i++)
#define EACH(i, c) for (auto &(i): c)
#define all(c) begin(c),end(c)
#define EXIST(s, e) ((s).find(e)!=(s).end())
#define SORT(c) sort(begin(c),end(c))
#define pb emplace_back
#define MP make_pair
#define SZ(a) int((a).size())

//#define LOCAL 0
//#ifdef LOCAL
//#define DEBUG(s) cout << (s) << endl
//#define dump(x)  cerr << #x << " = " << (x) << endl
//#define BR cout << endl;
//#else
//#define DEBUG(s) do{}while(0)
//#define dump(x) do{}while(0)
//#define BR
//#endif


//改造
typedef long long int ll;
using namespace std;
#define INF (1 << 30)
#define INFl (ll)5e15
#define DEBUG 0 //デバッグする時1にしてね
#define dump(x)  cerr << #x << " = " << (x) << endl
#define MOD 1000000007
//ここから編集する
int W, H;

bool check(int w, int h) {
    return w >= 0 && h >= 0 && w < W && h < H;
}

void dfs(int w, int h, vector<vector<int> > &v) {
    if (check(w, h)) {
        if (v[h][w] != 1) return;
        v[h][w] = 2;
//        dfs(w+1,h,v);
//        dfs(w,h+1,v);
//        dfs(w-1,h,v);
//        dfs(w,h-1,v);
        for (int i = h - 1; i <= h + 1; i++) {
            for (int j = w - 1; j <= w + 1; j++) {
                dfs(j, i, v);
            }
        }
    } else {
        return;
    }
}

void solve(int w, int h) {
    W = w;
    H = h;
    vector<vector<int> > v(h, vector<int>(w, 0));
    rep(i, 0, h) {
        rep(j, 0, w) {
            cin >> v[i][j];
        }
    }
    int ans = 0;
    rep(i, 0, h) {
        rep(j, 0, w) {
            if (v[i][j] == 1) {
                ans++;
                dfs(j, i, v);
            }
        }
    }
    cout << ans << endl;
}

int main() {
    cin.tie(0);
    ios::sync_with_stdio(false);

    while (true) {
        int w, h;
        cin >> w >> h;
        if (w == 0) break;
        solve(w, h);
    }


    return 0;
}