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;

int H,W;

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

bool seen[51][51];
int board[51][51];

  void dfs(int h, int w) {
  seen[h][w] = true; 
  
  for (int dir = 0; dir < 8; ++dir) {
    int nw = w + dx[dir];
    int nh = h + dy[dir];  
    if (nw < 0 || nw >= W || nh < 0 || nh >= H) continue;
    if (seen[nh][nw]) continue; 
    if (board[nh][nw] == 1) dfs(nh,nw);
  }
} 

int main() {  
  cin >> W >> H;
  while (W >= 1 || H >= 1) {
    int ans =0;
    for (int i =0; i<H; i++) {
      for (int j =0; j<W; j++) {
        int tmp; cin >> tmp;
        board[i][j] = tmp;
      }
    }
    
    for (int i =0; i<H; i++) {
      for (int j =0; j<W; j++) {
        seen[i][j] = false;
      }
    }
  
    for (int i =0; i<H; i++) {
      for (int j =0; j<W; j++) {
        if (seen[i][j] == true || board[i][j] == 0) continue;
        dfs(i,j);
        ans++;
      }
    } 
    cout << ans << endl; 
    cin >> W >> H;
    for (int i =0; i<H; i++) {
      for (int j =0; j<W; j++) {
        board[i][j] = 0;
      }
    }
  }
}

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

int W, H;

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

void solve(int cx, int cy, vector<vector<int>>& v) {
    v[cx][cy] = 0;
    for(int i=0; i<8; ++i) {
        int nx = cx + dx[i];
        int ny = cy + dy[i];
        if(nx >= 0 && nx < W && ny >= 0 && ny < H && v[nx][ny] == 1) {
            solve(nx, ny, v);
        }
    }
}


int main() {
    while(cin >> W >> H, W) {
        vector<vector<int>> v(W, vector<int>(H));
        for(int i=0; i<H; ++i) {
            for(int j=0; j<W; ++j) {
                cin >> v[j][i]; // 0: sea, 1:island
            }
        }
        int ans=0;
        for(int i=0; i<W; ++i) {
            for(int j=0; j<H; ++j) {
                if(v[i][j] == 1) {
                    solve(i, j, v);
                    ++ans;
                }
            }
        }
        cout << ans << endl;
    }
}

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

//#define int long long
//struct Fast {Fast(){cin.tie(0);ios::sync_with_stdio(0);}} fast;
//const int MOD = 1e9 + 7;
using intpair = pair<int, int>;

#define rep(i, n) for (int i = 0; i < (int)(n); i++)
#define reps(i, n, m) for (int i = (int)(n); i <= (int)(m); i++)
#define ALL(obj) (obj).begin(), (obj).end()
#define rALL(obj) (obj).rbegin(), (obj).rend()
#define ceiv(a, b) ((int)((a) + (b) - 1)/(int)(b))

int H, W;
bool is_safe(int x, int y){
    return (0 <= x && x < W)&&(0 <= y && y < H);
}

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

void solve(){
    bool field[H][W], visited[H][W];
    rep(i,H)rep(j,W) visited[i][j] = false;
    rep(i,H)rep(j,W) cin >> field[i][j];
    
    int ans = 0;
    
    rep(y,H)rep(x,W){
        if(visited[y][x] || !field[y][x]) continue;
        stack<intpair> s;
        s.push({x,y});
        ans++;
        while(!s.empty()){
            int x2 = s.top().first, y2 = s.top().second;
            visited[y2][x2] = true;
            s.pop();
            rep(i,8){
                if(!is_safe(x2+dx[i], y2+dy[i])) continue;
                if(!field[y2+dy[i]][x2+dx[i]])   continue;
                if(visited[y2+dy[i]][x2+dx[i]])  continue;
                s.push({x2+dx[i],y2+dy[i]});
            }
        }
    }
    cout << ans << endl;
}

signed main(){
    while(true){
        cin >> W >> H;
        if(H==0 && W==0) break;
        solve();
    }
}

C++: #include <bits/stdc++.h>
#define rep(i,a,b) for(int i=int(a);i<int(b);++i)
#define SIZE 200005
#define INF 1000000005LL
#define MOD 1000000007

using namespace std;
typedef long long int ll;
typedef pair <int,int> P;

int w,h;
int c[55][55];
int d[55][55];
int a;

void dfs(int i,int j);
void dfs_(int i,int j){
  if(i>=0 && j>=0) if(c[i][j]==1) dfs(i,j);
}

void dfs(int i,int j){
  if(d[i][j]==1)return;
  d[i][j] = 1;
  rep(l,0,3) rep(r,0,3) dfs_(i-1+l,j-1+r);
}


int imain(){
  a=0;
  rep(i,0,52) rep(j,0,52) c[i][j]=d[i][j]=0;
  rep(i,0,h) rep(j,0,w) {
    cin >> c[i][j];
  }

  rep(i,0,h) rep(j,0,w) if(c[i][j]==1 && d[i][j]==0) {
    dfs(i,j);
    a++;
  }
  cout << a << endl;

  return 0;
}

int main(){
  for(;;){
    cin >> w >> h;
    if(w==0 && h==0)break;
    imain();
  }
  return 0;
}