Go: package main

import (
	"bufio"
	"fmt"
	"os"
	"strconv"
)

func getScanner(fp *os.File) *bufio.Scanner {
	scanner := bufio.NewScanner(fp)
	scanner.Split(bufio.ScanWords)
	scanner.Buffer(make([]byte, 1000005), 1000005)
	return scanner
}

func getNextString(scanner *bufio.Scanner) string {
	scanner.Scan()
	return scanner.Text()
}

func getNextInt(scanner *bufio.Scanner) int {
	i, _ := strconv.Atoi(getNextString(scanner))
	return i
}

func getNextInt64(scanner *bufio.Scanner) int64 {
	i, _ := strconv.ParseInt(getNextString(scanner), 10, 64)
	return i
}

func getNextUint64(scanner *bufio.Scanner) uint64 {
	i, _ := strconv.ParseUint(getNextString(scanner), 10, 64)
	return i
}

func getNextFloat64(scanner *bufio.Scanner) float64 {
	i, _ := strconv.ParseFloat(getNextString(scanner), 64)
	return i
}

func main() {
	fp := os.Stdin
	wfp := os.Stdout
	cnt := 0

	if os.Getenv("MASPY") == "ますピ" {
		fp, _ = os.Open(os.Getenv("BEET_THE_HARMONY_OF_PERFECT"))
		cnt = 0
	}
	if os.Getenv("MASPYPY") == "ますピッピ" {
		wfp, _ = os.Create(os.Getenv("NGTKANA_IS_GENIUS10"))
	}

	scanner := getScanner(fp)
	writer := bufio.NewWriter(wfp)
	for solve(scanner, writer) {

	}
	for i := 0; i < cnt; i++ {
		fmt.Fprintln(writer, "-----------------------------------")
		solve(scanner, writer)
	}
	writer.Flush()
}

func solve(scanner *bufio.Scanner, writer *bufio.Writer) bool {
	sol := solver{}
	sol.w = getNextInt(scanner)
	sol.h = getNextInt(scanner)
	if sol.w == 0 && sol.h == 0 {
		return false
	}
	sol.land = makeInts(sol.h, sol.w)
	for y := 0; y < sol.h; y++ {
		for x := 0; x < sol.w; x++ {
			sol.land[y][x] = getNextInt(scanner)
		}
	}
	ans := 0
	for y := 0; y < sol.h; y++ {
		for x := 0; x < sol.w; x++ {
			if sol.land[y][x] == 0 {
				continue
			}
			ans++
			sol.dfs(y, x)
		}
	}
	fmt.Fprintln(writer, ans)
	return true
}

type solver struct {
	w, h int
	land [][]int
}

func (sol *solver) dfs(y, x int) {
	if y < 0 {
		return
	}
	if y >= sol.h {
		return
	}
	if x < 0 {
		return
	}
	if x >= sol.w {
		return
	}
	if sol.land[y][x] == 0 {
		return
	}
	sol.land[y][x] = 0
	dy := [8]int{-1, -1, -1, 0, 0, 1, 1, 1}
	dx := [8]int{-1, 0, 1, -1, 1, -1, 0, 1}
	for i := 0; i < 8; i++ {
		sol.dfs(y+dy[i], x+dx[i])
	}
}
func makeInts(h, w int) [][]int {
	index := make([][]int, h, h)
	data := make([]int, h*w, h*w)
	for i := 0; i < h; i++ {
		index[i] = data[i*w : (i+1)*w]
	}
	return index
}

C++: #include<bits/stdc++.h>
#define rep(i,a,b) for(int i=int(a);i<int(b);++i)
using namespace std;
typedef long long ll;
int INF = (1LL << 30) - 1;
int MOD = 1e9+7;
int H,W;
void dfs(int h,int w,vector<vector<int> > &table){
    //cout << h << " " << w << endl;
    table[h][w] = 0;
    int dx[] = {-1,-1,-1,0 ,0,1 ,1,1};
    int dy[] = {-1,0 ,1 ,-1,1,-1,0,1};
    rep(i,0,8){
        int nx = w + dx[i];
        int ny = h + dy[i];
        if (nx >= W || nx < 0 || ny >= H || ny < 0) continue;
        if(table[ny][nx])dfs(ny,nx,table);
    }
}
main(){
    cin >> H >> W;
    vector< vector<int> > table;
    while(H){
        table = vector< vector<int> > (H,vector<int>(W) );
        rep(i,0,H)rep(j,0,W)cin >> table[i][j];
        int cnt = 0;
        rep(i,0,H)rep(j,0,W){
            if(table[i][j] == 1){
                dfs(i,j,table);
                cnt++;
            }
        }
        cout << cnt << endl;
        cin >> H >> W;
    }
}

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

#define rep(i, j, n) for(int i=j;i<n;++i)
#define all(i) i.begin(),i.end()
#define rall(i) i.rbegin(),i.rend()
#define inf 1e9
#define Puts(x) puts(x? "Yes":"No")
const int mod = 1e9 + 7;

int gcd(int n, int m) {return (m == 0? n : gcd(m, n % m));}
int dx[] = {0, 1, 0, -1, 1, 1, -1, -1};
int dy[] = {1, 0, -1, 0, 1, -1, 1, -1};

typedef pair<int, int> pi;
typedef long long i64;

template <class T> using vt = vector<T>;
template <class T> using vvt = vector<vector<T>>;

int w, h;

void dfs(int y, int x, auto &grid, auto &visited) {
  if(!grid[y][x] || visited[y][x]) return;

  visited[y][x] = true;
  rep(i, 0, 8) {
    dfs(y + dy[i], x + dx[i], grid, visited);
  }
}

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

  while(cin >> w >> h, w && h) {
    vvt<bool> grid(h + 2, vt<bool>(w + 2, false));
    vvt<bool> visited(h + 2, vt<bool>(w + 2, false));

    rep(i, 1, h + 1) {
      rep(j, 1, w + 1) {
        int c;
        cin >> c;
        grid[i][j] = !!c;
      }
    }

    int cou = 0;
    rep(i, 1, h + 1) {
      rep(j, 1, w + 1) {
        if(!grid[i][j] || visited[i][j]) continue;

        cou++;
        dfs(i, j, grid, visited);
      }
    }
    cout << cou << endl;
  }
}

C++: #include <bits/stdc++.h>

using namespace std;

#define rep(i, n) for(int i = 0; i < n; i++)
typedef long long ll;
template<typename T> bool chmax(T &a, T b) {if(a <= b){a = b; return true;}return false;}
template<typename T> bool chmin(T &a, T b) {if(a >= b){a = b; return true;}return false;}
#define sea 0
#define rand 1

int W, H;
vector<vector<int> > c;
vector<vector<bool> > memo;

int dw[8] = {0,1,1,1,0,-1,-1,-1};
int dh[8] = {1,1,0,-1,-1,-1,0,1};
void dfs(int h, int w){
    memo[h][w] = true;
    for(int i = 0; i < 8; i++){
        int next_h = h + dh[i];
        int next_w = w + dw[i];
        //地図の範囲外でないかの確認
        if(next_w < 0 || next_h < 0 || W <= next_w || H <= next_h)continue;
        //行き先が見探索であるか確認
        if(memo[next_h][next_w])continue;
        dfs(next_h, next_w);
    }
}

main(void) {
    while (true) {
        cin >> W >> H;
        if(W + H == 0)break;
        memo.resize(H);
        rep(h,H){
            memo[h].resize(W);
            rep(w,W)memo[h][w] = false;
        }
        c.resize(H);
        for(int h = 0; h < H; h++){
            c[h].resize(W);
            for(int w = 0; w < W; w++){
                cin >> c[h][w];
                if(c[h][w] == sea)memo[h][w] = true;
            }
        }
        int ans = 0;
        rep(h,H){
            rep(w, W){
                if(!memo[h][w]){
                    dfs(h, w);
                    ans++;
                }
            }
        }
        cout << ans << endl;
    }
}

C++: #include <bits/stdc++.h>

#define rep(i,a,b) for(int i=a;i<b;i++)
#define repd(i,a,b) for(int i=a;i>=b;i--)
#define llrep(i,a,b) for(ll i=a;i<b;i++)
#define llrepd(i,a,b) for(ll i=a;i>=b;i--)
#define fore(i,a) for(auto &i:a)
#define all(x) (x).begin(),(x).end()
using namespace std;
void _main(); int main() { cin.tie(0); ios::sync_with_stdio(false); _main(); }
typedef long long ll; const int inf = INT_MAX / 2; const ll infl = 1LL << 60; const ll MOD = 1000000007;
template<class T>bool chmax(T& a, const T& b) { if (a < b) { a = b; return 1; } return 0; }
template<class T>bool chmin(T& a, const T& b) { if (b < a) { a = b; return 1; } return 0; }

//---------------------------------------------------------------------------------------------------
int dx[8] = { 0, 1, 0, -1, 1, 1, -1, -1 };
int dy[8] = { 1, 0, -1, 0 ,1, -1, 1, -1 };
ll H, W;
vector<vector<int>> area;

void dfs(int i, int j) {
    area[i][j] = 0;
    rep(n, 0, 8) {
        int nx = i + dx[n], ny = j + dy[n];
        if (nx >= H || ny >= W || nx < 0 || ny < 0) continue;
        if (area[nx][ny] == 1) dfs(nx, ny);
    }
}


void resolve(ll W, ll H) {
    area = vector<vector<int>>(H, vector<int>(W));
    rep(i, 0, H) rep(j, 0, W) cin >> area[i][j];
    ll count = 0;
    rep(i, 0, H) {
        rep(j, 0, W) {
            if (area[i][j] == 1) {
                count++;
                dfs(i, j);
            }
        }
    }
    cout << count << endl;
}

void _main() {
    while(true) {
        cin >> W >> H;
        if (W == 0 && H == 0) break;
        resolve(W,H);
    }
}