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 <climits>
#include <iostream>
#include <string>
#include <utility>
#include <vector>
#include <map>
#include <set>
using namespace std;
using ll = long long int;

template <class T> ostream &operator<<(ostream &os, vector<T> &v) {
  for (auto i = v.begin(); i != v.end(); i++) {
    os << *i << " ";
  }
  return os;
}

ll gcd(ll a, ll b) {
  ll tmp;
  if ( b > a) {
    tmp = a;
    a = b;
    b = tmp;
  }
  while (a % b !=0) {
    tmp = b;
    b = a % b;
    a = tmp;
  }
  return b;
}

ll lcm(ll a, ll b) {
  return a * b / gcd(a, b);
}

int d[8][2] = {{1, 0}, {-1, 0}, {0, 1}, {0, -1}, {1, 1}, {1, -1}, {-1, 1}, {-1, -1}};
int w, h;
void dfs(int y, int x, vector<vector<int>> &v) {
  v[y][x] = 0;
  for(int i = 0; i<8; i++) {
    int ny = y + d[i][0];
    int nx = x + d[i][1];
    if(0<=ny and ny<h and 0<=nx and nx<w and v[ny][nx] == 1) {
      dfs(ny, nx, v);
    }
  }
}

int main(void) {
  while(1) {
    cin>>w>>h;
    if(w == 0 and h == 0)
      break;
    vector<vector<int>> v;
    for(int i = 0; i<h; i++) {
      vector<int> vv;
      for(int j=0; j<w; j++) {
	int x;
	cin>>x;
	vv.push_back(x);
      }
      v.push_back(vv);
    }
    int cnt = 0;
    for(int i=0; i<h; i++) {
      for(int j=0; j<w; j++) {
	if (v[i][j] == 1) {
	  cnt++;
	  dfs(i, j, v);
	}

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

C++: // https://onlinejudge.u-aizu.ac.jp/challenges/search/titles/1160
// dfs

#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <cmath>
#include <iostream>
#include <vector>
#include <string>
#include <algorithm>
#include <stack>
#include <queue>
#include <set>
#include <map>

using namespace std;

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

typedef long long ll;

int field[55][55];
int W, H;

void dfs(int y, int x)
{
    field[y][x] = 0;
    for (int i = -1; i <= 1; ++i) {
        for (int j = -1; j <= 1; ++j) {
            if (i == 0 && j == 0) continue;
            int nx = x + i;
            int ny = y + j;
            if (nx >= 0 && ny >= 0 && nx < W && ny < H && field[ny][nx] == 1) {
                dfs(ny, nx);
            }
        }
    }
}

void Main()
{
    while (true) {
        cin >> W >> H;
        if (W == 0 && H == 0) break;
        memset(field, 0, sizeof(field));
        rep(i, H)
        {
            rep(j, W)
            {
                cin >> field[i][j];
            }
        }
        int cnt = 0;
        rep(i, H)
        {
            rep(j, W)
            {
                if (field[i][j] == 1) {
                    dfs(i, j);
                    ++cnt;
                }
            }
        }
        cout << cnt << endl;
    }
}

int main()
{
    // cin.tie(nullptr);
	// ios_base::sync_with_stdio(false);
	Main();
}

C++: #include <bits/stdc++.h>
using namespace std;
vector<vector<long long>> c;
long long dx[8] = {-1, -1, -1, 0, 0, 1, 1, 1};
long long dy[8] = {-1, 0, 1, 1, -1, -1, 0, 1};
long long W, H;

void DFS(long long y, long long x)
{
    c[y][x] = 0;
    for (long long i = 0; i < 8; i++)
    {
        long long tx = x + dx[i];
        long long ty = y + dy[i];

        if (tx < 0 || ty < 0 || tx >= W || ty >= H)
            continue;

        if (c[ty][tx] == 0) //if(tx~)と逆だとover
            continue;

        DFS(ty, tx);
    }
}

int main()
{

    while (cin >> W >> H)
    {
        if (H == 0 && W == 0)
            break;
        long long cnt = 0;
        c.assign(H, vector<long long>(W, 0));
        //bool seen[H][W] = {false};

        for (int i = 0; i < H; i++)
        {
            for (int j = 0; j < W; j++)
            {
                cin >> c[i][j];
            }
        }

        for (int i = 0; i < H; i++)
        {
            for (int j = 0; j < W; j++)
            {
                //if (c[i][j] == 1) この条件だと,海の近くに島があるとそれを塗りつぶしてしまう。
                //    cnt++;
                if (c[i][j] == 0) //この条件を入れないと,海隣の島を塗りつぶしてしまう。
                    continue;
                DFS(i, j);
                cnt++;
            }
        }

        cout << cnt << endl;
    }
    return 0;
}

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

using namespace std;

#define llong long long 
int w,h;
int sum;
int area[50][50];
int nw[8] = {0, 1, 1, 1, 0, -1, -1, -1};
int nh[8] = {1, 1, 0, -1, -1, -1, 0, 1};

void solve(int H, int W){
	if(area[H][W] == 0)return;
	area[H][W] = 0;
	for(int i = 0; i < 8; i++){
		int sh = H + nh[i];
		int sw = W + nw[i];
		if(sh < 0 || sw < 0 || sh >= h || sw >= w) continue;
		solve(sh, sw);
	}

	return;
}

int main(){

	while(cin >> w >> h, w){
		sum = 0;
		fill_n(*area, 50 * 50, 0);
		for(int i = 0; i < h; i++){
			for(int j = 0; j < w; j++){
				cin >> area[i][j];
			}
		}	

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

		cout << sum << endl;
	}
	return 0;
}