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 <vector>
#include <list>
#include <map>
#include <set>
#include <deque>
#include <stack>
#include <bitset>
#include <algorithm>
#include <functional>
#include <numeric>
#include <utility>
#include <sstream>
#include <iostream>
#include <iomanip>
#include <cstdio>
#include <cmath>
#include <cstdlib>
#include <cctype>
#include <string>
#include <cstring>
#include <ctime>
#include <fstream>
#include <queue>

#pragma warning( disable: 4996 )

using namespace std;

typedef long long ll;

#define INF 100000000
#define EPS 1e-9

#define MAX_W 50
#define MAX_H 50

int w, h;
int maze[MAX_H][MAX_W];

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

vector<int> ans_list;

void solve(int x, int y){

	maze[y][x] = 0;

	for (int i = 0; i < 8; i++){
		int nx = x + dx[i];
		int ny = y + dy[i];
		if (nx >= 0 && nx < w && ny >= 0 && ny < h && maze[ny][nx] == 1)
			solve(nx, ny);
	}

}

int main(){

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

		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){
					solve(j, i);
					ans++;
				}
			}
		}
	
		ans_list.push_back(ans);

	}

	for (int i = 0; i < ans_list.size(); i++){
		cout << ans_list[i] << endl;
	}

	return 0;

}

C++: #include <bits/stdc++.h>
#if 1
#define int long long
#define MAX LLONG_MAX
#define MIN LLONG_MIN
#define stoi stoll
#else
#define MAX INT_MAX
#define MIN INT_MIN
#endif
#define ALL(obj) (obj).begin(), (obj).end()
#define fi first
#define se second
const int NIL = -1;
const int MOD = 1000000007;
#define REP(i,n) for (int i=0; i < (n); ++i)
#define FOR(i,a,b) for (int i=(a); i < (b); ++i)
using namespace std;

int get_gcd(int a, int b){while(1){if (a < b) swap(a, b); if (b == 0) return a; a %= b;}}
int get_lcm(int a, int b){return a * b / get_gcd(a, b);}
//int dx[4]={1,0,-1,0};
//int dy[4]={0,1,0,-1};

//-------------------------テンプレここまで-------------------------//

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

int w, h;
// 0なら海、1なら陸（未探索)
bool field[50][50];
// 探索済かどうか
bool seen[50][50];

void dfs(int i, int j){
  seen[i][j] = true;

  REP(k,8) {
    int ni = i+dx[k];
    int nj = j+dy[k];
    if (ni<0 || ni>=h || nj<0 || nj>=w) continue;
    if (!field[ni][nj] || seen[ni][nj]) continue;
    dfs(ni, nj);
  }
}

signed main () {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);

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

    REP(i,h) {
      REP(j,w) {
        cin >> field[i][j];
        seen[i][j] = false;
      }
    }

    int ans = 0;
    REP(i,h) {
      REP(j,w) {
        if (!field[i][j] || seen[i][j]) continue;
        dfs(i,j);
        ans++;
        //cout << i << " " << j << endl;
      }
    }

    cout << ans << endl;
  }

  return 0;
}

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

#define rep(i,n) for (int i = 0; i < (n); ++i)

int w, h, c[55][55], ans;
bool is_search[55][55];
int vx[8] = {0, -1, -1, -1, 0, 1, 1, 1},
    vy[8] = {-1, -1, 0, 1, 1, 1, 0, -1};

struct land_or_sea {
    int x, y, num;
    land_or_sea(int x, int y, int num) : x(x), y(y), num(num) {}
};

void bfs(int x, int y) {
    queue<pair<int, int> > que;
    que.push(make_pair(x, y));
    is_search[x][y] = true;

    while (!que.empty()) {
        pair<int, int> p = que.front(); que.pop();
        rep(i,8) {
            int nx, ny;
            nx = p.first + vx[i], ny = p.second + vy[i];
            if (0 <= nx && nx < w && 0 <= ny && ny < h &&
                c[nx][ny] == 1 && is_search[nx][ny] == false) {
                   que.push(make_pair(nx, ny));
                   is_search[nx][ny] = true;
            }
        }
    }

}

int main(void) {
    cin.tie(0);
    ios::sync_with_stdio(false);

    while (cin >> w >> h, w | h) {
        memset(c, 0, sizeof(c));
        memset(is_search, false, sizeof(is_search));
        ans = 0;
        rep(i,h) {
            rep(j,w) {
                cin >> c[i][j];
            }
        }

        rep(i,w) {
            rep(j,h) {
                if (is_search[i][j] == false && c[i][j] == 1) {
                    bfs(i,j);
                    ans++;
                }
            }
        }

        cout << ans << endl;
    }

    return 0;
}