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<cstdio>
#include<stack>

using namespace std;

struct Coord {
  int row;
  int col;
};

int solve (int W, int H);
  
int main () {
  int W, H;

  while(1)
    {
      scanf("%d%d", &W, &H);
      if(W==0 && H==0) break;	
      printf("%d\n", solve(W,H));	
    }
}

int solve (int W, int H) {
  int n=0;
  char M[H+2][W+2];
  Coord start;
  for (int i=0; i<=W+1; i++) { M[0][i] = '0'; M[H+1][i] = '0'; }
  for (int i=1; i<=H; i++) { M[i][0] = '0'; M[i][W+1] = '0'; } 
  for (int i=1; i<=H; i++) {
    for (int j=1; j<=W; j++) {
      scanf(" %c", &M[i][j]);
    }
  }
  for(int i=1 ; i<=H ; i++) {
    for(int j=1 ; j<=W ; j++){
      if(M[i][j]=='1'){	
        start.row=i;
        start.col=j;
        M[start.row][start.col]='@';

        stack<Coord> S;
        S.push(start);
        while (!S.empty()) {
          Coord current = S.top(); S.pop();
          int D1[]={1,1,0,-1,-1,-1,0,1}; int D2[]={0,-1,-1,-1,0,1,1,1};
          for (int d=0; d<8; d++) {
            Coord nc = current;
            nc.row += D1[d];
            nc.col += D2[d];
            if(M[nc.row][nc.col]!='0' && M[nc.row][nc.col]!='*' && M[nc.row][nc.col]!='@')
	      {
	        M[nc.row][nc.col] = '*';
	        S.push(nc);
	      }
          } 
        }
        n++;
      }
    }
  }
  return n;
}

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

int fill(int field[50][50], int x, int y, int w, int h){
    if(field[x][y] == 1){
        field[x][y] = 0;
        if(x+1<w) fill(field, x+1, y, w, h);
        if(y+1<h) fill(field, x, y+1, w, h);
        if(x-1>=0) fill(field, x-1, y, w, h);
        if(y-1>=0) fill(field, x, y-1, w, h);
        if(x+1<w && y+1<h) fill(field, x+1, y+1, w, h);
        if(x+1<w && y-1>=0) fill(field, x+1, y-1, w, h);
        if(x-1>=0 && y+1<h) fill(field, x-1, y+1, w, h);
        if(x-1>=0 && y-1>=0) fill(field, x-1, y-1, w, h);
        return 1;
    }
    return 0;
}

int main(){
    int w, h;
    int field[50][50];
    int result;
    cin >> w >> h;
    while(w || h){
        result = 0;
        for(int i=0;i<h;++i){
            for(int j=0;j<w;++j){
                cin >> field[j][i];
            }
        }
        for(int i=0;i<h;++i){
            for(int j=0;j<w;++j){
                result += fill(field, j, i, w, h);
            }
        }
        cout << result << endl;
        cin >> w >> h;
    }
}

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

using namespace std;

#define loop(i,a,b) for(int i=(a);i<int(b);i++)
#define rep(i,b) loop(i,0,b)

int c[200][200];
int vis[200][200];
int w,h;


void dfs(int ci, int cj){
    vis[ci][cj] = true;
    int di[] = {0,1,0,-1, 1,1,-1,-1};
    int dj[] = {1,0,-1,0, -1,1,-1,1};
    rep(d,8){
        int ni = ci+di[d];
        int nj = cj+dj[d];
        if(ni < 0 || nj < 0) continue;
        if(ni >= h || nj >= w) continue;
        if(vis[ni][nj]) continue;
        if(c[ni][nj]==0) continue;
        dfs(ni,nj);
    }
}

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

C++: #include <iostream>
#include <cstdio>
#include <cmath>
#include <vector>
#include <map>
#include <stack>
#include <queue>
#include <algorithm>
#include <set>

#define FOR(i,a,b) for(int i=(a);i<(b);i++)
#define REP(i,j) FOR(i,0,j)
#define mp std::make_pair

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

typedef unsigned long long ull;
typedef std::pair<int,int> P;

int W, H;
int map[50][50], used[50][50];

void walk(int x, int y){
    used[y][x] = 1;
    REP(i, 8){
        int nx = x+dx[i], ny = y+dy[i];
        if(0 <= nx && nx < W && 0 <= ny && ny < H && !used[ny][nx] && map[ny][nx] == 1){
            walk(nx, ny);
        }
    }
}

int main(){
    while(std::cin >> W >> H, W){
        REP(i, H){
            REP(j, W){
                std::cin >> map[i][j];
            }
        }

        REP(i, H){
            REP(j, W){
                used[i][j] = 0;
            }
        }

        int island_n = 0;
        REP(i, H){
            REP(j, W){
                if(map[i][j] == 1 && !used[i][j]){
                    walk(j, i);
                    island_n++;
                }
            }
        }

        std::cout << island_n << std::endl;
    }
}