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;

typedef long long ll;
typedef pair<int,int> P;
typedef vector<int> VI;
typedef vector<VI> VVI;
const double PI = 3.14159265358979323846;
const double EPS = 1e-12;
const int INF = numeric_limits<int>::max() / 2;
const int NEG_INF = numeric_limits<int>::min() / 2;
const int MOD=1e9+7;

int c[100][100];//0　海
int h,w;

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

void bfs(int i,int j){
    queue<P> q;
    q.push(P(i,j));
    while(!q.empty()){
        int x=q.front().first,y=q.front().second;
        q.pop();
        c[x][y]=0;
        for(int i=0;i<8;i++){
            int nx=x+dx[i],ny=y+dy[i];
            if(0<=nx&&nx<h&&0<=ny&&ny<w&&c[nx][ny]==1){
                q.push(P(nx,ny));
            }
        }
    }
}

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

  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>>c[i][j];
      }
      int res=0;
      for(int i=0;i<h;i++)for(int j=0;j<w;j++){
          if(c[i][j]==1){
              bfs(i,j);
              res++;
          }
      }
      cout<<res<<endl;
  }
}

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

int w, h;
int c[50][50];

void dfs(int x, int y){
    c[x][y] = 0;
    for(int dx=-1; dx<=1; dx++){
        for(int dy=-1; dy<=1; dy++){
            int nx = x + dx;
            int ny = y + dy;
            if(0<=nx && nx<h && 0<=ny && ny<w && c[nx][ny]==1) dfs(nx, ny);
        }
    }
    return;
}

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

C++: #include <iostream>
#include <string>
#include <algorithm>
#include <cstdio>
#include <vector>

using namespace std;
typedef long long ll;

int ans = 0;
vector <vector<int> > MAP;

int solve(int locateX,int locateY,int w,int h){
  //!
  if(locateX<0 || locateY<0 || locateX>=h || locateY>=w)return 0;
  //CHECK
  if(MAP[locateX][locateY]==1){
    MAP[locateX][locateY]=0;
    //!--
    solve(locateX,locateY+1,w,h);
    solve(locateX+1,locateY+1,w,h);
    solve(locateX+1,locateY,w,h);
    solve(locateX+1,locateY-1,w,h);
    solve(locateX,locateY-1,w,h);
    solve(locateX-1,locateY-1,w,h);
    solve(locateX-1,locateY,w,h);
    solve(locateX-1,locateY+1,w,h);
    //--!
    return 1;
  }
  return 0;
}

int ope(){
  int w,h;
  ans=0;
  MAP.resize(0);
  cin >> w >> h;
  if(!w&&!h)return 0;
  //INPUT
  for(int i=0;i<h;++i){
    vector<int> temp;
    for(int j=0;j<w;++j){
      int ti;
      cin >> ti;
      temp.push_back(ti);
    }
    MAP.push_back(temp);
  }
  //EXEC
  for(int i=0;i<h;++i)
    for(int j=0;j<w;++j)  
      ans+=solve(i,j,w,h);

  //OUTPUT
  cout << ans << endl;
  return 1;
}
int main(){
  while(ope()){}
}

C++: #include <bits/stdc++.h>
#define REP(i, n) for(int i = 0; (i) < (n); (i)++)
using namespace std;

struct Edge{
    int to;
    int weight;
    Edge(int t, int w) : to(t), weight(w) {}
};

long modpow(long a, long n, long mod) {
    long res = 1;
    while (n > 0) {
        if (n & 1) res = res * a % mod;
        a = a * a % mod;
        n >>= 1;
    }
    return res;
}

// a^{-1} mod を計算する
long modinv(long a, long mod) {
    return modpow(a, mod - 2, mod);
}

using Graph = vector<vector<int>>;
//using Graph = vector<vector<Edge>>;
bool seen[100][100];
int field[100][100];

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

int w, h;

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

int main()
{
    for(;;){
        cin >> w >> h;
        if(w==0 && h==0)
            break;
        REP(i, h)
            REP(j, w){
                cin >> field[i][j];
                seen[i][j] = false;
            }
        int count = 0;
        REP(i, h)
            REP(j, w){
                if(seen[i][j] || field[i][j] == 0) continue;
                dfs(i, j);
                count++;
            }
        cout << count << endl;
    }

    return 0;
    
}