Go: package main

import (
	"bufio"
	"fmt"
	"io"
	"os"
	"strconv"
)

const (
	initialBufSize = 100000
	maxBufSize     = 1000000
)

var sc *bufio.Scanner

func initScanner(r io.Reader) *bufio.Scanner {
	buf := make([]byte, initialBufSize)

	sc := bufio.NewScanner(r)
	sc.Buffer(buf, maxBufSize)
	sc.Split(bufio.ScanWords) // bufio.ScanLines
	return sc
}

func main() {
	sc = initScanner(os.Stdin)
	resolve()
}

func resolve() {
	results := []int{}
	for true {
		w, h := scanInt(sc), scanInt(sc)
		if w == 0 && h == 0 {
			break
		}
		grid := make([][]int, h)
		for i := 0; i < h; i++ {
			grid[i] = make([]int, w)
			for j := 0; j < w; j++ {
				grid[i][j] = scanInt(sc)
			}
		}
		num := 0
		for i := range loop0(h) {
			for j := range loop0(w) {
				start := pos{y: i, x: j}
				if grid[start.y][start.x] == 0 {
					continue
				}
				num++
				_dfs(grid, w, h, start)
			}
		}
		results = append(results, num)
	}
	for _, v := range results {
		fmt.Println(v)
	}
	return
}

func _dfs(grid [][]int, w, h int, start pos) {
	grid[start.y][start.x] = 0
	for _, df := range []pos{
		pos{1, 0}, pos{-1, 0},
		pos{0, -1}, pos{0, 1},
		pos{1, 1}, pos{-1, -1},
		pos{-1, 1}, pos{1, -1},
	} {
		np := addPos(start, df)
		if np.y < 0 || h <= np.y || np.x < 0 || w <= np.x {
			continue
		}
		if grid[np.y][np.x] == 0 {
			continue
		}
		_dfs(grid, w, h, np)
	}
}

type pos struct{ x, y int }

func addPos(p, d pos) pos {
	return pos{
		x: p.x + d.x,
		y: p.y + d.y,
	}
}

func loop0(n int) chan int {
	return loop(0, n-1, 1)
}

func loop1(n int) chan int {
	return loop(1, n, 1)
}

func loop(s, e, d int) chan int {
	ch := make(chan int)
	go func() {
		for i := s; i <= e; i += d {
			ch <- i
		}
		close(ch)
	}()
	return ch
}

// snip-scan-funcs
func scanInt(sc *bufio.Scanner) int {
	sc.Scan()
	i, _ := strconv.Atoi(sc.Text())
	return int(i)
}
func scanString(sc *bufio.Scanner) string {
	sc.Scan()
	return sc.Text()
}

C++: #include <bits/stdc++.h>
using namespace std;
typedef long long ll;
#define i_7 (ll)(1E9+7)
#define i_5 (ll)(1E9+5)
ll mod(ll a){
    ll c=a%i_7;
    if(c>=0)return c;
    else return c+i_7;
}
typedef pair<int,int> i_i;
typedef pair<ll,ll> l_l;
ll inf=(ll)1E12;//10^12
#define rep(i,l,r) for(ll i=l;i<=r;i++)
#define pb push_back
ll max(ll a,ll b){if(a<b)return b;else return a;}
ll min(ll a,ll b){if(a>b)return b;else return a;}
const double EPS=1E-8;
////////////////////////////////////////

int w,h;
bool m[55][55];

void dfs(int k,int l){
    
    rep(i,-1,1)rep(j,-1,1){
        int x=k+i;int y=l+j;
        if(0<=x&&x<=h-1&&0<=y&&y<=w-1&&m[x][y]){
            
            m[x][y]=false;
            dfs(x,y);
        }
    }
}


int main() {
    ios::sync_with_stdio(false);cin.tie(0);
    
    while(cin>>w>>h){
        if(w==0)break;
        rep(i,0,h-1)rep(j,0,w-1)cin>>m[i][j];
        int ans=0;
        rep(i,0,h-1)rep(j,0,w-1){
            if(m[i][j]){
                ans++;
                m[i][j]=false;
                dfs(i,j);
            }
        }
        cout<<ans<<endl;
    }
    return 0;
}

C++: #include <bits/stdc++.h>
#define int long long
#define double long double
#define INF 1e18
using namespace std;
int grid[100][100];
int H, W;

void dfs(int x, int y) {
  int mx[8] = { 1, 1, 0, -1, -1, -1, 0, 1 };
  int my[8] = { 0, 1, 1, 1, 0, -1, -1, -1 };
  for (int i = 0; i < 8; i++) {
    int _x = x + mx[i];
    int _y = y + my[i];
    if (grid[_x][_y] == 1 && _x >= 0 && _y >= 0 && _x < W && _y < H) {
      grid[_x][_y] = 0;
      dfs(_x, _y);
    }
  }
}

signed main() {
  while (cin >> W >> H, W+H!=0) {
    for (int i = 0; i < H; i++) {
      for (int j = 0; j < W; j++) {
        cin >> grid[j][i];
      }
    }
 
    int ans = 0; 
    for (int i = 0; i < H; i++) {
      for (int j = 0; j < W; j++) {
        if (grid[j][i] == 1) {
          dfs(j,i);
          ans++;
        }
      }
    }

    cout << ans << endl;
  }
}

C++: #include<bits/stdc++.h>
using namespace std;
int m[50][50];
void del(int a,int b){
	m[a][b]=0;
	if(m[a-1][b]==1)del(a-1,b);
	if(m[a][b-1]==1)del(a,b-1);
	if(m[a+1][b]==1)del(a+1,b);
	if(m[a][b+1]==1)del(a,b+1);
	if(m[a-1][b-1]==1)del(a-1,b-1);
	if(m[a-1][b+1]==1)del(a-1,b+1);
	if(m[a+1][b-1]==1)del(a+1,b-1);
	if(m[a+1][b+1]==1)del(a+1,b+1);
}
int main(){
	int a,b,i,j,w,q;
	while(cin>>a>>b,a!=0&&b!=0){
		int c=0;
		for(i=0;i<b;i++)for(j=0;j<a;j++)cin>>m[i][j];
        for(i=0;i<b;i++)for(j=0;j<a;j++)if(m[i][j]==1){del(i,j);c++;}
        	cout<<c<<endl;
    }
}

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

int w,h;
int g[100][100];

void dfs(int y, int x){
  g[y][x] = 0;

  for(int i=-1;i<=1;i++){
    for(int j=-1;j<=1;j++){
      if(y+i<0 || x+j<0 || y+i>=h || x+j>=w)continue;
      if(g[y+i][x+j])dfs(y+i,x+j);
    }
  }
}

int main(){
  while(cin >> w >> h, w){
    for(int i=0;i<h;i++){
      for(int j=0;j<w;j++)cin >> g[i][j];
    }

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

C++: #include <algorithm>
#include <iostream>

using namespace std;
#define REP(i, n) for (int i = 0; i < n; i++)

int w, h;
int C[50][50] = {};

void visit(int x, int y) {
  C[x][y] = 0;
  for (int ix = x - 1; ix <= x + 1; ix++) {
    for (int iy = y - 1; iy <= y + 1; iy++) {
      if (ix >= 0 && iy >= 0 && ix < w && iy < h && C[ix][iy] == 1) {
        visit(ix, iy);
      }
    }
  }
}

int main() {
  while (cin >> w >> h, w) {
    int ans = 0;
    C[50][50] = {};
    REP(j, h) {
      REP(i, w) { cin >> C[i][j]; }
    }
    REP(i, w) {
      REP(j, h) {
        if (C[i][j] == 1) {
          ans++;
          visit(i, j);
        }
      }
    }
    cout << ans << endl;
  }
}

C++: #include <bits/stdc++.h>
#define rep(i,n) for (int i = 0; i < (n); ++i)
#define all(v) v.begin(), v.end()
using namespace std;
typedef long long ll;

int W, H;
// dfs
// https://qiita.com/drken/items/4a7869c5e304883f539b
void dfs(vector<vector<int>> &g, int h, int w) {
	g[h][w] = 0;
	for(int dh = -1; dh <= 1; dh++) {
		for(int dw = -1; dw <= 1; dw++) {
			int next_h = h + dh;
			int next_w = w + dw;
			if(next_h < 0 || next_h >= H || next_w < 0 || next_w >= W) continue;
			if(g[next_h][next_w] == 0) continue;
			dfs(g, next_h, next_w);
		}
	}
}

int main() {
	while(1) {
		cin >> W >> H;
		if(W == 0 && H == 0) break;
		vector<vector<int>> c(H, vector<int>(W));
		rep(i, H) rep(j, W) {
			cin >> c[i][j];
		}
		int cnt = 0;
		rep(i, H) rep(j, W) {
			if(c[i][j] == 0) continue;
			dfs(c, i, j);
			cnt++;
		}
		cout << cnt << endl;
	}
	return 0;
}

C++: #include <cstdio>
#define f(n)for(int i=0;i<n;i++)
#define s(w)scanf("%d",&w)
int a,n,m,d[99][99];
int u[16]={-1,-1,-1,0,0,1,1,1,-1,0,1,-1,1,-1,0,1};
void k(int x,int y){
	if(d[x][y]){f(8)k(x+u[i],y+u[i+8]);d[x][y]=0;}
}
int main(){
	a=0;
	s(n);s(m);
	f(9801)d[0][i]=0;
	f(n*m)s(d[i/n+1][i%n+1]);
	//f(n*m)if(d[i/n+1][i%n+1])a++,k(i/n+1,i%n+1);
	//printf("%d\n",a);
	//main();
}

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

int field[64][64];
bool visited[64][64];
int dx[] = {0, 0, 1, -1, 1, 1, -1, -1}, dy[] = {1, -1, 0, 0, 1, -1, 1, -1};

void rec(int y, int x)
{
	if (visited[y][x] == false){
		return;
	}
	visited[y][x] = false;
	for (int i = 0; i < 8; i++){
		rec(y + dy[i], x + dx[i]);
	}
	return;
}

int main(void)
{
	int w, h;
	while (cin >> w >> h, w && h){
		for (int i = 0; i < 64; i++){
			for (int j = 0; j < 64; j++){
				field[i][j] = 0;
				visited[i][j] = false;
			}
		}
		for (int i = 1; i <= h; i++){
			for (int j = 1; j <= w; j++){
				cin >> field[i][j];
				if (field[i][j] == 1){
					visited[i][j] = true;
				}
			}
		}
		int ret = 0;
		for (int i = 0; i < 64; i++){
			for (int j = 0; j < 64; j++){
				if (visited[i][j] == true){
					rec(i, j);
					ret++;
				}
			}
		}
		cout << ret << endl;
	}
	
	return 0;
}