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>
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||nw<0||H-1<nh||W-1<nw) continue;
			if(field[nh][nw] == 0) continue;
			
			dfs(nh, nw);
		}
	}	
}

int main()
{
	while(1){
		cin>>W>>H;
		if(W==0&&H==0) break;
		field.assign(H, vector<int>(W, 0));
		for(int i=0;i<H;i++){
			for(int j=0;j<W;j++){
				cin>>field[i][j];
			}
		}
		
		int cnt = 0;
		for(int i=0;i<H;i++){
			for(int j=0;j<W;j++){
				if(field[i][j] == 0) continue;
				dfs(i, j);
				cnt++;
			}
		}
		cout<<cnt<<endl;
	}
}

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

int c[MAX][MAX];

void dfs(int w, int h, int x, int y);
int main(int argc, char const *argv[]){
    int w, h, ans;

    while (cin>>w>>h, w || h){
        ans = 0;
        for (int i = 0; i < h; i++){
            for (int j = 0; j < w; j++){
                cin>>c[i][j];
            }
        }
        for (int i = 0; i < w; i++){
            for (int j = 0; j < h; j++){
                if (c[i][j] == 1){
                    dfs(w, h, i, j);
                    ans++;
                }
            }
        }
        cout<<ans<<endl;
    }

    return 0;
}

void dfs(int w, int h, int x, int y){
    int dx[8] = {1, 1, 0, -1, -1, -1, 0, 1};
    int dy[8] = {0, 1, 1, 1, 0, -1, -1, -1};
    c[x][y] = 0;

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

        if (0 <= nx && nx < w && 0 <= ny && ny < h && c[nx][ny] == 1){
            dfs(w, h, nx, ny);
        }
    }
    return;
}

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

#define REP(i,m,n) for(int i=(int)m; i < (int) n; ++i)
#define rep(i,n) REP(i,0,n)

int H, W;
vector<vector<int> > field;

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

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

    while(cin >> W >> H) {
        if(H == 0 && W == 0) break;
        field.assign(H, vector<int> (W, 0));
        rep(i, H) rep(j, W) cin >> field[i][j];
        int ans = 0;
        rep(i, H) rep(j, W) {
            if(field[i][j] == 0) continue;
            dfs(i, j);
            ans++;
        }

        cout << ans << '\n';
    }
    return 0;
}

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

constexpr int LEN = 55;
bool m[LEN][LEN];

int main(){
	while(true){
		int w, h; cin >> w >> h;
		if(!w && !h) break;
		rep(i, h)rep(j, w) cin >> m[i][j];

		int ans = 0;
		rep(i, h)rep(j, w){
			if(!m[i][j]) continue;
			ans++;
			queue<P> q;
			q.push(make_pair(i, j));
			while(q.size()){
				P now = q.front(); q.pop();
				loop(di, -1, 2)loop(dj, -1, 2){
					int ni = now.first + di, nj = now.second + dj;
					if(0 <= ni && ni < h && 0 <= nj && nj < w && m[ni][nj]){
						m[ni][nj] = false;
						q.push(make_pair(ni, nj));
					}
				}
			}
		}
		cout << ans << endl;
	}
	return 0;
}

C++: #include <stdio.h>

void solve(int a[50][50],int x,int y){
	int i, j;
	a[y][x] = 0;
	for(i = y - 1;i <= y + 1;i++){
		for(j = x - 1;j <= x + 1;j++)
			if(a[i][j]) solve(a,j,i);
	}
	return;
}

int main(void){
	int w, h, i, j, flag[50][50], count;
	scanf("%d%d",&w,&h);
	while(w != 0 || h != 0){
		for(i = 0;i < h;i++)
			for(j = 0;j < w;j++) scanf("%d",&flag[i][j]);
		count = 0;
		for(i = 0;i < h;i++){
			for(j = 0;j < w;j++){
				if(flag[i][j]){
					count++;
					solve(flag,j,i);
				}
			}
		}
		printf("%d\n",count);
		scanf("%d%d",&w,&h);
	}
	return 0;
}

C++: #include <iostream>
#define MAX_W 50
#define MAX_H 50
using namespace std;

int w, h;
int t[MAX_H][MAX_W];

void PRINT() {
  for(int i = 0; i < h; ++i) {
    for(int j = 0; j < w; ++j) {
      cout << t[i][j] << ' ';
    }
    cout << endl;
  }
  cout << endl;
}

// 深さ優先探索
// 現在位置(y, x)
void dfs(int y, int x) {
  t[y][x] = 8;

  for(int dx = -1; dx <= 1; dx++) {
    for(int dy = -1; dy <= 1; dy++) {
      // 現在位置を(nx, ny)に更新
      int nx = x + dx;
      int ny = y + dy;

      // (nx, ny)は部屋の中か？ & t[nx][ny]は陸か？ を判定
      if(0 <= nx && nx < w && 0 <= ny && ny < h && t[ny][nx] == 1) {
        dfs(ny, nx);
        //PRINT();
      }
    }
  }
  return;
}

void solve() {
  int cnt = 0;
  for(int y = 0; y < h; ++y) {
    for(int x = 0; x < w; ++x) {

      if(t[y][x] == 1) {
        dfs(y, x);
        cnt++;
      }
      
    }
  }
  cout << cnt << endl;
}

int main() {
  while(cin >> w >> h && w && h) {

    for(int i = 0; i < h; ++i) {
      for(int j = 0; j < w; ++j) {
        cin >> t[i][j];
      }
    }
    solve();
  }
  return 0;
}