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<iostream>
#include<queue>
#include<utility>
using namespace std;

int W,H;
int c[51][51]={0};
int d[51][51]={0};
int island=0;

bool valid(pair<int,int> a){
    return (a.first>=1 && a.first<=W && a.second>=1 && a.second<=H);
}

template<typename A, size_t N, typename T>
void Fill(A (&array)[N], const T &val){
    std::fill( (T*)array, (T*)(array+N), val );
}

void bfs(pair<int,int> s){
    queue<pair<int,int>> Q;

    if(c[s.first][s.second]==1 && d[s.first][s.second]==0 && valid(s)){
        Q.push(s);
        d[s.first][s.second]=1;
        ++island;

        while(!Q.empty()){
            pair<int,int> cur=Q.front();
            Q.pop();

            for(int i=-1;i<=1;++i){
                for(int j=-1;j<=1;++j){
                    pair<int,int> dst=make_pair(cur.first+i,cur.second+j);

                    if(d[dst.first][dst.second]==0 && c[dst.first][dst.second]==1 && valid(dst)){
                        Q.push(dst);
                        d[dst.first][dst.second]=1;
                    }
                }
            }
        }
    }
}

int main(){
    
    while(cin>>W>>H && W!=0){
        for(int i=1;i<=H;++i){
            for(int j=1;j<=W;++j){
                cin>>c[j][i];
            }
        }

        for(int i=1;i<=W;++i){
            for(int j=1;j<=H;++j){
                pair<int,int> start=make_pair(i,j);
                bfs(start);
            }
        }
        cout<<island<<"\n";
        island=0;
        Fill(c,0);
        Fill(d,0);
    }
}

C++: #include <bits/stdc++.h>
using namespace std;

template <class T>
void readVector(int n, vector<T> &v){
	for(int i=0; i<n; ++i){
		T t;
		cin >> t;
		v.push_back(t);
	}
}
int dx[8] = {0, 1, 1, 1, 0, -1, -1, -1};
int dy[8] = {1, 1, 0, -1, -1, -1, 0, 1};
typedef pair<long long, long long> P;

void solve(int w, int h){
	vector<int> v[h];
	for(int i=0; i<h; ++i)
		readVector(w, v[i]);
	int ans = 0;
	for(int i=0; i<h; ++i)
		for(int j=0; j<w; ++j){
			if(v[i][j] == 1){
				++ans;
				v[i][j] = 0;
				queue<P> q;
				q.push(P(i, j));
				while(!q.empty()){
					P p = q.front();
					q.pop();
					int nx = p.first;
					int ny = p.second;
					for(int k=0; k<8; ++k)
						if(0<=nx+dx[k] && nx+dx[k]<h && 0<=ny+dy[k] && ny+dy[k]<w && v[nx+dx[k]][ny+dy[k]] == 1){
							v[nx+dx[k]][ny+dy[k]] = 0;
							q.push(P(nx+dx[k], ny+dy[k]));
						}
				}
			}
		}
	cout << ans << "\n";
}

int main(){
//	cin.tie(0);
//	ios::sync_with_stdio(false);
	while(1){
		int w, h;
		cin >> w >> h;
		if(w == 0 && h == 0) return 0;
		solve(w, h);
	}
}

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

int map[20][20];

typedef pair<int, int> pxy;

int h, w, ans = 0;

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

void bfs(int sx, int sy)
{
  queue<pxy> q;
  q.push(pxy(sx, sy)); 
  while(!q.empty()){
    int x, y;
    x = q.front().first;
    y = q.front().second;
    q.pop();
    if(map[x][y]){
      map[x][y] = 0;
      for(int i = 0; i < 8; i++){
	int next_x = x + dx[i], next_y = y + dy[i];
	if(map[next_x][next_y] && next_x < w && next_x >= 0 && next_y < h && next_y >= 0){
	  q.push(pxy(next_x, next_y));
	}
      }
    }
  }
}

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

C++: #include <bits/stdc++.h>
using namespace std;

const int dx[8]={1,0,-1,0,1,1,-1,-1};
const int dy[8]={0,1,0,-1,1,-1,1,-1};
bool seen[50][50];
void dfs(vector<vector<int>> C, int h, int w, int a, int b){
    seen[a][b]=true;
    for (int i=0; i<8; i++) {
        int aa=a+dx[i];
        int bb=b+dy[i];
        if (aa<0 || bb<0 || aa>=h || bb>=w) continue;
        if (C[aa][bb]==0) {
            continue;
        }
        if (seen[aa][bb]==true) continue;
        dfs(C,h,w,aa,bb);
    }
}

int main() {
    int w,h;
   cin >> w >> h;
   int total=0;
   vector<int> x;
   while(w!=0 || h!=0) {
       total++;
   vector<vector<int>> C(h,vector<int>(w));
   for (int i=0; i<h; i++) {
       for (int j=0; j<w; j++) {
           int p;
           cin >> p;
           C[i][j]=p;
       }
   }
   int count=0;

   for (int i=0; i<h; i++) {
       for (int j=0; j<w; j++) {
           seen[i][j]=false;
       }
   }

   for (int i=0; i<h; i++) {
       for (int j=0; j<w; j++) {
           if (seen[i][j]==true) continue;
           if (C[i][j]==0) continue;
           count++;
           dfs(C,h,w,i,j);
           
       }
   }


   x.push_back(count);
   int a,b;
   cin >> a >> b;
   w=a,h=b;
   }
   for (int i=0; i<total; i++) {
       cout << x[i] << endl;
   }
}