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 <iostream>
#include <vector>
using namespace std;

int w, h, ans, tmp;
vector<vector<int>> c(60, vector<int>(60,0));

void ume(int x, int y){
	if(c[x][y]==0) return;
	c[x][y]=0;
	ume(x-1,y);
	ume(x+1,y);
	ume(x-1,y-1);
	ume(x,y-1);
	ume(x+1,y-1);
	ume(x-1,y+1);
	ume(x,y+1);
	ume(x+1,y+1);
	return;
}

bool hantei(int x, int y){
	if(c[x][y]==1){
		ume(x,y);
		return 1;
	}
	else return 0;
}

int main(){
	while(cin >> w >> h){
		if(w == 0&&h==0) break;
		ans = 0;
		for(int i = 1;i<=h;i++){
			for(int j = 1;j<=w;j++){
				cin >> c[i][j];
			}
		}
		for(int i = 1;i<=h;i++){
			for(int j = 1;j<=w;j++){
				ans+=hantei(i,j);
			}
		}
		cout << ans << endl;
	}
	return 0;
}

C++: #include<stdio.h>
#include<string>
#include<iostream>
using namespace std;
int a[55][55]={};
int w,h;
int count=0;
void dfs(int y,int x){
  a[y][x]=0;
  int dy[8]={-1,-1,-1,0,0,1,1,1};
  int dx[8]={-1,0,1,-1,1,-1,0,1};
  for(int i=1;i<8;i++){
    int ny=y+dy[i];
    int nx=x+dx[i];
    if(nx<0||ny<0||nx>=w||ny>=h)continue;
    if(a[ny][nx]==0)continue;
    dfs(ny,nx);
  }
}
int main(){
  while(1){
    int ans=0;
    cin>>w>>h;
    if(w==0&&h==0)break;
    for(int i=0;i<h;i++){
      for(int j=0;j<w;j++){
	cin>>a[i][j];
      }
    }
    for(int i=0;i<h;i++){
      for(int j=0;j<w;j++){
	if(a[i][j]==0)continue;
	ans++;
	dfs(i,j);
      }
    }
    cout<<ans<<endl;
  }
}

C++: # include <iostream>
# include <algorithm>
# include <vector>
# include <string>
# include <set>
# include <map>
# include <cmath>
# include <iomanip>
# include <functional>
# include <utility>
# include <stack>
# include <queue>
# include <list>
using namespace std;
using LL = long long;
constexpr int MOD = 1000000000 + 7;
//?¶????????????????????????????????????§??????????????????????¢????????????????
constexpr int INF = 2000000000;
const double PI = acos(-1);

int maze[51][51];
int w, h;

void dfs(int hpos, int wpos) {
	maze[hpos][wpos] = 0;
	for (int x = -1; x <= 1; x++) {
		for (int y = -1; y <= 1; y++) {
			int nx = wpos + x;
			int ny = hpos + y;
			if (0 <= nx&&nx < w && 0 <= ny&&ny < h&&maze[ny][nx] == 1)dfs(ny, nx);
		}
	}
}

int main() {
	while (cin >> w >> h&&w&&h) {
		for (int i = 0; i < h; i++) {
			for (int j = 0; j < w; j++) {
				cin >> maze[i][j];
			}
		}
		int ans = 0;
		for (int i = 0; i < h; i++) {
			for (int j = 0; j < w; j++) {
				if (maze[i][j] == 1) {
					ans++;
					dfs(i, j);
				}
			}
		}
		cout << ans << endl;
	}

}

C++: #include <iostream>
using namespace std;
int w,h;
int in[100][100];
bool seen[100][100];
int ans =0;
bool flag = false;

void solve(int i,int j){
    //cout << i << " " << j << endl;
    if(i < 0 || j < 0 || i >= h || j >= w){
        return;
    }
    if(seen[i][j]){
        return;
    }
    if(in[i][j] == 0){
        return;
    }
    seen[i][j] = true;
    solve(i-1,j-1);
    solve(i-1,j);
    solve(i-1,j+1);
    solve(i,j-1);
    solve(i,j+1);
    solve(i+1,j-1);
    solve(i+1,j);
    solve(i+1,j+1);

}
int main(){
    cin >> w >> h;
    for(int i=0;i<h;i++){
        for(int j=0;j<w;j++){
            cin >> in[i][j];
        }
    }
    for(int i=0;i<h;i++){
        for(int j=0;j<w;j++){
            if(seen[i][j] == true || in[i][j] == 0){
            }
            else{
                solve(i,j);
                ans++;
            }
            
        }
    }
    if(w != 0 && h != 0){
        cout << ans << endl;
    }
    return 0;
}

C++: #include <iostream>
#define MAX 70
#define rep(i,p) for(int i=0;i<p;i++)
#define pb push_back
using namespace std;
int w,h,ans,yoko,tate;
int hoge;
bool c[MAX][MAX];
bool visit[MAX][MAX];
// ?§???? {?????????????????????????????????????????????????????????}
const int dx[] = {1,0,-1,0,1,1,-1,-1};
const int dy[] = {0,-1,0,1,1,-1,1,-1};

bool dfs (int x, int y) {
  if(!c[x][y]) return false;
  visit[x][y] = true;
  rep(i,8) {
    yoko = x+dx[i];
    tate = y+dy[i];
    if ( c[yoko][tate] and !visit[yoko][tate] ) dfs(yoko,tate);
  }
  return true;
}
int main(){
  while (1) {
    cin >> w >> h;
    if (w == 0 and h == 0) break;
    rep(i,MAX) rep(j,MAX) c[i][j] = false;
    rep(i,MAX) rep(j,MAX) visit[i][j] = false;
    ans = 0;
    // ?????? 0 false
    // ?????? 1 true
    // ??§?¨? [1,w]x[1,h]????????´??????(false)
    for(int i=1;i<=h;i++)
      for(int j=1;j<=w;j++) {
        cin >> hoge;
        if (hoge == 1) c[i][j] = true;
      }
    for(int i=1;i<=h;i++)
      for(int j=1;j<=w;j++)
        if (!visit[i][j])
          // dfs ????????¢?´¢???????????\???????????¨??????false?????????
          if (dfs(i,j)) ans++;
    cout << ans << endl;
  }
  return 0;
}