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<iostream>

using namespace std;

int w, h;
int m[50][50];
void paint(int x, int y) {
	if (x < 0 || x >= w || y < 0 || y >= h) return;
	if (m[y][x] == 0) return;
	m[y][x] = 0;
	for (int dx = -1; dx <= 1; ++dx) for (int dy = -1; dy <= 1; ++dy) {
		paint(x + dx, y + dy);
	}
}
int main() {
	while (cin >> w >> h, w || h) {
		for (int y = 0; y < h; ++y) {
			for (int x = 0; x < w; ++x) {
				cin >> m[y][x];
			}
		}
		int ans = 0;
		for (int y = 0; y < h; ++y) {
			for (int x = 0; x < w; ++x) {
				if (m[y][x]) {
					paint(x, y);
					++ans;
				}
			}
		}
		cout << ans << endl;
	}
}

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

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

int W, H;
bool m[52][52];

void reset()
{
	rep(i, 52) rep(j, 52) m[i][j] = false;	
}

void saiki(int r, int c)
{
	if (!m[r][c]) return;
	m[r][c] = false;
	int dr[] = {-1, -1, -1,  0,  0,  1,  1,  1};
	int dc[] = {-1,  0,  1, -1,  1, -1,  0,  1};
	rep(i, 8) saiki(r+dr[i], c+dc[i]);
}


int main(void){
	cin >> W >> H;
	while (H != 0) {
		reset();
		rep(i, H) rep(j, W) {
			int t;
			cin >> t;
			m[i+1][j+1] = t == 1;
		}
		int res = 0;
		rep(i, H) rep(j, W) {
			if (m[i+1][j+1]) {
				res++;
				saiki(i+1, j+1);
			}
		}
		cout << res << endl;
		cin >> W >> H;
	}
}

C++: #include <bits/stdc++.h>

using namespace std;

typedef long long ll;
typedef pair<int,int> P;

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(j,i);
              res++;
          }
      }
      cout<<res<<endl;
  }
}

C++: #include <iostream>

#define MAX 51

int map[MAX][MAX];
int w, h;

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


int main()
{

    int cnt;

    while(std::cin >> w >> h, w+h) {
        cnt = 0;
        for (int i = 0; i < h; i++) {
            for (int j = 0; j < w; j++) {
                std::cin >> map[i][j];
            }
        }

        for (int i = 0; i < h; i++) {
            for (int j = 0; j < w; j++) {
                if (map[i][j] == 1) {
                    cnt++;
                    dfs(i, j);
                }
            }
        }

        std::cout << cnt << std::endl;

    }

return 0;
}

C++: #include <cstdio>
#define f(i,n)for(int i=0;i<n;i++)
#define s (scanf("%d",&e),e)
int main(){int a=0,w,n,m,b,e,d[99];f(i,99)d[i]=0;n=s;m=s;if(n==0)return 0;f(i,m){w=b=0;f(j,n){s;if(e)f(k,3){e=d[j+k];if(e&&e-w){if(w){f(l,n+2)d[l]=d[l]==e?w:d[l];a--;}else w=e;}}if(w==0)a++,w=i*n+j+1;else w=0;d[j]=b;b=w;}d[n]=b;w=0;}printf("%d\n",a);main();}

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

const int hmax = 50;
int w, h;
void move(string area[], int i, int j){
	area[i][j] = '0';
	for(int k = -1; k <= 1; k++){
		for(int l = -1; l <= 1; l++){
			if(i+k < h && i+k >= 0 && j+l < w && j+l >= 0)
				if(area[i+k][j+l] == '1') move(area,i+k,j+l);
		}
	}
}

int main(){
	while(cin >> w >> h){
		string area[hmax];
		if(w == 0 && h == 0) break;
		for(int i = 0; i < h; i++){
			string s;
			for(int j = 0; j < w; j++){
				char c;
				cin >> c;
				s.push_back(c);
			}
			area[i] = s;
		}
		int count = 0;
		for(int i = 0; i < h; i++){
			for(int j = 0; j < w; j++){
				if(area[i][j] == '1'){
					count++;
					move(area,i,j);
				}
			}
		}
		cout << count << endl;
	}
	return 0;
}