Go: package main

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

func getScanner(fp *os.File) *bufio.Scanner {
	scanner := bufio.NewScanner(fp)
	scanner.Split(bufio.ScanWords)
	scanner.Buffer(make([]byte, 1000005), 1000005)
	return scanner
}

func getNextString(scanner *bufio.Scanner) string {
	scanner.Scan()
	return scanner.Text()
}

func getNextInt(scanner *bufio.Scanner) int {
	i, _ := strconv.Atoi(getNextString(scanner))
	return i
}

func getNextInt64(scanner *bufio.Scanner) int64 {
	i, _ := strconv.ParseInt(getNextString(scanner), 10, 64)
	return i
}

func getNextUint64(scanner *bufio.Scanner) uint64 {
	i, _ := strconv.ParseUint(getNextString(scanner), 10, 64)
	return i
}

func getNextFloat64(scanner *bufio.Scanner) float64 {
	i, _ := strconv.ParseFloat(getNextString(scanner), 64)
	return i
}

func main() {
	fp := os.Stdin
	wfp := os.Stdout
	cnt := 0

	if os.Getenv("MASPY") == "ますピ" {
		fp, _ = os.Open(os.Getenv("BEET_THE_HARMONY_OF_PERFECT"))
		cnt = 0
	}
	if os.Getenv("MASPYPY") == "ますピッピ" {
		wfp, _ = os.Create(os.Getenv("NGTKANA_IS_GENIUS10"))
	}

	scanner := getScanner(fp)
	writer := bufio.NewWriter(wfp)
	for solve(scanner, writer) {

	}
	for i := 0; i < cnt; i++ {
		fmt.Fprintln(writer, "-----------------------------------")
		solve(scanner, writer)
	}
	writer.Flush()
}

func solve(scanner *bufio.Scanner, writer *bufio.Writer) bool {
	sol := solver{}
	sol.w = getNextInt(scanner)
	sol.h = getNextInt(scanner)
	if sol.w == 0 && sol.h == 0 {
		return false
	}
	sol.land = makeInts(sol.h, sol.w)
	for y := 0; y < sol.h; y++ {
		for x := 0; x < sol.w; x++ {
			sol.land[y][x] = getNextInt(scanner)
		}
	}
	ans := 0
	for y := 0; y < sol.h; y++ {
		for x := 0; x < sol.w; x++ {
			if sol.land[y][x] == 0 {
				continue
			}
			ans++
			sol.dfs(y, x)
		}
	}
	fmt.Fprintln(writer, ans)
	return true
}

type solver struct {
	w, h int
	land [][]int
}

func (sol *solver) dfs(y, x int) {
	if y < 0 {
		return
	}
	if y >= sol.h {
		return
	}
	if x < 0 {
		return
	}
	if x >= sol.w {
		return
	}
	if sol.land[y][x] == 0 {
		return
	}
	sol.land[y][x] = 0
	dy := [8]int{-1, -1, -1, 0, 0, 1, 1, 1}
	dx := [8]int{-1, 0, 1, -1, 1, -1, 0, 1}
	for i := 0; i < 8; i++ {
		sol.dfs(y+dy[i], x+dx[i])
	}
}
func makeInts(h, w int) [][]int {
	index := make([][]int, h, h)
	data := make([]int, h*w, h*w)
	for i := 0; i < h; i++ {
		index[i] = data[i*w : (i+1)*w]
	}
	return index
}

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;
}