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;

const int dx[3]={-1,0,1};

int w,h,ans;
queue<pair<int,int>> que;

int main(){
    cin.tie(0);
    ios::sync_with_stdio(false);
    while(cin >> w >> h,w){
        vector<vector<int>> m(h,vector<int>(w));
        for (int i=0;i<h;++i)
            for (int j=0;j<w;++j)
                cin >> m[i][j];
        vector<vector<int>> visited(h,vector<int>(w,0));
        ans=0;
        for (int i=0;i<h;++i){
            for (int j=0;j<w;++j){
                if (visited[i][j]||!m[i][j]) continue;
                ++ans;
                visited[i][j]=1; que.emplace(i,j);
                while(!que.empty()){
                    pair<int,int> p=que.front(); que.pop();
                    for (int k=0;k<3;++k){
                        for (int l=0;l<3;++l){
                            int nh=p.first+dx[k],nw=p.second+dx[l];
                            if (nh<0||h<=nh||nw<0||w<=nw) continue;
                            if (visited[nh][nw]||!m[nh][nw]) continue;
                            visited[nh][nw]=1; que.emplace(nh,nw);
                        }
                    }
                }
            }
        }
        cout << ans << endl;
    }
}

C++: #include<iostream>
#include<algorithm>
#include<vector>
#include<stdio.h>
#include<cstring>
#include<math.h>
#include<map>
#include<bitset>
#include<iomanip>
using namespace std;

#define rep(i, n) for (int i = 0; i < (int)(n); ++i)
int w, h;
int m[50][50];
int visited[50][50] = {0};

void dfs(int x, int y) {
  visited[x][y] = 1;
  for(int i = -1; i <= 1; i++) {
    for(int j = -1; j <= 1; j++) {
      int nx = x + i;
      int ny = y + j;
      if(0 <= nx && nx < h && 0 <= ny && ny < w && visited[nx][ny] == 0 && m[nx][ny] == 1) {
        dfs(nx, ny);
      }
    }
  }
  
}

int main() {
  while(true) {
    rep(i, h) {
      rep(j, w) {
        visited[i][j] = 0;
      }
    }
    cin >> w >> h;
    if(w == 0 && h == 0) break;
    rep(i, h) {
      rep(j, w) {
        cin >> m[i][j];
      }
    }
    int count = 0;
    for(int i = 0; i < h; i++) {
      for(int j = 0; j < w; j++) {
        if(m[i][j] == 1 && visited[i][j] == 0) {
          count++;
          dfs(i, j);
        }
      }
    }
    cout << count << endl;
  }
  return 0;
}

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

int H, W;
bool grid[55][55];

void island(int h, int w) {
  grid[h][w] = 0;

  for(int i = -1; i <= 1; ++i) {
    for(int j = -1; j <= 1; ++j) {
      if(i == 0 && j == 0) continue;
      int nh = h + i;
      int nw = w + j;
      if(0 <= nh && nh < H && 0 <= nw && nw < W && grid[nh][nw]) island(nh, nw);
    }
  }
}

int main(){
  while(true) {
    cin >> W >> H;
    if(W == 0 && H == 0) break;

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

    int ans = 0;
    for(int i=0; i < H; ++i) {
      for(int j=0; j < W; ++j) {
        if(grid[i][j]) {
          island(i,j);
          ++ans;
        }
      }
    }
    cout << ans << '\n';
  }

  return 0;
}

C++: #include <algorithm>
#include<iostream>
#include<vector>
#include<list>
#include<map>
#include<set>
#include<string>

#include<stdlib.h>
#include<string.h>
#include<math.h>
#include<limits.h>

using namespace std;

long mapp[51][51];
bool seen[51][51];
int W,H;

// 八方向への移動ベクトル
const int dx[8] = {1, 0, -1, 0, -1, 1 ,-1, 1};
const int dy[8] = {0, 1, 0, -1, -1,-1 , 1, 1};

void dfs(int w,int h){

  seen[h][w] = true;
  int ii,jj;
  int nx,ny;

  for(ii=0;ii<8;ii++){

    nx = w + dx[ii];
    ny = h + dy[ii];

    if(nx < 0 || ny < 0 || nx >= W || ny >= H){continue;}
    if(mapp[ny][nx] == 0){continue;}
    if(seen[ny][nx] == true){continue;}

    //cout << "H:" << H << "W:" << W << "nx:" << nx << "ny:" << ny << endl;
    dfs(nx,ny);
  }

  
}

int main(){

  long ii,jj,kk;
  vector<int> ret;


  while(true){
    cin >> W >> H;
    //cout << "W:" << W << "H:" << H <<endl;
    if(W == 0){
      break;
    }
    memset(mapp,0,sizeof(mapp));    
    for(ii=0;ii<H;ii++){
      for(jj=0;jj<W;jj++){
        cin >> mapp[ii][jj];
      }
    }

    memset(seen,0,sizeof(seen));

    int cnt=0;

    for(ii=0;ii<H;ii++){
      for(jj=0;jj<W;jj++){

        //cout << "ii:" << ii << "jj:" << jj << endl;
        if(mapp[ii][jj] == 1 && seen[ii][jj] == false){
          //cout << "ii:" << ii << "jj:" << jj << endl;
          dfs(jj,ii);
          cnt++;
        }
      }
    }

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

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

using namespace std;

void dfs(vector<vector<int> >& map, int y, int x, int H, int W, int index)
{
  int dx[8] = {-1, 0, 1, -1, 1, -1, 0, 1};
  int dy[8] = {-1, -1, -1, 0, 0, 1, 1, 1};

  map[y][x] = index;
  for (int i = 0; i < 8; i++)
  {
    if (y + dy[i] >= 0 && y + dy[i] < H && x + dx[i] >= 0 && x + dx[i] < W){
      if (map[y + dy[i]][x + dx[i]] == 1)
      {
        dfs(map, y + dy[i], x + dx[i], H, W, index);
      }
    }
  }
}

int main()
{
  int W,H;
  int index = 2;
  while(1){
    cin >> W >> H;
    if(W == 0 && H == 0){
      break;
    }
    vector<vector<int> > map(H, vector<int>(W, 0));
    for (int i = 0; i < H; i++)
    {
      for (int j = 0; j < W;j++){
        cin >> map[i].at(j);
      }
    }
    for (int i = 0; i < H;i++){
      for (int j = 0; j < W;j++){
        if(map[i][j] == 1){
          dfs(map, i, j, H, W, index);
          index++;
        }
      }
    }

    cout << index - 2 << endl;
    index = 2;
    map.clear();
  }
  return 0;
}