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>

using namespace std;

int w, h;
int c[50][50];
int ax[8] = {-1,-1,-1, 0, 0, 1, 1, 1};
int ay[8] = {-1, 0, 1,-1, 1,-1, 0, 1};
int cnt=0;

void solve(int y, int x){
	if(y<0 || h<=y || x<0 || w<=x) return;
	if(c[y][x] == 1){
		c[y][x] = 0;
		for(int i=0; i<8; i++){
			for(int j=0; j<8; j++){
				solve(y+ay[i], x+ax[j]);
			}
		}
	}
}

int main(){
	while(1){
		cin >> w >> h;
		if(!w && !h) break;
		for(int i=0; i<h; i++){
			for(int j=0; j<w; j++){
				cin >> c[i][j];
			}
		}

		cnt = 0;
		for(int i=0; i<h; i++){
			for(int j=0; j<w; j++){
				if(c[i][j] == 1){
					solve(i, j);
					cnt++;
				}
			}
		}
		cout << cnt << endl;
	}
	return 0;
}

C++: #include <bits/stdc++.h>
using namespace std;
#define rep(i, n) for(int i = 0, i##_len = (n); i < i##_len; ++i)
#define rep2(i, x, n) for(int i = x, i##_len = (n); i < i##_len; ++i)
#define all(n) begin(n), end(n)
using ll = long long;
using P = pair<int, int>;
using vi = vector<int>;
using vl = vector<ll>;
using vs = vector<string>;
using vc = vector<char>;
using vb = vector<bool>;
using vd = vector<double>;
vi dir = {-1, 0, 1, 0, -1, -1, 1, 1, -1};

void solve(int h, int w) {
  vector<vi> g(h + 2, vi(w + 2, -1));
  rep2(i, 1, h + 1) rep2(j, 1, w + 1) {
    int c;
    cin >> c;
    if(c == 1) g[i][j] = 0;
  }
  int ans = 0;
  rep2(a, 1, h + 1) rep2(b, 1, w + 1) {
    if(g[a][b] != 0) continue;
    ans++;
    g[a][b] = ans;
    stack<pair<int, int>> st;
    st.push(P(a, b));
    while(!st.empty()) {
      int i = st.top().first;
      int j = st.top().second;
      st.pop();
      rep(k, 8) {
        int ni = i + dir[k];
        int nj = j + dir[k + 1];
        if(g[ni][nj] != 0) continue;
        g[ni][nj] = g[i][j];
        st.push(P(ni, nj));
      }
    }
  }
  cout << ans << endl;
}

int main() {
  while(true) {
    int h, w;
    cin >> w >> h;
    if(h == 0 && w == 0) break;
    solve(h, w);
  }
}

C++: #include <iostream>
#include <queue>
using namespace std;

int map[20][20];

typedef pair<int, int> pxy;

int h, w, ans = 0;

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

void bfs(int sx, int sy)
{
  queue<pxy> q;
  q.push(pxy(sx, sy)); 
  while(!q.empty()){
    int x, y;
    x = q.front().first;
    y = q.front().second;
    q.pop();
    if(map[x][y]){
      map[x][y] = 0;
      for(int i = 0; i < 8; i++){
	int next_x = x + dx[i], next_y = y + dy[i];
	if(map[next_x][next_y]){
	  q.push(pxy(next_x, next_y));
	}
      }
    }
  }
}

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

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

#define rep(i,n) for(int i=0;i<(n);i++)
#define mp(a,b) make_pair(a,b)
#define pb(a) push_back(a)
#define dbg(x) cout<<#x"="<<x<<endl

typedef long long ll;
typedef pair<int, int> pii;
typedef pair<pii, int> ppi;

#define INF 1000000000
#define SIZE 51

bool f[SIZE][SIZE]; //true=land (1)
int w,h;

void wfs(int i, int j){
  //width-first search
  int d[]={1,0,-1};
  rep(di,3){
    int ni=i+d[di];
    if(ni<0 || ni>=h) continue;
    rep(dj,3){
      int nj=j+d[dj];
      if(nj<0 || nj>=w) continue;
      if(f[ni][nj]){
	f[ni][nj]=false;
	wfs(ni,nj);
      }
    }
  }
}

int main(){
  while(true){
    cin >>w>>h;
    if(w==0&&h==0) break;
    
    rep(i,h){
      rep(j,w){
	int d;
	scanf("%d", &d);
	if(d==0) f[i][j]=false;
	else  f[i][j]=true; //1=land
      }
    }

    int ans=0;
    rep(i,h){
      rep(j,w){
	if(f[i][j]){
	  ans++;
	  wfs(i,j);
	}
      }
    }

    cout << ans << endl;

  }
}

C++: #include<iostream>
#include<queue>

using namespace std;

int w,h;
int map[100][100];

void delIsland(int y,int x){

  queue<pair<int,int> > que;
  que.push(make_pair(y,x));


  int next[8][2] = {{0,1},{0,-1},{1,0},{-1,0},{1,1},{1,-1},{-1,1},{-1,-1}};
  while(!que.empty()){
    pair<int,int> now  = que.front();
    que.pop();

    if(now.first<0 || now.first>=h || now.second<0 || now.second>=w || map[now.first][now.second]!=1){
      continue;
    }else{
      map[now.first][now.second] = 0;
    }


    for(int i=0;i<8;i++){
      int ny = now.first + next[i][0];
      int nx = now.second + next[i][1];
      que.push(make_pair(ny,nx));
    }
  }
}

int main(){
  cin>>w>>h;
  while(w!=0 && h!=0){
    for(int i=0;i<h;i++){
      for(int j=0;j<w;j++){
       cin>>map[i][j];
     }
   }

   int ans = 0;
   for(int i=0;i<h;i++){
    for(int j=0;j<w;j++){
     if(map[i][j] == 1){
       ans++;
       delIsland(i,j);
     }
   }
 }

 cout<<ans<<endl;
 cin>>w>>h;
}

return 0;
}