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>
#include <vector>
#include <string>
#include <algorithm>
#include <cmath>
#include <iomanip>
#include <map>
#include <unordered_map>
#include <cstring>

using namespace std;
using ll = long long;
using pint = pair<int, int>;
using Graph = vector<vector<int>>;

ll INF = 1LL << 60;

int W, H;

void dfs(Graph& field, int h, int w) {
	field[h][w] = 0;
	for (int dh = -1; dh <= 1; dh++) {
		for (int dw = -1; dw <= 1; dw++) {
			if (dh == 0 && dw == 0) continue;
			int nh = h + dh;
			int nw = w + dw;
			if (nh < 0 || nh >= H || nw < 0 || nw >= W) continue;
			if (field[nh][nw] == 0) continue;
			dfs(field, nh, nw);
		}
	}
}

int main() {
	while (cin >> W >> H) {
		if (W == 0 && H == 0) break;
		Graph field(H, vector<int>(W));
		for (int h = 0; h < H; h++) {
			for (int w = 0; w < W; w++) cin >> field[h][w];
		}
		int count = 0;
		for (int h = 0; h < H; h++) {
			for (int w = 0; w < W; w++) {
				if (field[h][w] == 1) {
					dfs(field, h, w);
					count++;
				}
			}
		}
		cout << count << endl;
	}
	return 0;
}

C++: #include<bits/stdc++.h>
using namespace std;

int W,H;
int C[111][111];

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

bool used[111][111];
void solve(int x,int y){
  if( used[x][y] ) return;
  used[x][y] = true;
  for(int i=0;i<8;i++){
    int nx = x + dx[i], ny = y + dy[i];
    if( nx < 0 || ny < 0 || nx >= W || ny >= H ) continue;
    if( C[nx][ny] == 1 )
      solve( nx, ny );
  }
}

int main(){

  while(  cin >> W >> H && (W||H) ){
    for(int i=0;i<H;i++){
      for(int j=0;j<W;j++){
	cin >> C[j][i];
      }
    }
    
    int res = 0;
    memset(used,0,sizeof(used));
    for(int i=0;i<H;i++){
      for(int j=0;j<W;j++){
	if( !used[j][i] && C[j][i] == 1 )
	  {
	    res++;
	    solve(j,i);
	  }
      }
    }
    cout << res << endl;
  }


}

C++: #include <iostream>

using namespace std;
int field[50][50];
int w, h, ans;
int dx[8] = {-1, 0, 1, -1, 1, -1, 0, 1}, dy[8] = {-1, -1, -1, 0,  0, 1, 1, 1};

void check(int x, int y)
{
    if (field[x][y])
        field[x][y] = 0;
    else
        return;
        
    for (int i=0; i<8; i++) {
        int nx = x+dx[i], ny = y+dy[i];
        if (0<= nx && nx<w && 0<=ny&&ny<h) {
            check(nx, ny);
        }
    }
}

int main()
{
    while (cin>>w>>h, w||h) {
        for (int y=0; y<h; y++) {
            for (int x=0; x<w; x++) {
                int i; cin >> i;
                field[x][y] = i;
                }
           }
       
       ans = 0;
       for (int y=0; y<h; y++) {
          for (int x=0; x<w; x++) {
               if (field[x][y]) {
                       ans++;
                       check(x, y);
               }  
            }   
        }

        cout << ans << endl;
   }           
    
    return 0;
}

C++: #include <math.h>
#include <stdlib.h>
#include <string.h>
#include <algorithm>
#include <deque>
#include <iomanip>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <vector>

using namespace std;

#define ll long long
#define INF 1e9
#define MOD 1e9 + 7
#define rep(i, n) for (int i = 0; i < n; i++)
#define loop(i, a, n) for (int i = a; i < n; i++)
#define all(in) in.begin(), in.end()
int c[51][51];
int w, h;
void dfs(int x, int y) {
    c[x][y] = 0;
    for (int i = -1; i <= 1; i++) {
        for (int k = -1; k <= 1; k++) {
            int new_x = x + i, new_y = y + k;
            if (0 <= new_x && new_x < h && 0 <= new_y && new_y < w &&
                c[new_x][new_y] == 1) {
                dfs(new_x, new_y);
            }
        }
    }
}

int main() {
    while (true) {
        int ans = 0;
        cin >> w >> h;
        if (w == 0 && h == 0) break;
        rep(i, h) rep(k, w) cin >> c[i][k];
        for (int x = 0; x < h; x++) {
            for (int y = 0; y < w; y++) {
                if (c[x][y] == 1) {
                    ans++;
                    dfs(x, y);
                }
            }
        }
        cout << ans << endl;
    }
    return 0;
}

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

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

int mesh[50][50];
int n, m;

struct Point
{
	int x;
	int y;
};

int main()
{
	deque<Point> stack;
	while (true)
	{
		scanf("%d %d", &n, &m);
		if (n == 0 && m == 0) break;
		for (int i = 0; i < m; i++)
		{
			for (int j = 0; j < n; j++)
			{
				int a;
				scanf("%d", &a);
				mesh[i][j] = a;
			}
		}
		int count = 0;
		for (int i = 0; i < m; i++)
		{
			for (int j = 0; j < n; j++)
			{
				if (mesh[i][j] == 1)
				{
					Point pt = {j, i};
					stack.push_front(pt);
					mesh[i][j] = 0;
					while (!stack.empty())
					{
						Point point = stack.front();
						stack.pop_front();
						for (int k = 0; k < 8; k++)
						{
							if (mesh[point.y + dy[k]][point.x + dx[k]] == 1)
							{
								Point pt = {point.x + dx[k], point.y+dy[k]};
								stack.push_front(pt);
								mesh[point.y + dy[k]][point.x + dx[k]] = 0;
							}
						}
					}
					count++;
				}
			}
		}
		printf("%d\n", count);
	}
}