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>
#define pq priority_queue
using namespace std; typedef pair<int, int> P; typedef pair<int, P> P2;
typedef pair<int, P2> P3; typedef long long ll; typedef long double ld;
constexpr long long gcd(long long a, long long b){return b ? gcd(b, a % b) : a;}
constexpr long long lcm(long long a, long long b){return a / gcd(a, b) * b;}
constexpr int INF = 1e9, MOD = INF + 7, around[] = {0, 1, 1, -1, -1, 0, -1, 1, 0, 0};
constexpr int mod_pow(long long x, long long n, const int mod){long long ret=1;while(n){if(n&1)(ret*=x)%=mod;(x*=x)%=mod;n>>=1;}return ret;}
template<int n> struct Prime{bool arr[n+1];constexpr bool operator[](int k){return arr[k];}constexpr Prime():arr(){for(int i=2;i<n;i++){arr[i]=true;for(int j=2;j*j<=i;j++){if(!(i%j))arr[i]=false;}}}};
template<int n> struct Factorial{long long arr[n+1],ary[n+1];constexpr Factorial():arr(),ary(){arr[0]=1;ary[0]=1;for(int i=0;i<n;i++){arr[i+1]=arr[i]*(i+1)%MOD;ary[i+1]=mod_pow(arr[i+1],MOD-2,MOD);}}};
constexpr Factorial<10> fact; constexpr Prime<10> prime;
constexpr int comb(int a, int b){long long pos = fact.arr[a], pot = fact.ary[a - b], por = fact.ary[b];return pos * pot % MOD * por % MOD;}
template<int n> struct Bernoulli{long long arr[n+1];constexpr Bernoulli():arr(){arr[0]=1;for(int i=1;i<=n;i++){long long sum=0;for(int j=0;j<i;j++){(sum+=comb(i+1,j)*arr[j]%MOD)%=MOD;}arr[i]=(MOD-mod_pow(i+1,MOD-2,MOD))%MOD*sum%MOD;}}};
constexpr int vx[] = {1, 0, -1, 0}, vy[] = {0, 1, 0, -1};
constexpr int sqrtN = 512, logN = 32;
constexpr ld PI = abs(acos(-1));
constexpr ll LINF=1e18;

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

int main(){
	while(cin >> w >> h,w){
		for(int i = 0; i < h; i++){
			for(int j = 0; j < w; j++){
				cin >> a[i][j];
			}
		}
		
		int c = 0;
		for(int i = 0; i < h; i++){
			for(int j = 0; j < w; j++){
				if(a[i][j] != 1) continue; c++;
				
				queue<P> qu; qu.push({i, j}); a[i][j] = 2;
				while(!qu.empty()){
					auto p = qu.front(); qu.pop();
					
					for(int k = 0; k < 8;){
						int nx = p.first + around[k], ny = p.second + around[++k];
						if(nx<0||ny<0||nx>=h||ny>=w||a[nx][ny] != 1) continue;
						a[nx][ny] = 2;
						qu.push({nx, ny});
					}
				}
			}
		}
		
		cout << c << endl;
	}

	return 0;
}

C++: #include <bits/stdc++.h>
#define rep(i,n) for(int i=0;i<n;i++)
using namespace std;
typedef long long ll;
typedef pair<int,int> pp;
const int INF = 1e9;
const int MOD = 1000000007;
int w = 51,h = 51;
vector<vector<int>> maze(h,vector<int>(w,0));
vector<int> dx = {0,0,1,-1,1,1,-1,-1};
vector<int> dy = {1,-1,0,0,1,-1,1,-1};

void dfs(int y,int x){
        maze.at(y).at(x) = 2;
        rep(i,8){
                int new_y,new_x;
                new_y = y + dy[i];
                new_x = x + dx[i];
                if(0 <=new_y && new_y < h && 0<= new_x && new_x < w){
                        if(maze.at(new_y).at(new_x) == 1){
                                dfs(new_y,new_x);
                        }
                }
        }
}

int main(){
        while(1){
                cin >> w >> h;
                if(w == 0 && h == 0) break;
            rep(i,h)rep(j,w) cin >> maze.at(i).at(j);
        
            int cnt = 0;
            rep(i,h)rep(j,w){
                        if(maze.at(i).at(j) == 1){
                                dfs(i,j);
                        cnt ++;
                }
            }
        
                cout << cnt << endl;
        }
        return 0;
}

C++: #include<iostream>

int field[52][52];

void clear();

bool walk(int x, int y);

int main(){

	int w,h,ans;
	
	while(true){

		std::cin >> w >> h;
		if(w==0 && h==0)
			break;

		clear();
		for(int i=1; i<=h; i++)
			for(int j=1; j<=w; j++)
				std::cin >> field[i][j];

		ans = 0;
		for(int i=1; i<=h; i++)
			for(int j=1; j<=w; j++)
				if(walk(j, i))
					ans++;

		std::cout << ans << std::endl;

	}

	return 0;
}

void clear(){
	for(int i=0; i<52; i++)
		for(int j=0; j<52; j++)
			field[i][j] = 0;
}

bool walk(int x, int y){
	if(field[y][x] == 0)
		return false;

	field[y][x] = 0;
	
	for(int i=-1; i<=1; i++)
		for(int j=-1; j<=1; j++)
			walk(x+i, y+j);

	return true;
}

C++: #include<bits/stdc++.h>
#include<vector>
using namespace std;
vector< vector<int> > c(50, vector<int> (50));
void dfs(int x, int y, int w, int h){
  c[y][x] = 0;

  for(int i = -1 ; i <= 1 ; i++){
    for(int j = -1 ; j <= 1 ; j++){
      int nx = x + i, ny = y + j;

      if(0 <= nx && nx < w && 0 <= ny && ny < h && c[ny][nx] == 1)
        dfs(nx, ny, w, h);
    }
  }
}

int main(void){

while(true){
  int w, h;   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 ans = 0;
  for(int i = 0 ; i < h ; i++){
    for(int j = 0 ; j < w ; j++){
      if(c[i][j] == 1){
        dfs(j, i, w, h);
        ans++;
      }
    }
  }

  cout << ans << endl;
}

  return 0;
}