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 <bits/stdc++.h>
using namespace std;
#define rep(i, n) for (int i = 0; i < (int)(n); i++)
#define rep2(i, s, n) for (int i = s; i < (int)(n); i++)
#define rep3(i, s, n) for (int i = s; i > (int)(n); i--)
#define all(obj) obj.begin(), obj.end()
#define db(x) cerr << #x << ":" << x << " "
#define dbl(x) cerr << #x << ":" << x << "\n"
#define dbv(vec) cerr << #vec << ":"; for (auto e : vec) cerr << e << " "; cout << "\n"
#define dbvv(vv) cerr << #vv << ":\n"; for (auto vec : vv) { for (auto e : vec) cerr << e << " "; cerr << "\n"; }
#define YN(f) cout << (f ? "YES" : "NO") << "\n"
#define Yn(f) cout << (f ? "Yes" : "No") << "\n"
#define yn(f) cout << (f ? "yes" : "no") << "\n"
using ll = long long;
using vi = vector<int>; using vl = vector<ll>; using vs = vector<string>;
using vvi = vector<vi>; using vvl = vector<vl>;
using pii = pair<int, int>; using pll = pair<ll, ll>;
using vpii = vector<pii>; using vpll = vector<pll>;
using ti3 = tuple<int, int, int>; using tl3 = tuple<ll, ll, ll>;

vector<vector<bool>> c;
vector<vector<bool>> r;

void dfs(int i, int j) {
	r[i][j] = true;	
	vi di = { 1, 1, 0, -1, -1, -1, 0, 1 };
	vi dj = { 0, 1, 1, 1, 0, -1, -1, -1 };
	rep(k, 8) {
		int in = i + di[k], jn = j + dj[k];
		if (c[in][jn] and !r[in][jn]) dfs(in, jn);
	}
}

int main() {
	while (true) {
		int w, h; cin >> w >> h;
		if (w == 0 and h == 0) break;
		c.assign(h + 2, {}); rep(i, h + 2) c[i].assign(w + 2, false);
		rep2(i, 1, h + 1) {
			rep2(j, 1, w + 1) {
				int cij; cin >> cij;
				c[i][j] = cij == 1;
			}
		}

		// dbvv(c);
		r.assign(h + 2, {}); rep(i, h + 2) r[i].assign(w + 2, false);
		int ans = 0;		
		rep2(i, 1, h + 1) rep2(j, 1, w + 1) {
			if (c[i][j] and !r[i][j]) {
				dfs(i, j);
				ans++;
			}
		}
		// dbl(ans);
		cout << ans << endl;
	}
}

C++: #include <iostream>
#include <vector>
#include <queue>

using namespace std;

struct Pos { int y, x; };

int main(void)
{
    int w, h;
    int map[52][52] = {{0}};
    int nx[8] = {-1, 0, 1, 1, 1, 0, -1, -1};
    int ny[8] = {-1, -1, -1, 0, 1, 1, 1, 0};
    queue<Pos> poss;
    Pos now;
    int ans;
    while (true)
    {
        cin >> w >> h;
        if (!w && !h) break;
        ans = 0;
        for (int i = 1; i <= h; i++) for (int j = 1; j <= w; j++) cin >> map[i][j];
        for (int i = 1; i <= h; i++)
        {
            for (int j = 1; j <= w; j++)
            {
                if (map[i][j])
                {
                    poss.push({i, j});
                    while (!poss.empty())
                    {
                        now = poss.front();
                        poss.pop();
                        if (!map[now.y][now.x]) continue;
                        else map[now.y][now.x] = 0;
                        for (int k = 0; k < 8; k++) if (map[now.y + ny[k]][now.x + nx[k]]) poss.push({now.y + ny[k], now.x + nx[k]});
                    }
                    ans++;
                }
            }
        }
        cout << ans << endl;
    }
    return 0;
}

C++: #include <iostream>

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

using namespace std;

int area[50][50], cnt = 0;
int w, h;

void dfs(int x ,int y)
{
  area[x][y] = 0;

  for(int dx = -1 ; dx <= 1 ; dx++){
    for(int dy = -1 ; dy <= 1 ; dy++){
      int nx = x + dx, ny = y + dy;

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

int main()
{
  while(true){
    cin >> w >> h;
    if(w == 0 && h == 0) break;

    rep(j, h){
      rep(i, w){
	cin >> area[i][j];
      }
    }

    rep(i, w){
      rep(j, h){
	if(area[i][j] == 1){
	  dfs(i, j);
	  cnt++;
	}
      }
    }
    
    cout << cnt << '\n';
    cnt = 0;
  }
}

C++: #include <iostream>
#include <vector>
using namespace std;
int ans;
int w, h;
//?§???????
vector<int> vx = { -1, 0, 1, -1, 1, -1, 0, 1 };
vector<int> vy = { 1, 1, 1, 0, 0, -1, -1, -1 };
vector<vector<int>> map;
void Search(int i,int j){
	//??¨????????????????????§??????
	for (int m = 0; m < vx.size(); m++){
		int x = j + vx[m];
		int y = i + vy[m];
		if (x >= 0 && x < w &&
			y >= 0 && y < h){
			if (map[y][x] == 1){
				map[y][x] = map[i][j];
				Search(y,x);
			}
		}
	}
}

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

		//??¢?´¢
		for (int i = 0; i < h; i++){
			for (int j = 0; j < w; j++){
				if (map[i][j] != 1) continue;
				ans++;
				Search(i,j);
			}
		}
		cout << --ans << endl;
	}
	return 0;
}

C++: #include<bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef pair<ll,ll> pll;

ll w,h;
vector<vector<ll>> ans(50,vector<ll> (50,-1));
vector<vector<ll>> maze(50,vector<ll> (50));

vector<ll> idx={1,0,-1,0,1,1,-1,-1};
vector<ll> idy={0,1,0,-1,1,-1,1,-1};

bool dfs(ll th,ll tw,ll n) {
  if(ans[th][tw]<0&&maze[th][tw]==1) {
    ans[th][tw]=n;
    for(ll i=0;i<8;i++) {
      ll nh=th+idy[i];
      ll nw=tw+idx[i];
      if(nh>-1&&nh<h&&nw>-1&&nw<w) {
        dfs(nh,nw,n);
      }
    }
    return true;
  }
  else {
    return false;
  }
}

int main() {
  while(1) {
    cin >> w >> h;
    if(w==0&&h==0) {
      return 0;
    }
    for(ll i=0;i<h;i++) {
      for(ll j=0;j<w;j++) {
        cin >> maze[i][j];
        ans[i][j]=-1;
      }
    }
    ll count=0;
    for(ll i=0;i<h;i++) {
      for(ll j=0;j<w;j++) {
        if(dfs(i,j,count)) {
          count++;
        }
      }
    }
    cout << count << endl;
  }
}