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;
#define int long long
typedef pair<int,int> P;
signed main(){
  int w,h;
  while(cin>>w>>h,w||h){
    int c[h][w];
    for(int i=0;i<h;i++)
      for(int j=0;j<w;j++)
	cin>>c[i][j];
    int ans=0;
    int ax[]={-1,0,1,-1,1,-1,0,1};
    int ay[]={-1,-1,-1,0,0,1,1,1};
    int used[h][w];
    memset(used,0,sizeof(used));
    for(int i=0;i<h;i++){
      for(int j=0;j<w;j++){
	if(used[i][j]) continue;
	if(!c[i][j]) continue;
	ans++;
	queue<P> q;
	q.push(P(i,j));
	while(!q.empty()){
	  P p=q.front();q.pop();
	  int y=p.first,x=p.second;
	  //cout<<y<<" "<<x<<endl;
	  if(used[y][x]) continue;
	  if(!c[y][x]) continue;
	  used[y][x]=1;
	  for(int k=0;k<8;k++){
	    int ny=y+ay[k],nx=x+ax[k];
	    if(ny<0||ny>=h||nx<0||nx>=w) continue;
	    if(used[ny][nx]) continue;
	    if(!c[ny][nx]) continue;
	    q.push(P(ny,nx));
	  }
	}
      }
    }
    cout<<ans<<endl;
  }
  return 0;
}

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

#define rep(i, m, n) for(int(i) = (int)(m); i < (int)(n); ++i)
#define rep2(i, m, n) for(int(i) = (int)(n)-1; i >= (int)(m); --i)
#define REP(i, n) rep(i, 0, n)
#define REP2(i, n) rep2(i, 0, n)
#define all(hoge) (hoge).begin(), (hoge).end()
#define en '\n'
using ll = long long;
using ull = unsigned long long;
using ld = long double;
template <class T>
using vec = vector<T>;
template <class T>
using vvec = vector<vec<T>>;
typedef pair<ll, ll> P;
constexpr long long INF = 1LL << 60;
constexpr int INF_INT = 1 << 25;
constexpr long long MOD = (ll)1e9 + 7;
//constexpr long long MOD = 998244353LL;
using ld = long double;
static const ld pi = 3.141592653589793L;
typedef vector<ll> Array;
typedef vector<Array> Matrix;

template <class T>
inline bool chmin(T &a, T b) {
    if(a > b) {
        a = b;
        return true;
    }
    return false;
}
template <class T>
inline bool chmax(T &a, T b) {
    if(a < b) {
        a = b;
        return true;
    }
    return false;
}

struct Edge {
    ll to, rev;
    long double cap;
    Edge(ll _to, long double _cap, ll _rev) {
        to = _to;
        cap = _cap;
        rev = _rev;
    }
};
using Edges = vector<Edge>;
using Graph = vector<Edges>;

void add_edge(Graph &G, ll from, ll to, long double cap, bool revFlag,
              long double revCap) {
    G[from].push_back(Edge(to, cap, (ll)G[to].size()));
    if(revFlag)
        G[to].push_back(Edge(from, revCap, (ll)G[from].size() - 1));
}

void solve() {
    int w, h;
    while(cin >> w >> h) {
        if(w == 0 and h == 0)
            break;
        Matrix c(h, Array(w));

        REP(i, h) {
            REP(j, w) {
                cin >> c[i][j];
            }
        }

        Matrix used(h, Array(w, false));

        auto dfs = [&](auto &&self, int i, int j) -> ll {
            used[i][j] = true;
            ll ret = 1;
            for(int ni = max(i - 1, 0); ni <= min(i + 1, h - 1); ni++) {
                for(int nj = max(j - 1, 0); nj <= min(j + 1, w - 1); nj++) {
                    if(used[ni][nj])
                        continue;
                    if(c[ni][nj])
                        ret += self(self, ni, nj) + 1;
                }
            }
            return ret;
        };

        ll ans = 0;
        REP(i, h) {
            REP(j, w) {
                if(!c[i][j] or used[i][j])
                    continue;
                dfs(dfs, i, j);
                ans++;
            }
        }
        cout << ans << en;
    }
}

int main() {
    ios::sync_with_stdio(false);
    cin.tie(0);
    cout.tie(0);

    solve();

    /*
    ll t;
    cin >> t;
    REP(i, t) { solve();}
    */

    return 0;
}

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

const int dy[] = {-1, -1, -1, 0, 0, 1, 1, 1};
const int dx[] = {-1, 0, 1, -1, 1, -1, 0, 1};
int w, h;
int c[50][50];

void dfs(int y, int x) {
    c[y][x] = 0;
    for (int i = 0; i < 8; ++i) {
        int ny = y + dy[i];
        int nx = x + dx[i];
        if (ny >= 0 && ny < h && nx >= 0 && nx < w && c[ny][nx]) {
            dfs(ny, nx);
        }
    }    
}

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

C++: #include<bits/stdc++.h>
using namespace std;
#define rp(i,j) for(long long i=0;i<j;i++)
#define rep(i,j,k) for(long long i=j;i<k;i++)
#define mod 1000000007
#define NIL -1
#define MAX 50
const int INFTY=(1<<21);
typedef long long ll;
int H,W;
int c[MAX][MAX];
int checked[MAX][MAX]={0};
void dfs(int x,int y){
    if(x<0||x>=H||y<0||y>=W||c[x][y]==0)return;
    if(checked[x][y]==1)return;
    checked[x][y]=1;
    rep(i,-1,2){
        rep(j,-1,2){
            dfs(x+i,y+j);
        }
    }
}
int main(){
    while(1){
        int ans=0;
        cin>>W>>H;
        if(H==0&&W==0)break;
        else {
            rp(i,H){
                rp(j,W){
                    cin>>c[i][j];
                    checked[i][j]=0;
                }
            }
            rp(i,H){
                rp(j,W){
                    if(c[i][j]==1&&checked[i][j]==0){
                        dfs(i,j);
                        ans++;
                    }
                }
            }
        }
        cout<<ans<<endl;
    }
}

C++: #include<iostream>
#include<algorithm>
using namespace std;
int H,W,k[100][100],n[100][100],color=1,i,j,l,m,o,r;
int main(){cin>>H>>W;for(i=0;i<H;i++){for(j=0;j<W;j++){cin>>k[i][j];}}
for(i=0;i<H;i++){for(j=0;j<W;j++){if(r==1){color++;}r=0;if(k[i][j]==1 && n[i][j]==0){n[i][j]=color;for(l=0;l<100;l++){
	for(m=0;m<H;m++){for(o=0;o<W;o++){if(n[m][o]==color){
		for(int p=-1;p<=1;p++){for(int q=-1;q<=1;q++){if(m-p>=0 && m-p<H){if(o-q>=0 && o-q<W){n[m][o]=color;}}}}
	}}}
}}}}
cout<<color<<endl;
}