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 <algorithm>
#include <iostream>
#include <limits.h>
#include <stdlib.h>
#include <string>
#include <vector>

#define el endl
#define fd fixed
#define INF INT_MAX/2-1
#define pb push_back

using namespace std;

bool island[50][50];
bool visited[50][50];

void visit(int x, int y) {
  visited[y][x] = true;
  if (x > 0 && island[y][x-1] && !visited[y][x-1]) visit(x-1, y);
  if (x < 49 && island[y][x+1] && !visited[y][x+1]) visit(x+1, y);
  if (y > 0 && island[y-1][x] && !visited[y-1][x]) visit(x, y-1);
  if (y < 49 && island[y+1][x] && !visited[y+1][x]) visit(x, y+1);
  if (x > 0 && y > 0 && island[y-1][x-1] && !visited[y-1][x-1]) visit(x-1, y-1);
  if (x > 0 && y < 49 && island[y+1][x-1] && !visited[y+1][x-1]) visit(x-1, y+1);
  if (x < 49 && y > 0 && island[y-1][x+1] && !visited[y-1][x+1]) visit(x+1, y-1);
  if (x < 49 && y < 49 && island[y+1][x+1] && !visited[y+1][x+1]) visit(x+1, y+1);
}

int main() {
  int w, h, tmp, cnt;
  while (cin >> w >> h, w | h) {
    for (int i = 0; i < 50; i++) {
      for (int j = 0; j < 50; j++) {
        island[i][j] = false;
        visited[i][j] = false;
      }
    }
    cnt = 0;

    for (int i = 0; i < h; i++) {
      for (int j = 0; j < w; j++) {
        if (cin >> tmp, tmp)
          island[i][j] = true;
      }
    }
    for (int i = 0; i < h; i++) {
      for (int j = 0; j < w; j++) {
        if (island[i][j] && !visited[i][j]) {
          visit(j, i);
          cnt++;
        }
      }
    }
    cout << cnt << el;
  }
}

C++: #include <iostream>
#include <map>
// #include <cstdio>
#include <stacK>

using namespace std;

int main() {
	while(1) {
	int w, h, data[52][52]={{0}};
	cin >> w >> h;
	if (w == 0 && h ==0) break;
	// memset(data, 0, sizeof(data));
	for (int i=1; i<=h; i++) {
		for (int j=1; j<=w; j++) {
			cin >> data[i][j];
		}
	}
	// for (int i=0; i<h; i++) {
	// 	for (int j=0; j<w; j++) {
	// 		cout << data[i][j] << "," ;
	// 	}cout <<endl;
	// }
	stack<pair<int, int> > s;
	int count=0;
	int H[8] = {-1, -1, -1, 0, 0, 1, 1, 1}, W[8] ={-1, 0, 1, -1, 1, -1, 0, 1};
	for(int i=1; i<=h; i++) {
		for (int j=1; j<=w; j++) {
			if (data[i][j]) {
				count++;
				s.push(make_pair(i, j));
				while (!(s.empty())) {
					pair<int, int> now;
					now = s.top();
					s.pop();
					data[now.first][now.second]=0;
					for (int k=0; k<8; k++) {
						if (data[H[k]+now.first][W[k]+now.second])
							s.push(make_pair(H[k]+now.first, W[k]+now.second));
					}
				}
			}
		}
	}
	cout << count <<endl;
}
}

C++: #include<iostream>
#include<algorithm>
#include<vector>
#include<map>
#include<set>
#include<string>
#include<queue>
#include<stack>
using namespace std;
#define INF (1 << 30) - 1
#define LINF 1LL << 60
const int MOD = 1000000007;
using ll = long long;
using P = pair<int, int>;

vector<int> dx{1, 0, -1, 0, 1, -1, 1, -1};
vector<int> dy{0, 1, 0, -1, -1, 1, 1, -1};

vector<vector<int>> g;
int w, h;

vector<vector<bool>> seen;
void dfs(int x, int y){
  seen[y][x] = true;
  for(int i = 0; i < 8; ++i){
    int nx = x + dx[i];
    int ny = y + dy[i];
    if(nx < 0 || w <= nx || ny < 0 || h <= ny)continue;
    else if(!g[ny][nx])continue;
    else if(seen[ny][nx])continue;
    dfs(nx, ny);
  }
}

int main(){
  while(true){
    cin >> w >> h;
    if(w == 0 && h == 0)return 0;
    g = vector<vector<int>>(h, vector<int>(w));
    for(int i = 0; i < h; ++i){
      for(int j = 0; j < w; ++j){
        cin >> g[i][j];
      }
    }

    seen = vector<vector<bool>>(h, vector<bool>(w, false));
    int cnt = 0;
    for(int i = 0; i < h; ++i){
      for(int j = 0; j < w; ++j){
        if(seen[i][j])continue;
        if(!g[i][j])continue;
        dfs(j, i);
        ++cnt;
      }
    }

    cout << cnt << endl;
  }

  return 0;
}

C++: #include <iostream>
#include <string>
#include <algorithm>
using namespace std;
#define FOR(i,a,b) for(int i=a;i<b;i++)
#define REP(i,b) FOR(i,0,b)
#define INF 1e9

int w,h,c[50][50],i_num;
int dx[] = {1,0,-1}, dy[] = {1,0,-1};

void search(int x, int y){
  if(c[y][x] == 1 && x >=0 && x < w && y >=0 && y < h){
    c[y][x] = i_num;
    REP(i,3) REP(j,3){
      if(i != 0 || j != 0 || x-1 >= 0 || x+1 < w || y-1 >= 0 || y+1 < h)
      search(x+dx[i],y+dy[j]);
    }
  }
}

void solve(int X, int Y){
  i_num = 2;
  REP(i,20) REP(j,20) c[i][j] = 0;
  //0:ocean, 1:land, 2~:island_number

  REP(i,Y){
    REP(j,X){
      cin>>c[i][j];
    }
  }

  REP(i,Y){
    REP(j,X){
      if(c[i][j] == 1){
        search(j,i);
        i_num++;
      }
    }
  }

  cout<<i_num-2<<endl;
  /*REP(i,20){
     REP(j,20) cout<<c[i][j];
     cout<<endl;
  }*/
}

int main(){

  while(1) {
    cin>>w>>h;
    if(w == 0 && h == 0) break;
    solve(w,h);
  }

  return 0;
}