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 <string>
#include <cstring>
#include <fstream>
#include <random>
#include <cmath>
#include <iomanip>
#include <climits>
#include <cstdlib>
#include <algorithm>
#include <vector>
#include <map>
#include <deque>
#include <map>
#include <set>
#include <queue>
#include <stack>
#include <list>
#include <unordered_map>
#include <bitset>
#include <sstream>
#include <new>
#include <typeinfo>
#include <iterator>

typedef long long ll;
typedef unsigned long long ull;
constexpr ll mop = 1000000007;
constexpr ll mop2 = 998244353;

using namespace std;

void dfs(const ll& x, const ll& y, vector<vector<bool>>& grid, const ll& h, const ll & w) {
	grid[x][y] = false;
	for (ll dx = -1; dx <= 1; dx++) {
		for (ll dy = -1; dy <= 1; dy++) {
			ll nextx = x + dx;
			ll nexty = y + dy;
			if (0 <= nextx && nextx < h && 0 <= nexty && nexty < w && grid[nextx][nexty] == true) {
				dfs(nextx, nexty, grid, h, w);
			}
		}
	}
}

int main() {
	while (true) {
		ll w, h;
		cin >> w >> h;
		if (w == 0 && h == 0) {
			break;
		}
		vector<vector<bool>> grid(h);
		for (ll i = 0; i < h; i++) {
			grid[i].resize(w);
			for (ll j = 0; j < w; j++) {
				ll tmp;
				cin >> tmp;
				grid[i][j] = (tmp == 1 ? true : false);
			}
		}
		ll ans = 0;
		for (ll i = 0; i < h; i++) {
			for (ll j = 0; j < w; j++) {
				if (grid[i][j] == true) {
					ans++;
					dfs(i, j, grid, h, w);
				}
			}
		}
		cout << ans << endl;
	}
}

C++: #include <iostream>
#include <string>
#include <utility>
#include <stack>
#include <vector>
#include <queue>
#include <algorithm>
#include <map>
#include <climits>
#include <set>
#include <numeric>
#include <iomanip>
#include <cmath>
#include <cstring>
using namespace std;
int grid[50][50];
bool used[50][50];
int move_h[8] = {-1, 1, 0, 0, -1, 1, -1, 1};
int move_w[8] = {0, 0, -1, 1, 1, -1, -1, 1};

void dfs(int cur_h, int cur_w, int h, int w){
  used[cur_h][cur_w] = true;
  for(int i = 0; i < 8; i++){
    int next_h = cur_h + move_h[i];
    int next_w = cur_w + move_w[i];

    if(0 <= next_h && next_h < h && 0 <= next_w && next_w < w 
        && !used[next_h][next_w] && grid[next_h][next_w] == 1){
      dfs(next_h, next_w, h, w);
    }
  }
}
int main(){
  while(true){
    int w;
    int h;
    cin >> w >> h;
    if(w == 0 && h == 0){
      break;
    }
    for(int i = 0; i < h; i++){
      for(int j = 0; j < w; j++){
        cin >> grid[i][j];
        used[i][j] = false;
      }
    }

    int ans = 0;
    for(int i = 0; i < h; i++){
      for(int j = 0; j < w; j++){
        if(!used[i][j] && grid[i][j] == 1){
          ans ++;
          dfs(i, j, h, w);
        }
      }
    }
    cout << ans << endl;
  }
  return 0;
}

C++: #include<iostream>
using namespace std;
void dfs(int,int);
int box[51][51];
int main(){
  int w,h,i,I,a;
  while(1){
    a=0;
    cin>>w>>h;
    if(w==0&&h==0) break;
    for(i=0;i<=h+1;i++){
      for(I=0;I<=w+1;I++){
	box[i][I]==0;
      }
    }
    for(i=1;i<=h;i++){
      for(I=1;I<=w;I++){
	cin>>box[i][I];
      }
    }
    for(i=1;i<=h;i++){
      for(I=1;I<=w;I++){
	if(box[i][I]==1){
	  dfs(i,I);
	  a++;
	}
      }
    }
    cout<<a<<endl;
  }
  return 0;
}
  void dfs(int i,int I){
  if(box[i][I]==0) return;
  box[i][I]=0;
  dfs(i,I+1);
  dfs(i+1,I+1);
  dfs(i+1,I);
  dfs(i+1,I-1);
  dfs(i,I-1);
  dfs(i-1,I-1);
  dfs(i-1,I);
  dfs(i-1,I+1);
  }

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

typedef long long ll;
typedef pair<int,int> P;

bool ok=true;
int c[60][60];
int w,h;
bool seen[60][60];
int dx[8]={1,-1,0,0,1,-1,1,-1};
int dy[8]={1,-1,1,-1,0,0,-1,1};

void dfs(int v,int u){
	if(!seen[v][u]){
		seen[v][u]=true;
		for(int i=0;i<8;i++){
			if(v+dx[i]>=0&&v+dx[i]<h&&u+dy[i]>=0&&u+dy[i]<w&&c[v+dx[i]][u+dy[i]]==1)dfs(v+dx[i],u+dy[i]);
		}
	}
	return;
}

int main(){
	while(ok){
		cin>>w>>h;
		if(w==0||h==0){
			ok=false;
			return 0;
		}
		for(int i=0;i<h;i++){
			for(int j=0;j<w;j++){
				cin>>c[i][j];
			}
		}
		for(int i=0;i<60;i++){
			for(int j=0;j<60;j++)seen[i][j]=false;
		}
		int ans=0;
		for(int i=0;i<h;i++){
			for(int j=0;j<w;j++){
				if(c[i][j]==1&&!seen[i][j]){
					ans++;
					dfs(i,j);
				}
			}
		}
		cout<<ans<<endl;
	}
	return 0;
}