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>
#define REP(i, n) for (int i = 0; i < n; ++i)
#define REPR(i, n) for (int i = n - 1; i >= 0; --i)
#define FOR(i, a, b) for (int i = a; i < b; ++i)
#define FORR(i, a, b) for (int i = b - 1; i >= a; --i)
#define SORT(v) sort(v.begin(), v.end())
#define SORTR(v) sort(v.rbegin(), v.rend())
#define REV(v) reverse(v.begin(), v.end())
#define ITER(itr, v) for (auto itr = v.begin(); itr != v.end(); ++itr)
#define LB(v, x) (lower_bound(v.begin(), v.end(), x) - v.begin())
#define UB(v, x) (upper_bound(v.begin(), v.end(), x) - v.begin())
#define SZ(v) (int)v.size()
using namespace std;
using ll = long long;
using P = pair<int, int>;

class Grid {
	int h, w;
	vector<vector<int>> c;
	const int di[8] = {1, 1, 0, -1, -1, -1, 0, 1};
	const int dj[8] = {0, 1, 1, 1, 0, -1, -1, -1};

public:
	Grid(int h, int w) : h(h), w(w), c(h, vector<int>(w)) {}
	void input() {
		REP(i, h) {
			REP(j, w) {
				cin >> c[i][j];
			}
		}
	}
	int dfs(int i, int j) {
		if (c[i][j] == 0) return 0;
		c[i][j] = 0;
		REP(k, 8) {
			int u = i + di[k];
			int v = j + dj[k];
			if (u >= 0 && u < h && v >= 0 && v < w) {
				dfs(u, v);
			}
		}
		return 1;
	}
	int ans() {
		int res = 0;
		REP(i, h) {
			REP(j, w) {
				res += dfs(i, j);
			}
		}
		return res;
	}
};

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

	while (true) {
		int h, w;
		cin >> w >> h;
		if (h == 0 && w == 0) break;
		Grid grid(h, w);
		grid.input();
		cout << grid.ans() << endl;
	}

	return 0;
}

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

using namespace std;

typedef vector<int> vi;
typedef pair<int,int> pii;

#define dump(x)  cerr << #x << " = " << (x) << endl
#define rep(i,n) for(int i=0;i<(n);i++)
#define pb push_back

int map[60][60]={};
bool visited[60][60]={false};
int w,h;

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

void dfs(int x,int y){
    if(x<0 || x>=w || y<0 || y>=h)return ;
    if(map[y][x]==0)return ;
    if(visited[y][x]==true)return ;
    
    visited[y][x]=true;
    rep(i,8){
        dfs(x+dx[i],y+dy[i]);
    }
}

int main(){
    while(cin>>w>>h){
        rep(i,60)rep(j,60)visited[i][j]=false;
        int c=0;
        if(w==0 || h==0)break;
        
        rep(i,h) rep(j,w) cin>>map[i][j];
        
        rep(i,h){
            rep(j,w){
                if(visited[i][j]==false && map[i][j]==1){
                    dfs(j,i);
                    c++;
                }
            }
        }
        cout<<c<<endl;
        
    }
    
    return 0;
}

C++: #include <iostream>

using namespace std;
int c[51][51];
int w ,h;

bool input(){
	cin >> h >> w;
	if(w==0) return false;
	for(int i = 0;i <51;i++){
		for(int j = 0;j <51 ;j++){
			c[i][j] = 2;
		}
	}

	for(int i = 0;i < w ;i++){
		for(int j = 0; j < h ;j++){
			cin >> c[i][j];
//			cout <<c[i][j];
		}
	}
	return true;
}

void dfs(int x,int y){
	c[x][y] = 0;
	for(int i = -1; i <=1 ;i++){
		for(int j = -1; j <=1 ;j++){
			int nx = x + i;
			int ny = y + j;
//			cout <<nx << ' ' << ny << endl;
			if(0 <= nx && nx < w && 0 <= ny && ny < h && c[nx][ny] == 1)dfs(nx,ny);
		}
	}
}

void solve(){
	int res = 0;
/*	for(int i=0;i < w ;i++){
		for (int j=0;j<h;j++){
			cout <<c[i][j];
		}
		cout <<endl;
	}
*/
	for(int i=0; i < h;i++){
		for(int  j=0;j < w;j++){
			if(c[i][j] ==1){
				res++;
				dfs(i,j);
			}
		}
	}
	cout << res << endl;
}

int main(){
	while(input())solve();
}

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

using namespace std;

struct node{
  int x,y;
};

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

int main(){
  int w,h,i,j,k,ans=0;
  while(1){
    queue<node> que;
    scanf("%d %d",&w, &h);
    if(w+h == 0)break;
    int c[h][w];
    ans = 0;
    node now,next,to;
    for(i=0;i<h;i++){
      for(j=0;j<w;j++)scanf("%d",&c[i][j]);
    }
    for(i=0;i<h;i++){
      for(j=0;j<w;j++){
        if(c[i][j]){
          ++ans;
          now.x = j; now.y = i;
          que.push(now);
          while(!que.empty()){
            next = que.front();
            c[next.y][next.x] = 0;
            que.pop();
            for(k=0;k<8;k++){
              to.x = next.x + dx[k];
              to.y = next.y + dy[k];
              if(to.x<0 || to.x >=w || to.y<0 || to.y >=h)continue;
              if(c[to.y][to.x]){
                que.push(to);
              }
            }
          }
        }
      }
    }
    printf("%d",ans);
  }
  return 0;
}

C++: #include<bits/stdc++.h>
using namespace std;
using i64 = int_fast64_t;
#define rep(i, N) for(int i = 0; i < N; i++)
#define all(v) v.begin(), v.end()
#define eb emplace_back
#define Cout(s) cout << s << endl
string Yes(bool b) {if(b){return "Yes";} else {return "No";}}
string YES(bool b) {if(b){return "YES";} else {return "NO";}}

int main(){
    int W, H;
    while(cin >> W >> H, W != 0){

        vector<vector<int>> mp(H, vector<int>(W));
        rep(h, H) rep(w, W) cin >> mp[h][w];

        const int dy[8] = {1,1,-1,-1,1,-1,0,0};
        const int dx[8] = {1,0,-1,1,-1,0,-1,1};
        vector<vector<bool>> visited(H, vector<bool>(W, false));

        auto dfs = [&](auto&& dfs_, int y, int x) -> void {
            visited[y][x] = true;

            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) continue;
                if(visited[ny][nx] || mp[ny][nx] == 0) continue;

                dfs_(dfs_, ny, nx);
            }
        };

        int ans = 0;
        rep(h, H) rep(w, W){
            if(mp[h][w] == 1 && visited[h][w] == false){
                ans++;
                dfs(dfs, h, w);
            }
        }

        cout << ans << endl;
    }
}