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>
#include <iostream>
#include <fstream>
#include <vector>
#include <string>
#include <cmath>
#include <algorithm>
#include <map>
#include <iomanip>
#include <stdlib.h>
#include <stdio.h>
#include <queue>
#include <deque>
#include <set>
#include <stack>
#include <time.h>
 
using namespace std;
 
typedef long long ll;
typedef long double ld;
typedef pair<int, int> Pii;
typedef pair<int, ll> Pil;
typedef pair<ll, ll> Pll;
typedef pair<ll, int> Pli;
 
const ll MOD = 1e9 + 7;
const ll MOD2 = 998244353;
const ll INF = 1ll << 60;
const double PI = 2 * asin(1);

void yes() {printf("yes\n");}
void no() {printf("no\n");}
void Yes() {printf("Yes\n");}
void No() {printf("No\n");}
void YES() {printf("YES\n");}
void NO() {printf("NO\n");}

int W, H, C[100][100];
const int di[3] = {-1, 0, 1}, dj[3] = {-1, 0, 1};
bool visited[100][100];

int Solve(){
  fill((bool*)visited, (bool*)(visited + H), false);
  for (int i = 0; i < H; i++){
    for (int j = 0; j < W; j++){
      cin >> C[i][j];
    }
  }
  
  int nowi = 0, ans = 0;
  queue <Pii> que;
  for (int i = 0; i < H; i++){
    for (int j = 0; j < W; j++){
      if (C[i][j] == 1){
        que.push(make_pair(i, j));
        nowi = i; visited[i][j] = true;
        ans++;
        break;
      }
    }
    if (!que.empty()) break;
  }
  
  while (!que.empty()){
    Pii Q = que.front(); que.pop();
    int I = Q.first, J = Q.second;

    for (int i = 0; i < 3; i++){
      for (int j = 0; j < 3; j++){
        if (0 <= I + di[i] && I + di[i] < H && 0 <= J + dj[j] && J + dj[j] < W){
          if (C[I + di[i]][J + dj[j]] == 1 && visited[I + di[i]][J + dj[j]] == false){
            que.push(make_pair(I + di[i], J + dj[j]));
            visited[I + di[i]][J + dj[j]] = true;
          }
        }
      }
    }

    if (que.empty()){
      for (int i = nowi; i < H; i++){
        for (int j = 0; j < W; j++){
          if (C[i][j] == 1 && visited[i][j] == false){
            que.push(make_pair(i, j)); ans++; nowi = i;
            visited[i][j] = true; break;
          }
        }
        if (!que.empty()) break;
      }
    }
  }
  cout << ans << endl;
  
  return 0;
}

int main(){
  while (cin >> W >> H){
    if (W == 0 && H == 0) return 0;
    Solve();
  }
  
  return 0;
}

C++: #include <iostream>
#include <vector>
#include <string>

using namespace std;

void fill(vector< vector<int> >& table, int i, int j, int w, int h, int e){
    if(i < 0 || j < 0) return;
    if(i >= h || j >= w) return;
    for(int k=-1; k <= 1; ++k){
        for(int l=-1; l <= 1; ++l){
            if(i+k < 0 || j+l < 0) continue;
            if(i+k >= h || j+l >= w) continue;
            if(table[i+k][j+l] == 1) {
                table[i+k][j+l] = e;
                fill(table, i+k, j+l, w, h, e);
            }
        }
    }
    return;
}

int main(){
    while(true){
        int w, h;
        int cnt = 1;
        cin >> w;
        cin >> h;
        if(w == 0 && h == 0) break;
        vector< vector<int> > table(h, vector <int>(w));
        for(int i=0; i<h; ++i){
            for(int j=0; j<w; ++j){
                cin >> table[i][j];
            }
        }
        for(int i=0; i<h; ++i){
            for(int j=0; j<w; ++j){
                int e = table[i][j];
                if(e == 0) continue;
                else if(e == 1) {
                    cnt++; e = cnt; table[i][j] = e;
                    fill(table, i, j, w, h, e);
                }
            }
        }
        /*
        for(int i=0; i<h; ++i){
            for(int j=0; j<w; ++j){
                cout << table[i][j] << " ";
            }
            cout << endl;
        }*/
        cout << (cnt-1) << endl;
    }
}

C++: #include<stdio.h>
#include<iostream>

int field[51][51];

void land(int x,int y){
	field[x][y]=0;

	for(int xx=-1;xx<=1;xx++){
		for(int yy=-1;yy<=1;yy++){

			if(field[x+xx][y+yy]==1){
				land(x+xx,y+yy);
			}

		}
	}
	return;
}

int main(void){

	int w,h,kazu=0;

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

	for(int i=0;i<h;i++){
		for(int j=0;j<w;j++){
			scanf("%d",&field[i][j]);
		}
	}

	for(int i=0;i<h;i++){
		for(int j=0;j<w;j++){

			if(field[i][j]==1){
				land(i,j);
				kazu++;
			}

		}
	}

	printf("%d",kazu);

}

C++: #include <algorithm>
#include <climits>
#include <cmath>
#include <cstdio>
#include <cstring>
#include <iostream>
#include <queue>
#include <stack>
#include <string>
#include <vector>
#define INF 10000000
#define PI 3.14159265358979323846
using namespace std;
typedef long long ll;
typedef pair<int, int> p;
typedef vector<int> v;
typedef vector<int, int> v2;
typedef vector< pair<int, int> > vp;
int dx[4] = { 0, 1, 0, -1 };
int dy[4] = { 1, 0, -1, 0 };

int w, h;
int c[52][52];
int f[52][52];

bool dfs(int y, int x){
	if (c[y][x] == 0) return false;
	if (f[y][x] == 1) return false;
	
	f[y][x] = 1;
	if (y > 0) dfs(y - 1, x);
	if (x > 0) dfs(y, x - 1);
	
	if (y < h) dfs(y + 1, x);
	if (x < w) dfs(y, x + 1);

	if (y > 0 && x > 0) dfs(y - 1, x - 1);
	if (y < h && x > 0) dfs(y + 1, x - 1);
	if (y > 0 && x < w) dfs(y - 1, x + 1);
	if (y < h && x < w) dfs(y + 1, x + 1);

	return true;
}

int main(){
	ios::sync_with_stdio(false);

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

		int ans = 0;

		for (int i = 0; i < h; i++)
			for (int j = 0; j < w; j++)
				cin >> c[i][j];
			
		for (int i = 0; i < h; i++)
			for (int j = 0; j < w; j++)
				f[i][j] = -1;
		
		for (int i = 0; i < h; i++)
			for (int j = 0; j < w; j++)
				if(dfs(i, j)) ans++;

		cout << ans << endl;

	}

	return 0;
}