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 <queue>
#include <stack>
#include <algorithm>
#include <list>
#include <vector>
#include <complex>
#include <utility>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <cmath>
#include <climits>
#include <bitset>
#include <ctime>
#include <map>
#include <unordered_map>
#include <set>
#include <unordered_set>
#include <cassert>
#include <cstddef>
#include <iomanip>
#include <numeric>
#include <tuple>
#include <sstream>
#include <fstream>

using namespace std;
#define REP(i, n) for (int (i) = 0; (i) < (n); (i)++)
#define FOR(i, a, b) for (int (i) = (a); (i) < (b); (i)++)
#define RREP(i, a) for(int (i) = (a) - 1; (i) >= 0; (i)--)
#define FORR(i, a, b) for(int (i) = (a) - 1; (i) >= (b); (i)--)
#define DEBUG(C) cerr << #C << " = " << C << endl;
using LL = long long;
using VI = vector<int>;
using VVI = vector<VI>;
using VL = vector<LL>;
using VVL = vector<VL>;
using VD = vector<double>;
using VVD = vector<VD>;
using PII = pair<int, int>;
using PDD = pair<double, double>;
using PLL = pair<LL, LL>;
using VPII = vector<PII>;
#define ALL(a) begin((a)), end((a))
#define RALL(a) rbegin((a)), rend((a))
#define SORT(a) sort(ALL((a)))
#define RSORT(a) sort(RALL((a)))
#define REVERSE(a) reverse(ALL((a)))
#define MP make_pair
#define FORE(a, b) for (auto &&a : (b))
#define FIND(s, e) ((s).find(e) != (s).end())
#define EB emplace_back

const int INF = 1e9;
const int MOD = INF + 7;
const LL LLINF = 1e18;

VVI C;
vector<vector<bool>> used;
int MH, MW;
int dh[] = {0, 0, -1, 1, -1, -1, 1, 1};
int dw[] = {-1, 1, 0, 0, -1, 1, -1, 1};
bool inside(int h, int w) {
    return 0 <= h && h < MH && 0 <= w && w < MW;
}
bool dfs(int h, int w) {
    if (used[h][w]) return false;
    used[h][w] = true;
    REP(i, 8) {
        int nh = h + dh[i], nw = w + dw[i];
        if (!inside(nh, nw)) continue;
        if (C[nh][nw] == 1) dfs(nh, nw);
    }
    return true;
}
void solve(int w, int h) {
    MH = h; MW = w;
    C.resize(h, VI(w));
    used.resize(h, vector<bool>(w, false));
    REP(i, h)REP(j, w) cin >> C[i][j];
    int cnt = 0;
    REP(i, h)REP(j, w) {
        if (C[i][j] == 1) cnt += dfs(i, j);
    }
    cout << cnt << endl;
    used.clear();
    C.clear();
    return;
}

int main(void) {
    int n, p, m, w, h;
    string s;
    int a, b, c;
    while (cin >> w >> h) {
        if (w == 0 && h == 0) return 0;
        solve(w, h);
        //return 0;
    }
}

C++: #include<cstdio>
#include<cstdlib>
#include<cstring>
#include<cmath>
#include<cassert>
#include<iostream>
#include<sstream>
#include<string>
#include<vector>
#include<queue>
#include<set>
#include<map>
#include<utility>
#include<numeric>
#include<algorithm>
#include<bitset>
#include<complex>

using namespace std;

typedef long long Int;
typedef vector<int> vint;
typedef pair<int,int> pint;
#define mp make_pair

template<class T> void pv(T a, T b) { for (T i = a; i != b; ++i) cout << *i << " "; cout << endl; }
template<class T> void chmin(T &t, T f) { if (t > f) t = f; }
template<class T> void chmax(T &t, T f) { if (t < f) t = f; }
int in() { int x; scanf("%d", &x); return x; }

int sea[60][60];
int w,h;

void f(int x,int y){
//cout<<x<<" "<<y<<endl;
	int i,j;
/*for(i=0;i<h;i++){
	for(j=0;j<w;j++){
		cout<<sea[i][j]<<" ";
	}
	cout<<endl;
}*/
	sea[y][x]=0;
	for(i=-1;i<2;i++){
		for(j=-1;j<2;j++){
			if(y+i<0||h<=y+i||x+j<0||w<=y+i)continue;
			if(sea[y+i][x+j]==1)f(x+j,y+i);
		}
	}
}

int main() {
	while(w=in(),h=in()){
		int i,j;
		for(i=0;i<h;i++){
			for(j=0;j<w;j++){
				cin>>sea[i][j];
			}
		}
		/*for(i=0;i<h;i++){
			for(j=0;j<w;j++){
				cout<<sea[i][j]<<" ";
			}
			cout<<endl;
		}*/
		int res=0;
		for(i=0;i<h;i++){
			for(j=0;j<w;j++){
				if(sea[i][j]==1){
					f(j,i);
					res++;
				}
			}
		}
		cout<<res<<endl;
	}
	return 0;
}

C++: #include <bits/stdc++.h>
using namespace std;
#define int long long
#define Rep(i, N) for(int i = 0; i < N; i++)

typedef pair<int, int> Pi;
#define fi first
#define se second

int mx[] = {1, 1, 0, -1, -1, -1, 0, 1};
int my[] = {0, 1, 1, 1, 0, -1, -1, -1};
int W, H;
int mas[55][55];

void bfs(int sy, int sx) {
  
  queue < Pi > que;
  que.push(Pi(sy, sx));

  while(!que.empty()) {
    Pi d = que.front(); que.pop();
    int y = d.fi, x = d.se;
    Rep(i, 8) {
      int ny = y + my[i], nx = x + mx[i];
      if(ny < 0 || nx < 0 || ny >= H || nx >= W || !mas[ny][nx]) continue;
      mas[ny][nx] = 0; bfs(ny, nx);
    }
  }
}

signed main()
{  
  while(cin >> W >> H, W || H) {
    int ans = 0;
    Rep(i, H) Rep(j, W) cin >> mas[i][j];

    Rep(i, H) Rep(j, W) {
      if(mas[i][j]) {
	mas[i][j] = 0, ans++;
	bfs(i, j);
      }
    }

    cout << ans << endl;
  }
}