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<cstdio>
using namespace std;

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

int dfs(int x, int y) {
  if (x < 0 || y < 0 || x >= w || y >= h) return 0;
  if (c[y][x] == 0) return 0;
  c[y][x] = 0;
  for (int i = 0; i < 8; i++) dfs(x + dx[i], y + dy[i]);
  return 1;
}

int main() {
  while (1) {
    scanf("%d%d", &w, &h);
    if (w == 0) break;
    for (int i = 0; i < h; i++)
      for (int j = 0; j < w; j++) scanf("%d", &c[i][j]);
    int counter = 0;
    for (int i = 0; i < h; i++)
      for (int j = 0; j < w; j++) counter += dfs(j, i);
    printf("%d\n", counter);
  }
}

C++: //
// Created by elegant-belltree on 15/06/03.
//

#include <iostream>
#include <set>

using namespace std;

int main(){
    int w,h;
    int c[50][50];

    while (cin>>w>>h && (w>0 || h>0)) {
        int label=2;
        set<int> labels;

        for (int n = 0; n < 2; ++n) {
            for (int i = 0; i < h; ++i) {
                for (int j = 0; j < w; ++j) {
                    if(n==0) {
                        cin >> c[j][i];
                    }
                    if (0<c[j][i]) {
                        for (int k = -1; k < n+1 && i+k<h; ++k) {
                            for (int l = -1; (n!=0 || k!=0 || l!=0) && l < 3 && j + l < w; ++l) {
                                if (-1 < i + k && -1 < j + l && 0 < c[j + l][i + k] && (c[j][i] == 1 || c[j + l][i + k] < c[j][i])) {
                                    if(n==1){
                                        labels.erase(c[j][i]);
                                    }
                                    c[j][i] = c[j + l][i + k];
                                }
                            }
                        }
                        if (c[j][i] == 1) {
                            c[j][i] = label++;
                            labels.insert(c[j][i]);
                        }
                    }
                }
            }
        }
        /*for (int m = 0; m < h; ++m) {
            for (int i = 0; i < w; ++i) {
                cout<<c[i][m]<<" ";
            }
            cout<<endl;
        }*/
        cout<<labels.size()<<endl;
    }
    return 0;
}

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

void update(int x,int y,vector< vector<int> >& map){
	if(map[y][x]==1){
		map[y][x]=2;
	}else{
		return;
	}
	if(x!=0)update(x-1,y,map);
	if(y!=0)update(x,y-1,map);
	if(x!=0&&y!=0)update(x-1,y-1,map);
	if(y!=map.size()-1)update(x,y+1,map);
	if(x!=map[0].size()-1)update(x+1,y,map);
	if(y!=map.size()-1&&x!=0)update(x-1,y+1,map);
	if(y!=map.size()-1&&x!=map[0].size()-1)update(x+1,y+1,map);
	if(x!=map[0].size()-1&&y!=0)update(x+1,y-1,map);
}
int main(){
	while(true){
		vector< vector<int> > map;
		int w,h,tmp,sum=0;
		cin>>w>>h;
		if(w==0)break;
		map.resize(h);
		for(int y=0;y<h;y++){
			for(int x=0;x<w;x++){
				map[y].resize(w);
				cin>>tmp;
				map[y][x]=tmp;
			}
		}
		for(int y=0;y<h;y++){
			for(int x=0;x<w;x++){
				if(map[y][x]==1){
					sum++;
					update(x,y,map);
				}
			}
		}
		cout<<sum<<endl;
	}
	return 0;
}

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

typedef pair<int,int> P;

const int dx[] = { 0, 1, 0,-1, 1, 1,-1,-1};
const int dy[] = { 1, 0,-1, 0, 1,-1, 1,-1};
int h, w;
vector<vector<int> > islands(50, vector<int>(50));

void bfs (int i, int j) {
    queue<P> q;
    q.push(P(i,j));
    while (q.size()) {
        const P p = q.front();
        q.pop();
        for (int i = 0; i < 8; i++) {
            int x = p.first + dx[i], y = p.second + dy[i];
            if (0 <= x && x < w && 0 <= y && y < h && islands[x][y]) {
                islands[x][y] = 0;
                q.push(P(x,y));
            }
        }
    }
}

int main ()
{
    while (cin >> h >> w, (h || w)) {
        for (int i = 0; i < w; i++) {
            for (int j = 0; j < h; j++) {
                cin >> islands[i][j];
            }
        }

        int count = 0;
        for (int i = 0; i < w; i++) {
            for (int j = 0; j < h; j++) {
                if (islands[i][j] == 1) {
                    count++;
                    islands[i][j] = 0;
                    bfs(i, j);
                }
            }
        }
        cout << count << endl;
    }


    return 0;
}

C++: #include <bits/stdc++.h>
using namespace std;
#define rep(i, n) for (int i=0; i < (int)(n); i++)
using ull = unsigned long long;
using ll = long long;

struct pos {
    int x, y;
};

int main() {

    while (true) {
        int W, H;
        cin >> W >> H;
        if (W*H == 0) break;

        bool C[W][H] = {};
        rep(y, H) rep(x, W) cin >> C[x][y];

        int cnt = 0;
        stack<pos> s;
        rep(y, H) rep(x, W) {
            if (C[x][y]) {
                cnt++;
                s.push({x, y});
            }
            while (!s.empty()) {
                pos p = s.top();
                s.pop();
                C[p.x][p.y] = false;
                for (int dx=-1; dx<=1; dx++) {
                    for (int dy=-1; dy<=1; dy++) {
                        int nx = p.x+dx, ny = p.y+dy;
                        if ((nx<0) || (nx>=W) || (ny<0) || (ny>=H)) continue;
                        if (C[nx][ny]) {
                            s.push({nx, ny});
                            // cout << p.x << p.y << " " << nx << ny << endl;
                        }
                    }
                }
            }
        }
        cout << cnt << endl;
    }

    return 0;
}