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 rep(i, n) for(int(i) = 0; i < (int)(n); (i)++)
using Graph = vector<vector<int>>;

int w, h;
ll cnt = 0;
Graph G;
struct Direct {
    int x;
    int y;
};

Direct direction[8] = {{-1, -1}, {0, -1}, {1, -1}, {-1, 0},
                       {1, 0},   {-1, 1}, {0, 1},  {1, 1}};

void dfs(int x, int y) {
    if(G[y][x] == 1) {
        G[y][x] = 0;
    }
    rep(i, 8) {
        if(0 <= x + direction[i].x && x + direction[i].x < w) {
            if(0 <= y + direction[i].y && y + direction[i].y < h) {
                if(G[y + direction[i].y][x + direction[i].x] == 1) {
                    dfs(x + direction[i].x, y + direction[i].y);
                }
            }
        }
    }
}

int main() {
    while(1) {
        cnt=0;
        cin >> w >> h;

        G = Graph(h);
        if(!w && !h) break;
        rep(i, h) {
            G[i] = vector<int>(w);
        }
        rep(i, h) {
            rep(j, w) {
                cin >> G[i][j];
            }
        }
        rep(i, h) {
            rep(j, w) {
                if(G[i][j]) {
                    cnt++;
                    dfs(j, i);
                }
            }
        }
        cout << cnt << endl;
    }
    return 0;
}

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

typedef struct{
	int first;
	int second;
}point;

int main(){
	while(1){
		int w, h;
		int field[50][50];
		int dx[8]={1,1,0,-1,-1,-1,0,1}, dy[8]={0,1,1,1,0,-1,-1,-1};
		
		scanf(" %d %d", &w, &h);
		if(w==0) break;
		
		for(int i=0; i<h; ++i)
		for(int j=0; j<w; ++j)	
		scanf(" %d", &field[i][j]);
		
		int ans=0;
		for(int i=0; i<h; ++i){
			for(int j=0; j<w; ++j){
			
				if(field[i][j]==1){
					//printf("%d %d\n", i, j);
					queue<point> que;
					point p;
					p.first=i; p.second=j;
					field[i][j]=2; //訪問済み
					que.push(p);
					++ans;
					
					//bfs
					while(!que.empty()){
						p=que.front();
						que.pop();
						for(int k=0; k<8; ++k){
							int nf=p.first+dy[k], ns=p.second+dx[k];
							if(nf>=0 && nf<h && ns>=0 && ns<w && field[nf][ns]==1){
								field[nf][ns]=2;
								point q;
								q.first=nf;
								q.second=ns;
								que.push(q);
							}	
						}
					
					}
		
				}
			}
			
		}
		printf("%d\n", ans);
	}
	return 0;
}

C++: #include<iostream>

using namespace std;

bool map[50][50];
int h,w;
int dx[]={-1,0,1},dy[]={-1,0,1};
void solve(void);
void loop(int ,int );
void draw(void);
int main(){
	while(cin>>h>>w,h){
		for(int i=0;i<w;i++){
			for(int j=0;j<h;j++){
				cin>>map[i][j];
			}
		}
		solve();
	}
	return 0;
}
void solve(void){
	int cnt=0;
	for(int i=0;i<w;i++){
		for(int j=0;j<h;j++){
			if(map[i][j]){
				loop(i,j);	
				cnt++;	
			}
		}
	}
	cout<<cnt<<endl;
}
void loop(int x,int y){
	map[x][y]=0;
	for(int i=0;i<3;i++){
		for(int j=0;j<3;j++){
			
			if(map[x+dx[i]][y+dy[j]]){
				loop(x+dx[i],y+dy[j]);
			}
		}
	}
	return ;
}

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

void dfs(vector<vector<int> > &map, int x, int y){
    map[x][y] = 0;
    for(int dx = -1; dx <= 1; dx++){
        for(int dy = -1; dy <= 1; dy++){
            int nx = x + dx, ny = y + dy;
            if(1 <= nx && nx <= map.size()-2 && 1 <= ny && ny <= map[dx].size()-2 && map[nx][ny] == 1) dfs(map, nx, ny);
        }
    }
}

int main(){
    while(1){
        int W, H;
        cin >> W >> H;
        if(W == 0) break;
    
        vector<vector<int> > map(H+2, vector<int>(W+2));
        for(int i = 1; i <= H; i++){
            for(int j = 1; j <= W; j++){
                cin >> map[i][j];
            }
        }
    
        int res = 0;
        for(int i = 1; i <= H; i++){
            for(int j = 1; j <= W; j++){
                if(map[i][j] == 1){
                    dfs(map, i, j);
                    res++;
                }
            }
        }
    
        cout << res << endl;
    }
    return 0;
}

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

//const ll mod =  1000000007;
//const ll maxn = 1000000000;

//---------------------------------------------------------------------------------------------------
using vi = vector<int>; // intの1次元の型に vi という別名をつける
using vll = vector<ll>; // intの1次元の型に vi という別名をつける
using vvll = vector<vll>; // intの2次元の型に vvi という別名をつける
using vs = vector<string>; // stringの1次元の型に vs という別名をつける
using pll = pair<ll, ll>; // これ以降 pii という型名はpair<ll, ll> と同じ意味で使える
//---------------------------------------------------------------------------------------------------

int w,h;
vvll a(50,vll(50));

void dfs(int x,int y){
     a.at(x).at(y)=0;
     rep2(dx,-1,2){
       rep2(dy,-1,2){
         int  nx=x+dx;
         int  ny=y+dy;
           if(0<=nx&&nx<h&&0<=ny&&ny<w){
                if(a.at(nx).at(ny)==1){
                    dfs(nx,ny) ;         
                           }
                       }
                   }
               }                                  
}


int main() {
   while(1){
   cin>>w>>h;
   if(w==0&&h==0)break;
   else{
   rep(i,h){
       rep(j,w)cin>>a.at(i).at(j);
   }
  
   int ans=0;
   rep(i,h){
       rep(j,w){
           if(a.at(i).at(j)==1){
               
               
               dfs(i,j);
               ans++;
               
           }
       }
   }
   
  
   cout<<ans<<endl;
   }
   }
    
}