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;

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;
}