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 <cstdio>
#include <cstdlib>
#include <cstring>
#include <cmath>
#include <climits>
#include <vector>
#include <string>
#include <queue>
#include <deque>
#include <list>
#include <stack>
#include <set>
#include <map>
#include <algorithm>

#define int long long
#define MOD7 1000000007
#define MOD9 1000000009

#define rep(i, n) for (int i = 0; i < (n); i++)
#define all(a) (a).begin(), (a).end()

using namespace std;

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

int nextInt() {int a; cin >> a; return a;}
char nextChar() {char a; cin >> a; return a;}
double nextDouble() {double a; cin >> a; return a;}
string nextString() {string a; cin >> a; return a;}

void inputVector(vector<int> &v, int &n) {rep(i,n){v.push_back(nextInt());}}
void inputVector(vector<double> &v, int &n) {rep(i,n){v.push_back(nextDouble());}}
void inputVector(vector<string> &v, int &n) {rep(i,n){v.push_back(nextString());}}

void paint(vector<string> &board, int y, int x) {
	queue<int> q;
	q.push(y * 100 + x);
	board[y][x] = '0';
	while (!q.empty()) {
		int now = q.front(); q.pop();

		int nowX = now % 100;
		int nowY = now / 100;

		for (int ay = -1; ay <= 1; ay++) {
			for (int ax = -1; ax <= 1; ax++) {
				int nx = nowX + ax;
				int ny = nowY + ay;
				if (nx < 0 || nx >= board[0].size() || ny < 0 || ny >= board.size()) continue;
				if (board[ny][nx] == '0') continue;

				q.push(ny * 100 + nx);
				board[ny][nx] = '0';
			}
		}
	}
}

signed main() {
	while (true) {
		int w, h;
		cin >> w >> h;

		if (w == 0) break;

		vector<string> board;
		rep(y, h) {
			string tmp = "";
			rep(x, w) {
				tmp += nextChar();
			}
			board.push_back(tmp);
		}

		int ret = 0;
		rep(y, h) {
			rep(x, w) {
				if (board[y][x] == '1') {
					paint(board, y, x);
					ret++;
				}
			}
		}

		cout << ret << endl;
	}

    return 0;
}

C++: #include<iostream>
#include<algorithm>
#include<map>
#include<set>
#include<vector>
#include<cstring>
#include<string>
#include<queue>
#include<cstdio>
#include<cstdlib>
#include<cmath>
using namespace std;

#define REP(i,a,n) for(int i = a ; i < n  ;i++)
#define rep(i,n) REP(i,0,n)

typedef long long ll;

#define MAX 100

int w, h;
int t[MAX][MAX];

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

bool destroy(int x, int y){
  if(t[y][x] == 0) return false;
  t[y][x] = 0;
  rep(i, 8){
    int nx = x + dx[i], ny = y + dy[i];
    if(nx < 0 || w <= nx || ny < 0 || h <= ny) continue;
    destroy(nx, ny);
  }
  return true;
}

void solve(){
  int res = 0;
  rep(i,h) rep(j,w) if(destroy(j,i)) res++;
  cout << res << endl;
}
int main(){
  while(cin >> w >> h){
    if( w==0 && h ==0) break;
    rep(i, h) rep(j, w) cin >> t[i][j];
    solve();
  }
}

C++: /**
 * @Author: yujirotakahashi
 * @Date:   2020-05-17T20:21:41+09:00
 * @Last modified by:   yujirotakahashi
 * @Last modified time: 2020-05-23T00:13:28+09:00
 */

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

/*******  All Required define Pre-Processors and typedef Constants *******/
#define all(cont) cont.begin(), cont.end()
#define rall(cont) cont.end(), cont.begin()
#define rep(i,n) for(int i=0; i<(n); ++i)
#define rep1(i,n) for(int i=1; i<=(n); ++i)
#define Dcout(a) cout << setprecision(20) << a << endl
#define MP make_pair
#define PB push_back
#define EPS 1e-9
#define PI 3.1415926535897932384626433832795
#define MOD 1000000007
//const ll INF = (1LL << 60);
//const double pi=acos(-1.0);
using ll = long long;
using PAIR = pair<int, int>;
#define debug cout << "line : " << __LINE__ << " debug" << endl
/****** Template of some basic operations *****/
template<class T> inline bool chmin(T& a, T b) { if (a > b) { a = b; return true; } return false; }
template<class T> inline bool chmax(T& a, T b) { if (a < b) { a = b; return true; } return false; }
/**************************************/

/******** User-defined Function *******/
using Graph = vector<vector<int>>;
int w,h;
int c[55][55];
bool seen[55][55];
int dx[8] = {-1,0,1,-1,1,-1,0,1};
int dy[8] = {-1,-1,-1,0,0,1,1,1};
void dfs(int y,int x){
    seen[y][x] = true;

    rep(i,8){
        int nx = x + dx[i], ny = y + dy[i];
        if(nx<0 || nx>=w || ny<0 || ny>=h) continue;
        if(seen[ny][nx] == true) continue;
        if(c[ny][nx] == 0) continue;

        dfs(ny,nx);
    }

    return;
}
/**************************************/

/********** Main()  function **********/
int main(){
  cin.tie(0);
  ios::sync_with_stdio(false);

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

      rep(i,h)rep(j,w) cin >> c[i][j];
      memset(seen, false, sizeof(seen));
      int cnt = 0;
      rep(i,h)rep(j,w){
          if(seen[i][j]==false && c[i][j] == 1){
              dfs(i,j);
              cnt++;
          }
      }

      cout << cnt << endl;
  }

  return 0;
}
/********  Main() Ends Here *************/