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 <iostream>
#include <cstdio>
#include <cstring>
#include <cstdlib>
#include <cmath>
#include <vector>
#include <string>
#include <map>
#include <queue>
#include <stack>
#include <algorithm>

#define rep(i,j) REP((i), 0, (j))
#define REP(i,j,k) for(int i=(j);(i)<(k);++i)
#define between(a,x,b) ((a)<=(x)&&(x)<=(b))
#define ck(i, x) ((i) >= 0 && (i) < (x))
using namespace std;
int w, h;
int area[51][51];
int dx[] = {-1,0,1}, dy[] = {-1,0,1};
void dfs(int y, int x){
  area[y][x] = 0;
  //  printf("%d %d\n", y, x);
  rep(i, 3) rep(j, 3)
    if(ck(y+dy[i], h) && ck(x+dx[j], w) && area[y+dy[i]][x+dx[j]]) dfs(y+dy[i], x+dx[j]);

  return;
}
int main(){
  while(scanf("%d%d", &w, &h) && w && h){
    //    printf("in area\n");
    rep(i, h) rep(j, w) scanf("%d", &area[i][j]);
    int cnt = 0;
    //    puts("start solve");
    rep(i, h) rep(j, w) if(area[i][j]) dfs(i, j), cnt++;
    //    puts("end solve");
    printf("%d\n", cnt);
  }
  return 0;
}

C++: #include <bits/stdc++.h>

using namespace std;

#define pb push_back
#define fi first
#define se second
#define FOR(i, a, b) for(int i=(a);i<(b);i++)
#define REP(i, n) FOR(i, 0, n)
#define RFOR(i, a, b) for(int i=(a);i>=(b);i--)
#define RREP(i, n) FOR(i, n, 0)
#define MFOR(i, m) for(auto i=(m).begin();i!=(m).end();i++)
#define ALL(a) (a).begin(), (a).end()
#define SZ(x) ((int)(x).size())

typedef long long int ll;
typedef pair<int, int> P;
typedef vector<int> vi;
typedef vector<vi> vvi;

const double eps = 1e-10;
const int MOD = 1000000007;

template<typename T>
void printv(vector<T> const& s) {
  REP(i, s.size()) {
    cout << s[i] << " ";
  }
  cout << endl;
}

int main () {
  cin.tie(0);
  cout << setprecision(10);

  
  while(1) {
    int w, h; cin >> w >> h;
    if(w == 0 && h == 0) break;
    vector<vector<bool>> c(h);
    int tmp = 0;
    REP(i, h) {
      c[i].resize(w);
      REP(j, w) {
        cin >> tmp;
        c[i][j] = (tmp == 1);
      }
    }

    int cnt = 0;
    vector<vector<bool>> sel(h);
    REP(i, h) {
      sel[i].resize(w, false);
    }

    REP(i, h) {
      REP(j, w) {
        if(!sel[i][j] && c[i][j]) {
          P st = make_pair(i, j);
          // cout << i << ":" << j << endl;
          vi dx = {-1, 0, 1}, dy = {-1, 0, 1};
          queue<P> q;
          q.push(st);
          sel[i][j] = true;
          while(!q.empty()) {
            P now = q.front();q.pop();
            REP(k, 3) {
              REP(l, 3) {
                int tmpx = now.fi + dx[k], tmpy = now.se + dy[l];
                if(0 <= tmpx && tmpx <= h-1 &&
                   0 <= tmpy && tmpy <= w-1 &&
                   !sel[tmpx][tmpy] && c[tmpx][tmpy] && (dx[k] != 0 || dy[l] != 0)) {
                  q.push(make_pair(tmpx, tmpy));
                  sel[tmpx][tmpy] = true;
                }
              }
            }
          }
          cnt++;
        }
      }
    }

    cout << cnt << endl;
  }

}

C++: #include<bits/stdc++.h>
using namespace std;
#define OUT(x) cout<<x<<endl
#define DEBUG(x) cout<<#x<<": "<<x
const int BA = 1000;
const int INFTY = (1<<21);
int c[51][51];
int w,h;
int del(int x,int y){
  if(x<0 || y<0 || x>=w || y>= h)return 0;
  if(c[x][y]){
    c[x][y] = 0;
    return del(x+1,y)+del(x,y+1)+del(x+1,y+1)+
      del(x-1,y-1)+del(x-1,y)+del(x,y-1)+del(x+1,y-1)+del(x-1,y+1);
    
  }
  else return 0;
  
}
int solve(int x,int y){
  if(x==0 && y == h)return 0;
  else if(x==w){
    return solve(0,y+1);
  }
  else if(c[x][y]==1){
    del(x,y);
    return solve(x+1,y)+1;
  }
  else return solve(x+1,y);
}
int main(){
  while(cin>>w>>h&&w&&h){
    for(int i=0;i<h;i++){
      for(int j=0;j<w;j++){
	cin >> c[i][j];
      }
    }
    
    cout << solve(0,0) << endl;
  }
}

C++: #include "bits/stdc++.h"

using namespace std;

void dfs(int i, int j, const vector<vector<int> >& c, vector<vector<int> >& arrived, int count) {
    arrived[i][j] = count;

    if (c[i + 1][j] == 1 && arrived[i + 1][j] == 0) {
        dfs(i + 1, j, c, arrived, count);
    }
    if (c[i - 1][j] == 1 && arrived[i - 1][j] == 0) {
        dfs(i - 1, j, c, arrived, count);
    }
    if (c[i][j + 1] == 1 && arrived[i][j + 1] == 0) {
        dfs(i, j + 1, c, arrived, count);
    }
    if (c[i][j - 1] == 1 && arrived[i][j - 1] == 0) {
        dfs(i, j - 1, c, arrived, count);
    }
    if (c[i + 1][j + 1] == 1 && arrived[i + 1][j + 1] == 0) {
        dfs(i + 1, j + 1, c, arrived, count);
    }
    if (c[i + 1][j - 1] == 1 && arrived[i + 1][j - 1] == 0) {
        dfs(i + 1, j - 1, c, arrived, count);
    }
    if (c[i - 1][j + 1] == 1 && arrived[i - 1][j + 1] == 0) {
        dfs(i - 1, j + 1, c, arrived, count);
    }
    if (c[i - 1][j - 1] == 1 && arrived[i - 1][j - 1] == 0) {
        dfs(i - 1, j - 1, c, arrived, count);
    }
}

void Main() {
    while (true) {
        int w, h;
        cin >> w >> h;
        if (w == 0 && h == 0) {
            break;
        }

        vector<vector<int> > c(h + 2, vector<int>(w + 2, 0));
        for (int i = 0; i < h; ++i) {
            for (int j = 0; j < w; ++j) {
                int ct;
                cin >> ct;
                c[i + 1][j + 1] = ct;
            }
        }

        vector<vector<int> > arrived(h + 2, vector<int>(w + 2, 0));
        int count = 0;
        for (int i = 0; i < c.size(); ++i) {
            for (int j = 0; j < c[i].size(); ++j) {
                if (c[i][j] == 1 && arrived[i][j] == 0) {
                    ++count;
                    dfs(i, j, c, arrived, count);
                }
            }
        }

        set<int> ans;
        for (int i = 0; i < c.size(); ++i) {
            for (int j = 0; j < c[i].size(); ++j) {
                if (arrived[i][j] > 0) {
                    ans.insert(arrived[i][j]);
                }
            }
        }
        cout << ans.size() << endl;
    }
}

int main() {
    std::cout << std::fixed << std::setprecision(15);
    Main();
    return 0;
}