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>
using namespace std;
#define int long long
#define All(v) (v).begin(),(v).end()
int dy[8] = {-1,0,1,0,-1,1,1,-1};
int dx[8] = {0,1,0,-1,1,1,-1,-1};

int h,w,cnt=0;
int mas[55][55];
void dfs(int y,int x){
    mas[y][x]=0;
    for(int i=0;i<8;i++){
        int ny=y+dy[i];
        int nx=x+dx[i];
        if(ny<0||nx<0||ny>=h||nx>=w)continue;
        if(mas[ny][nx]==1){
            dfs(ny,nx);
        }
    }
}

signed main(){
    ios::sync_with_stdio(false);
    cin.tie(0);
    while(true){
        cin>>w>>h;
        if(h==0&&w==0)break;
        for(int i=0;i<h;i++){
            for(int j=0;j<w;j++){
                cin>>mas[i][j];
            }
        }
        cnt=0;
        for(int i=0;i<h;i++){
            for(int j=0;j<w;j++){
                if(mas[i][j]==1){
                    cnt++;
                    dfs(i,j);
                }
            }
        }
        cout<<cnt<<endl;
    }
}

C++: #include <bits/stdc++.h>
#define rep(i,a,n) for(int i=a;i<n;i++)
#define repr(i,a,n) for(int i=a;i>=n;i--)
#define INF 999999999
#define pb(a) push_back(a)

using namespace std;
typedef pair<int, int> pii;
typedef long long int ll;
typedef vector<pii> VP;

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

int w,h;

int dfs(int i, int j, int c[][51]) {
    if(c[i][j] == 1) {
        c[i][j] = 0;
        rep(di,i-1,i+2) {
            rep(dj,j-1,j+2) {
                if(di != i || dj != j) {
                    if(di<0 || di>=h || dj<0 || dj>=w) continue;
                    if(c[di][dj] == 1) dfs(di,dj,c);
                }
            }
        }
        return 1;
    }
    return 0;
}

int main() {
    int c[51][51];

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

        rep(i,0,h) {
            rep(j,0,w) {
                cin >> c[i][j];
            }
        }

        int ret = 0;
        rep(i,0,h) {
            rep(j,0,w) {
                ret += dfs(i,j,c);
            }
        }

        cout << ret << endl;
    }
    return 0;
}

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

using namespace std;

using ll = long long;

#define REP(i,n) for(ll i=0;i<(ll)(n);i++)
#define REPD(i,n) for(ll i=n-1;i>=0;i--)
#define FOR(i,a,b) for(ll i=a;i<=(ll)(b);i++)
#define FORD(i,a,b) for(ll i=a;i>=(ll)(b);i--)

#define input(...) __VA_ARGS__; in(__VA_ARGS__)

template<class T>
void print(vector<T> a) {
  cout << "[ ";
  REP(i, a.size()) cout << a[i] << " ";
  cout << "]" << endl;
}

void print() {
  std::cout << std::endl;
}

template <class Head, class... Tail>
void print(Head&& head, Tail&&... tail) {
  std::cout << head << " ";
  print(std::forward<Tail>(tail)...);
}

void in() { }

template <class Head, class... Tail>
void in(Head&& head, Tail&&... tail) {
  cin >> head;
  in(std::forward<Tail>(tail)...);
}

vector<pair<ll, ll>> d = {
  {-1, -1},
  {+1, -1},
  {-1, +1},
  {+1, +1},
  {+0, +1},
  {+0, -1},
  {+1, +0},
  {-1, +0},
};

void dfs(ll x, ll y, vector<vector<ll>> &graph) {
  graph[y][x] = 0;
  for(auto v: d) {
    ll nx = v.first + x;
    ll ny = v.second + y;
    if (0 <= ny && ny < graph.size() && 0 <= nx && nx < graph[y].size() && graph[ny][nx]) {
      dfs(nx, ny, graph);
    }
  }
}

bool solve() {
  ll input(w, h);
  if (w == 0 && h == 0) return false;

  vector<vector<ll>> graph(h);
  REP(i, h) {
    vector<ll> r(w);
    REP(j, w) cin >> r[j];
    graph[i] = r;
  }

  ll cnt = 0;
  REP(i, h) REP(j, w) {
    if (graph[i][j]) {
      dfs(j, i, graph);
      cnt++;
    }
  }
  cout << cnt << endl;
  return true;
}

int main() {
  while (solve());
}

C++: #include<bits/stdc++.h>
#define LL long long
#define REP(i,n) for(int i=0;i<(n);++i)
#define REPA(i,n) for(int i=1;i<(n);++i)
#define foreach(i, n) for(auto &i:(n))
#define PII pair<int,int>
#define PLI pair<long long, int>
#define PLL pair<long long, long long>
#define PDD pair<double, double>
#define MOD ((int)1e9 + 7)
#define INF ((int)1e9)
#define INFLL ((LL)1e18)
#define ALL(x) (x).begin(),(x).end()
#define ctoi(x) (x - 'a') 
#define CTOI(x) (x - 'A')
#define BIT(x) (1 << (x))
using namespace std;

LL modinv(LL a){
    LL b = MOD, u = 1, v = 0;
    while(b){
        LL t = a / b;
        a -= t * b;swap(a, b);
        u -= t * v;swap(u, v);
    }
    u%=MOD;
    if(u < 0) u += MOD;
    return u;

}

LL exp(LL a, LL b){
    LL sum = a;
    LL res = 1;
    while(b){
        if(b&1){
            res = (res * sum)%MOD;
        }
        sum = (sum * sum) % MOD;
        b>>=1;
    }
    return res;
}

int N, M;
int mx[]={1, 1, 1, 0, -1, -1, -1, 0}, my[]={1, 0, -1, -1, -1, 0, 1, 1};

int main(){
    while(cin>>M>>N&&N){
        vector<vector<int> > m(N+2, vector<int>(M+2, 0));
        REPA(i, N+1){
            REPA(j, M+1){
                cin >> m[i][j];
            }
        }
        int ans = 0;
        REPA(i, N+1){
            REPA(j, M+1){
                if(!m[i][j])continue;
                ++ans;
                queue<PII> que;
                que.push(PII(i, j));
                m[i][j]=0;
                while(!que.empty()){
                    PII d = que.front();
                    que.pop();
                    REP(i, 8){
                        int nx = d.first + mx[i];
                        int ny = d.second + my[i];
                        if(!m[nx][ny])continue;
                        m[nx][ny]=0;
                        que.push(PII(nx, ny));
                    }
                }
            }
        }
        cout << ans << endl;
    }
    return 0;
}