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<stdio.h>
void printmap();
void draw(int x,int y);
int w,h,c[50][50];

int main(){
	int i,j;
	int count;


	scanf("%d %d",&w,&h);


	while((w!=0)&&(h!=0)){
		count=0;
	
		for(j=0;j<h;j++){
			for(i=0;i<w;i++){
				scanf("%d",&c[j][i]);
			}
		}
		//printf("\n");
		//printmap();
	
		for(i=0;i<w;i++){
			c[h][i]=0;
		}
		for(i=0;i<h;i++){
			c[i][w]=0;
		}

		for(i=0;i<w;i++){
			for(j=0;j<h;j++){
				if(c[j][i]==1){
					count++;
					//printf("count up count=%d\n",count);
					draw(j,i);
				}
			}
		}

		printf("%d\n",count);
		scanf("%d %d",&w,&h);
	}
	return 0;
}

void draw(int x,int y){
	//printf("delete %d %d\n",x,y);
	c[x][y]=0;
	if(c[x+1][y]==1){
		draw(x+1,y);
	}else if(c[x-1][y]==1){
		draw(x-1,y);
	}else if(c[x][y-1]==1){
		draw(x,y-1);
	}else if(c[x][y+1]==1){
		draw(x,y+1);
	}

}

void printmap(){
	int i,j;
	for(j=0;j<h;j++){
		for(i=0;i<w;i++){
			printf("%d ",c[j][i]);
		}
		printf("\n");
	}
}

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

bool isprime(int n){
    double rootn = sqrt(n);
    if (n < 2){
        return false;
    }
    else if (n == 2){
        return true;
    }
    else if (n % 2 == 0){
        return false;
    }
    else{
        for(int i = 3; i <= rootn; i += 2){
            if(n % i == 0){
                return false;
            }
        }
        return true;
    }
}

// 空のベクトルを二つ渡す。N (>=2) 以下の素数を primes に順番に入れる。
void prime_vectors(int N, vector<int>& primes, vector<bool>& is_prime){
    primes = {};
    is_prime = {};
    for(int i=0; i<=N; i++){
        is_prime.push_back(true);
    }

    primes.push_back(2);
    for(int j=4; j<=N; j+=2){
        is_prime[j] = false;
    }

    int i;
    for(i=3; i*i<=N; i+=2){
        if(!is_prime[i]) continue;
        primes.push_back(i);
        for(int j=2*i; j<=N; j+=i){
            is_prime[j] = false;
        }
    }

    for(; i<=N; i+=2){
        if(is_prime[i]){
            primes.push_back(i);
        }
    }
}

signed main(){
    // cout << fixed << setprecision(10) << flush;

    int w, h;
    cin >> w >> h;
    vector<int> n_cc;
    while(w != 0 && h != 0){
        vector<vector<int>> c(h+2, vector<int>(w+2, 0));
        for(int i=1; i<=h; i++){
            for(int j=1; j<=w; j++){
                cin >> c[i][j];
            }
        }

        int visited[h+2][w+2] = {0};
        stack<pair<int, int>> stk;
        int cc = 0;
        for(int i=1; i<=h; i++){
            for(int j=1; j<=w; j++){
                if(c[i][j] == 0 || visited[i][j] == 1) continue;
                visited[i][j] = 1;
                stk.push(make_pair(i, j));
                while(!stk.empty()){
                    int i0 = stk.top().first, j0 = stk.top().second;
                    stk.pop();
                    for(int id=-1; id<=1; id++){
                        for(int jd=-1; jd<=1; jd++){
                            if(c[i0+id][j0+jd] == 1 && visited[i0+id][j0+jd] == 0){
                                visited[i0+id][j0+jd] = 1;
                                stk.push(make_pair(i0+id, j0+jd));
                            }
                        }
                    }
                }

                cc++;
            }
        }
        n_cc.push_back(cc);

        cin >> w >> h;
    }

    for(int i=0; i<n_cc.size(); i++){
        cout << n_cc[i] << endl;
    }

    return 0;
}

C++: #include <iostream>
#include <queue>
using namespace std;
struct Point{int x,y;};
int main(){
  int w,h,p[60][60]={},v[60][60]={},i,j,k,o;
  queue <Point> q;
  Point n,b;
  cin >> w >> h;
  while(w!=0){
    for(j=0;j<h;j++){
      for(i=0;i<w;i++){
	cin >> p[i][j];
	v[i][j]=0;
      }
    }
    o=0;
    for(j=0;j<h;j++){
      for(i=0;i<w;i++){
	if(v[i][j]==0&&p[i][j]==1){
	  o++;
	  n.x=i;n.y=j;
	  q.push(n);
	  while(!q.empty()){
	    n=q.front();
	    q.pop();
	    v[n.x][n.y]=1;
	    if(n.x+1<w&&v[n.x+1][n.y]==0&&p[n.x+1][n.y]==1) {
	      b.x=n.x+1;b.y=n.y;
	      q.push(b);
	    }
	    if(n.y+1<h&&v[n.x][n.y+1]==0&&p[n.x][n.y+1]==1) {
	      b.x=n.x;b.y=n.y+1;
	      q.push(b);
	    }
	    if(n.x-1>=0&&v[n.x-1][n.y]==0&&p[n.x-1][n.y]==1) {
	      b.x=n.x-1;b.y=n.y;
	      q.push(b);
	    }
	    if(n.y-1>=0&&v[n.x][n.y-1]==0&&p[n.x][n.y-1]==1) {
	      b.x=n.x;b.y=n.y-1;
	      q.push(b);
	    }
	    if(n.x+1<w&&n.y+1<h&&v[n.x+1][n.y+1]==0&&p[n.x+1][n.y+1]==1) {
	      b.x=n.x+1;b.y=n.y+1;
	      q.push(b);
	    }
	    if(n.x-1>=0&&n.y-1>=0&&v[n.x-1][n.y-1]==0&&p[n.x-1][n.y-1]==1) {
	      b.x=n.x-1;b.y=n.y-1;
	      q.push(b);
	    }
	    if(n.x+1<w&&n.y-1>=0&&v[n.x+1][n.y-1]==0&&p[n.x+1][n.y-1]==1) {
	      b.x=n.x+1;b.y=n.y-1;
	      q.push(b);
	    }
	    if(n.x-1>=0&&n.y+1<h&&v[n.x-1][n.y+1]==0&&p[n.x-1][n.y+1]==1) {
	      b.x=n.x-1;b.y=n.y+1;
	      q.push(b);
	    }
	  }
	}
      }
    }
    cout << o << endl;
    cin >> w >> h;
  }
  return 0;
}