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>
#include <iostream>
#include <fstream>
#include <vector>
#include <string>
#include <cmath>
#include <algorithm>
#include <map>
#include <iomanip>
#include <stdlib.h>
#include <stdio.h>
#include <queue>
#include <deque>
#include <set>
#include <stack>
#include <time.h>
 
using namespace std;
 
typedef long long ll;
typedef long double ld;
typedef pair<int, int> Pii;
typedef pair<int, ll> Pil;
typedef pair<ll, ll> Pll;
typedef pair<ll, int> Pli;
 
const ll MOD = 1e9 + 7;
const ll MOD2 = 998244353;
const ll INF = 1ll << 60;
const double PI = 2 * asin(1);

void yes() {printf("yes\n");}
void no() {printf("no\n");}
void Yes() {printf("Yes\n");}
void No() {printf("No\n");}
void YES() {printf("YES\n");}
void NO() {printf("NO\n");}

int W, H, C[100][100];
const int di[3] = {-1, 0, 1}, dj[3] = {-1, 0, 1};
bool visited[100][100];

int Solve(){
  fill((bool*)visited, (bool*)(visited + H), false);
  for (int i = 0; i < H; i++){
    for (int j = 0; j < W; j++){
      cin >> C[i][j];
    }
  }
  
  int nowi = 0, ans = 0;
  queue <Pii> que;
  for (int i = 0; i < H; i++){
    for (int j = 0; j < W; j++){
      if (C[i][j] == 1){
        que.push(make_pair(i, j));
        nowi = i; visited[i][j] = true;
        ans++;
        break;
      }
    }
    if (!que.empty()) break;
  }
  
  while (!que.empty()){
    Pii Q = que.front(); que.pop();
    int I = Q.first, J = Q.second;

    for (int i = 0; i < 3; i++){
      for (int j = 0; j < 3; j++){
        if (0 <= I + di[i] && I + di[i] < H && 0 <= J + dj[j] && J + dj[j] < W){
          if (C[I + di[i]][J + dj[j]] == 1 && visited[I + di[i]][J + dj[j]] == false){
            que.push(make_pair(I + di[i], J + dj[j]));
            visited[I + di[i]][J + dj[j]] = true;
          }
        }
      }
    }

    if (que.empty()){
      for (int i = nowi; i < H; i++){
        for (int j = 0; j < W; j++){
          if (C[i][j] == 1 && visited[i][j] == false){
            que.push(make_pair(i, j)); ans++; nowi = i;
            visited[i][j] = true; break;
          }
        }
        if (!que.empty()) break;
      }
    }
  }
  cout << ans << endl;
  
  return 0;
}

int main(){
  while (cin >> W >> H){
    if (W == 0 && H == 0) return 0;
    Solve();
  }
  
  return 0;
}

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

using namespace std;

void fill(vector< vector<int> >& table, int i, int j, int w, int h, int e){
    if(i < 0 || j < 0) return;
    if(i >= h || j >= w) return;
    for(int k=-1; k <= 1; ++k){
        for(int l=-1; l <= 1; ++l){
            if(i+k < 0 || j+l < 0) continue;
            if(i+k >= h || j+l >= w) continue;
            if(table[i+k][j+l] == 1) {
                table[i+k][j+l] = e;
                fill(table, i+k, j+l, w, h, e);
            }
        }
    }
    return;
}

int main(){
    while(true){
        int w, h;
        int cnt = 1;
        cin >> w;
        cin >> h;
        if(w == 0 && h == 0) break;
        vector< vector<int> > table(h, vector <int>(w));
        for(int i=0; i<h; ++i){
            for(int j=0; j<w; ++j){
                cin >> table[i][j];
            }
        }
        for(int i=0; i<h; ++i){
            for(int j=0; j<w; ++j){
                int e = table[i][j];
                if(e == 0) continue;
                else if(e == 1) {
                    cnt++; e = cnt; table[i][j] = e;
                    fill(table, i, j, w, h, e);
                }
            }
        }
        /*
        for(int i=0; i<h; ++i){
            for(int j=0; j<w; ++j){
                cout << table[i][j] << " ";
            }
            cout << endl;
        }*/
        cout << (cnt-1) << endl;
    }
}

C++: #include<stdio.h>
#include<iostream>

int field[51][51];

void land(int x,int y){
	field[x][y]=0;

	for(int xx=-1;xx<=1;xx++){
		for(int yy=-1;yy<=1;yy++){

			if(field[x+xx][y+yy]==1){
				land(x+xx,y+yy);
			}

		}
	}
	return;
}

int main(void){

	int w,h,kazu=0;

	scanf("%d %d",&w,&h);

	for(int i=0;i<h;i++){
		for(int j=0;j<w;j++){
			scanf("%d",&field[i][j]);
		}
	}

	for(int i=0;i<h;i++){
		for(int j=0;j<w;j++){

			if(field[i][j]==1){
				land(i,j);
				kazu++;
			}

		}
	}

	printf("%d",kazu);

}