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>

const int MAX_W = 50;
const int MAX_H = 50;

int w, h;

int c[MAX_H][MAX_W];

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

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

int solve() {
	int sum = 0;
	for (int i = 0; i < h; ++i) {
		for (int j = 0; j < w; ++j) {
			if (c[i][j] == 1) {
				++sum;
				dfs(j, i);
			}
		}
	}
	return sum;
}

int main() {
	while (1) {
		std::cin >> w >> h;
		if ((w + h) == 0)
			break;
		for (int i = 0; i < h; ++i) {
			for (int j = 0; j < w; ++j)
				std::cin >> c[i][j];
		}
		int res = solve();
		std::cout << res << std::endl;
	}
	return 0;
}

C++: #include <bits/stdc++.h>
using namespace std;
template<class T> inline bool chmax(T& a, T b) { if (a < b) { a = b; return true; } return false; }
template<class T> inline bool chmin(T& a, T b) { if (a > b) { a = b; return true; } return false; }

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

void dfs(vector< vector<int> > &isisland, vector< vector<bool> > &vvisited, int x, int y) {
    // ここで島の色塗りを行う
    vvisited[y][x] = true;
    for(int i = 0; i < 8; ++i) {
        int nx = x+dx[i]; int ny = y+dy[i];
        if(nx >= 0 && nx < w && ny >= 0 && ny < h) {
            if(isisland[ny][nx] == 1 && !vvisited[ny][nx]) {
                dfs(isisland, vvisited, nx, ny);
            }
        }
    }
}

int main() {
    while(1) {
        cin >> w >> h;
        if(w == 0 && h == 0) break;
        else {
            vector< vector<int> > island(h, vector<int>(w, 0));
            for(int i = 0; i < h; ++i) {
                for(int j = 0; j < w; ++j) {
                    cin >> island[i][j];
                }
            }

            vector< vector<bool> > visited(h, vector<bool>(w, false));
            int total = 0;
            for(int i = 0; i < h; ++i) {
                for(int j = 0; j < w; ++j) {
                    if(island[i][j] == 1 && !visited[i][j]) {
                        total++;
                        dfs(island, visited, j, i);
                    }
                }
            }
            cout << total << endl;
        }
    }    
}

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

using namespace std;
using ll = long long;

void dfs(const vector<vector<bool>> &field, vector<vector<bool>> &visit, int x, int y, int w, int h) {
    visit[y][x] = true;
    for (int dx = -1; dx <= 1; dx++) {
        for (int dy = -1; dy <= 1; dy++) {
            if (dx == 0 && dy == 0)
                continue;
            int nx = x + dx;
            int ny = y + dy;

            if (0 <= nx && nx < w && 0 <= ny && ny < h && field[ny][nx] && !visit[ny][nx]) {
                dfs(field, visit, nx, ny, w, h);
            }
        }
    }
}

void solve() {
    int w, h;
    cin >> w >> h;
    if (w == 0 && h == 0)
        exit(0);

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

    int ans = 0;
    vector<vector<bool>> visit(h, vector<bool>(w, false));
    for (int i = 0; i < h; i++) {
        for (int j = 0; j < w; j++) {
            if (field[i][j] && !visit[i][j]) {
                dfs(field, visit, j, i, w, h);
                ans++;
            }
        }
    }

    cout << ans << endl;
}

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

    while (true)
        solve();

    return 0;
}

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

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

int h,w;
void dfs(vector<vector<int>> &s,int x,int y){
  if(x<0 || x>=h || y<0 || y>=w) return;
  if(!s[x][y]) return;
  s[x][y] = 0;
  rep(i,8) dfs(s,x+dx[i],y+dy[i]);
}
  
  
int main(){
  while(1){   
    cin>>w>>h; 
    if(!h) break;
    vector<vector<int>> s(h,vector<int>(w));
    rep(i,h)rep(j,w) cin>>s[i][j];
     
    int ans = 0;
    rep(k,250){
      rep(i,h){
        rep(j,w){
          if(s[i][j]){
            dfs(s,i,j);
            ans++;
          }
        }
      }
    }
    cout << ans << endl;
  }
}