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++: #define _crt_secure_no_warnings

#ifndef _GLIBCXX_NO_ASSERT
#include <cassert>
#endif
#include <cctype>
#include <cerrno>
#include <cfloat>
#include <ciso646>
#include <climits>
#include <clocale>
#include <cmath>
#include <csetjmp>
#include <csignal>
#include <cstdarg>
#include <cstddef>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <ctime>

#ifdef __GXX_EXPERIMENTAL_CXX0X__
#include <ccomplex>
#include <cfenv>
#include <cinttypes>
#include <cstdbool>
#include <cstdint>
#include <ctgmath>
#include <cwchar>
#include <cwctype>
#endif


#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>

#ifdef __GXX_EXPERIMENTAL_CXX0X__
#include <array>
#include <atomic>
#include <chrono>
#include <condition_variable>
#include <forward_list>
#include <future>
#include <initializer_list>
#include <mutex>
#include <random>
#include <ratio>
#include <regex>
#include <system_error>
#include <thread>
#include <tuple>
#include <typeindex>
#include <type_traits>
#include <unordered_map>
#include <unordered_set>
#endif

using namespace std;

vector<vector<int>>data;

int x[] = { 0, 1, 1, 1, 0, -1, -1, -1 };
int y[] = { 1, 1, 0, -1, -1, -1, 0, 1 };

void serch(int i, int j)
{
	data[i][j] = 0;
	for (size_t k = 0; k < 8; k++)
	{
		if (data[i + x[k]][j + y[k]])
		{
			serch(i + x[k], j + y[k]);
		}
	}
}

int main()
{
	int N, M;
	int ans = 0;
	while (1)
	{
		data.clear();
		ans = 0;
		cin >> N >> M;
		if (N == 0 && M == 0)break;
		swap(M, N);
		data.resize(N + 2);
		for (size_t i = 0; i < N + 2; i++)
		{
			data[i].resize(M + 2);
			for (size_t j = 0; j < M+2; j++)
			{
				data[i][j] = 0;
			}
		}
		for (size_t i = 1; i <= N; i++)
		{
			for (size_t j = 1; j <= M; j++)
			{
				cin >> data[i][j];
			}
		}
		for (size_t i = 1; i <= N; i++)
		{
			for (size_t j = 1; j <= M; j++)
			{
				if (data[i][j])
				{
					ans++;
					serch(i, j);
				}
			}
		}
		cout << ans<<endl;
	}
}

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() {
    int w, h;
    cin >> w >> h;
    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;
}

C++: #include <bits/stdc++.h>
#define rep(i,n) for (int i=0; i<(n); ++i)
using namespace std;

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

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

void dfs(int i, int j){
    c[i][j]=0;
    rep(dir,8){
        int vy=i+dy[dir];
        int vx=j+dx[dir];
        if(vy>=0&&vy<h&&vx>=0&&vx<w&&c[vy][vx]==1){
            dfs(vy,vx);
        }
    }
    return;
}

int main(){
    while(1){
        cin >> w >> h;
        if(h==0&&w==0)break;
        c = vector <vector <int> >(h, vector <int> (w) );
        rep(i,h)rep(j,w)cin >> c[i][j];
        int cnt=0;
        rep(i,h)rep(j,w){
            if(c[i][j]==1){
                dfs(i,j);
                cnt++;
            }
        }
        cout << cnt << endl;
    }
    return 0;
}

C++: #include <bits/stdc++.h>
using namespace std;

using ll = long long;
#define REP(i, n) for(int (i)=0; (i)< (n); ++i)
#define REPR(i, n) for(int (i)=(n); (i)>=0; --i)
#define FOR(i, n, m) for(int (i)=(n); (i)<(m); ++i)

int w, h;
vector<vector<int>> c(50, vector<int>(50)), homon(50, vector<int>(50));
int dx[] = {0, 1, 0, -1, 1, -1, -1, 1};
int dy[] = {1, 0, -1, 0, 1, -1, 1, -1};

void dfs(int y, int x){
  homon[y][x] = 1;
  REP(i, 8){
    int nx = x + dx[i], ny = y+dy[i];
    if(nx < 0 || w <= nx || ny < 0 || h <= ny) continue;
    if(homon[ny][nx] || c[ny][nx] == 0) continue;
    dfs(ny, nx);
  }
}

int main(){
  vector<int> ans;
  while(true){
    cin >> w >> h;
    if(w==0 && h== 0) break;
    REP(i, h){
      REP(j, w){
        cin >> c[i][j];
      }
    }
    REP(i, h){
      REP(j, w){
        homon[i][j] = 0;
      }
    }
    int cnt = 0;
    REP(i, h){
      REP(j, w){
        if(homon[i][j]==1 || c[i][j]==0) continue;
        dfs(i, j);
        cnt++;
      }
    }
    ans.push_back(cnt);
  }
  REP(i, ans.size()){
    cout << ans[i] << endl;
  }
  return 0;
}