Go: package main

import (
	"bufio"
	"fmt"
	"io"
	"math"
	"os"
	"strconv"
)

var (
	readString func() string
	readBytes  func() []byte
	stdout     *bufio.Writer
)

func init() {
	readString, readBytes = newReadString(os.Stdin)
	stdout = bufio.NewWriter(os.Stdout)
}

func newReadString(ior io.Reader) (func() string, func() []byte) {
	r := bufio.NewScanner(ior)
	r.Buffer(make([]byte, 1024), math.MaxInt32)
	r.Split(bufio.ScanWords)

	f1 := func() string {
		if !r.Scan() {
			panic("Scan failed")
		}
		return r.Text()
	}
	f2 := func() []byte {
		return []byte(f1())
	}
	return f1, f2
}

func readInt() int {
	return int(readInt64())
}

func readInt64() int64 {
	i, err := strconv.ParseInt(readString(), 10, 64)
	if err != nil {
		panic(err.Error())
	}
	return i
}

func readFloat64() float64 {
	f, err := strconv.ParseFloat(readString(), 64)
	if err != nil {
		panic(err.Error())
	}
	return f
}

func printf(f string, args ...interface{}) (int, error) {
	return fmt.Fprintf(stdout, f, args...)
}

func println(args ...interface{}) (int, error) {
	return fmt.Fprintln(stdout, args...)
}

func eprintln(args ...interface{}) (int, error) {
	return fmt.Fprintln(os.Stderr, args...)
}

func eprintf(f string, args ...interface{}) (int, error) {
	return fmt.Fprintf(os.Stderr, f, args...)
}

func min(a, b int) int {
	if a < b {
		return a
	}
	return b
}

func min64(a, b int64) int64 {
	if a < b {
		return a
	}
	return b
}

func max(a, b int) int {
	if a < b {
		return b
	}
	return a
}

func max64(a, b int64) int64 {
	if a < b {
		return b
	}
	return a
}

func abs(a int) int {
	if a < 0 {
		return -a
	}
	return a
}

func abs64(a int64) int64 {
	if a < 0 {
		return -a
	}
	return a
}

func gcd(a, b int) int {
	if b == 0 {
		return a
	}
	return gcd(b, a%b)
}

func gcd64(a, b int64) int64 {
	if b == 0 {
		return a
	}
	return gcd64(b, a%b)
}

// readString() string
// readInt() int
// readInt64() int64
// readFloat64() float64

// -----------------------------------------------------------------------------

func main() {
	defer stdout.Flush()

	for {
		ans, ok := solve()
		if !ok {
			break
		}
		println(ans)
	}

}

func solve() (int, bool) {
	w := readInt()
	h := readInt()
	if w == 0 && h == 0 {
		return 0, false
	}

	c := make([][]int, h+2)
	c[0] = make([]int, w+2)
	c[h+1] = make([]int, w+2)
	for i := 1; i-1 < h; i++ {
		c[i] = make([]int, w+2)
		for j := 1; j-1 < w; j++ {
			c[i][j] = readInt()
		}
	}

	var ans int
	for i := 1; i-1 < h; i++ {
		for j := 1; j-1 < w; j++ {
			if 0 < c[i][j] {
				ans++
				dfs(c, i, j)
			}
		}
	}
	return ans, true
}

func dfs(c [][]int, y, x int) {
	c[y][x] = 0
	dx := []int{-1, 0, 1, -1, 1, -1, 0, 1}
	dy := []int{-1, -1, -1, 0, 0, 1, 1, 1}
	for i := range dx {
		nx := x + dx[i]
		ny := y + dy[i]
		if 0 < c[ny][nx] {
			dfs(c, ny, nx)
		}
	}
}

// -----------------------------------------------------------------------------

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

const int X[8] = {1, 1, 0, -1, -1, -1, 0, 1};
const int Y[8] = {0, 1, 1, 1, 0, -1, -1, -1};

int W, H;
vector<vector<int>> Field;

void dfs(int y, int x){
    Field[y][x] = 0;
    for(int i = 0; i < 8; i++){
        int NeX = x + X[i];
        int NeY = y + Y[i];
        if(NeX < 0 || W <= NeX || NeY < 0 || H <= NeY)continue;
        if(Field[NeY][NeX] == 0)continue;
        dfs(NeY, NeX);
    }
}

int main(){
    while(cin >> W >> H){
        if(W == 0 && H == 0)break;
        Field.assign(H, vector<int>(W, 0));
        for(int i = 0; i < H; i++){
            for(int j = 0; j < W; j++){
                cin >> Field[i][j];
            }
        }
        
        int ans = 0;
        for(int i = 0; i < H; i++){
            for(int j = 0; j < W; j++){
                if(Field[i][j] == 0)continue;
                dfs(i, j);
                ans++;
            }
        }
        
        cout << ans << endl;
    }
    return 0;
}

C++: #include <iostream>
#include <string>
#include <vector>
#include <algorithm>
#include <stdio.h>
#include <cmath>
#include <map>
#include <queue>
using namespace std;
#define rep(i, n) for (int i = 0; i < (int)(n); i++)

int W,H;
vector<vector<int>> C;
void dfs(int v, int w) {
  C[v][w] = 0;
  for(int dh = -1; dh <= 1; dh++) {
    for(int dw = -1; dw <= 1; dw++) {
      if(v+dh < 0 || v+dh >= H || w+dw < 0 || w+dw >= W) {continue;}
      if(C[v+dh][w+dw] == 0) {continue;}
      dfs(v+dh, w+dw);
    }
  }
}


int main() {

  while(cin >> W >> H) {
    if(W == 0 && H == 0) {break;}
    C.assign(H, vector<int>(W, 0));
    rep(i,H) {
      rep(j, W) {
        //int a;
        cin >> C[i][j];
      }
    }
    int count = 0;
    rep(i,H) {
      rep(j, W) {
        if(C[i][j] == 0) {continue;}
        dfs(i,j);
        count++;
      }
    }
    cout << count << endl;
  }
}

C++: #include "bits/stdc++.h"
using namespace std;
#define rep(i,a,n) for(ll i=a;i<n;i++)
#define ALL(s) s.begin(),s.end()
#define P pair<int,int>
#define print(n) cout<<n<<endl
const int M=100010;
const int MOD=1000000007;
const int INF=1000000007;
using ll=long long;


int w,h;
char c[55][55];
int ddx[8]={-1,-1,-1,0,0,1,1,1};
int ddy[8]={-1,0,1,-1,1,-1,0,1};
int dx[4]={-1,0,1,0},dy[4]={0,-1,0,1};
bool used[55][55]={};
void dfs(int W,int H){
	if(W<0||W>=w||H<0||H>=h||used[W][H])return;
	used[W][H]=true;
	if(c[W][H]=='1'){
		rep(i,0,8)dfs(W+ddx[i],H+ddy[i]);
	}
}
int main(){
	while(1){
		int land=0;
		cin>>w>>h;
		if(w==0)break;
		rep(j,0,h){
			rep(i,0,w){
				cin>>c[i][j];
				used[i][j]=false;
			}
		}
		rep(j,0,h){
			rep(i,0,w){
				if(used[i][j]==false&&c[i][j]=='1'){
					dfs(i,j);
					land++;
				}
			}
		}
		print(land);
	}
	return 0;
}

C++: #include<iostream>
#include<map>
#include<vector>
#include<algorithm>
#include<cmath>
#include<climits>
#include<ctime>
#include<cstring>

#define ALL(v) (v).begin(),(v).end()
#define REP(i,p,n) for(int i=p;i<(int)(n);++i)
#define rep(i,n) REP(i,0,n)
#define DUMP(list) cout << "{ "; for(auto nth : list){ cout << nth << " "; } cout << "}" << endl;

using namespace std;

int W,H;

bool sima[52][52];
bool checkmap[52][52];

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


int saiki(int x, int y) 
{
	if(sima[x][y]==1){sima[x][y]=0;}
	else if(sima[x][y]==0){return 0;}

	rep(i,9){
		saiki(x+dx[i], y+dy[i]);
	}

	return 1;
}

int main() {

	while(cin >> W >> H) {
		int count=0;
		if (W == 0 && H == 0) { break; }

		rep(h,H) {
			rep(w,W) {
				sima[w][h] = 0;
				checkmap[w][h] = 0;
			}
		}

		rep(h,H) {
			rep(w,W) {
				cin >> sima[w+1][h+1];
			}
		}

		REP(h,1,H+1){
			REP(w,1,W+1){
				if(sima[w][h]==1){
					saiki(w,h);
					//cout << w << ":" << h << endl; 
					count++;	
				}
			}
		}
/*
	cout << "map" << endl; 

		rep(h,51){
			rep(w,51){
				cout << sima[w][h] << " ";
			}
			cout << endl;
		}


	
	cout << "checkmap" << endl; 

		rep(h,51){
			rep(w,51){
				cout << checkmap[w][h] << " ";
			}
			cout << endl;
		}
*/

		cout << count << endl;
	}

	return 0;
}