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
//------------------------------------------
#include <vector>
#include <list>
#include <map>
#include <set>
#include <queue>
#include <stack>
#include <bitset>
#include <algorithm>
#include <functional>
#include <numeric>
#include <utility>
#include <sstream>
#include <iostream>
#include <iomanip>
#include <cstdio>
#include <cmath>
#include <cstdlib>
#include <cctype>
#include <string>
#include <cstring>
#include <ctime>
#include <fstream>
#include <complex>
#include <math.h>

using namespace std;

//conversion
//------------------------------------------
inline long toInt(string s) {int v; istringstream sin(s);sin>>v;return v;}
inline long toLong(string s) {long v; istringstream sin(s);sin>>v;return v;}
template<class T> inline string toString(T x) {ostringstream sout;sout<<x;return sout.str();}

//math
//-------------------------------------------
template<class T> inline T sqr(T x) {return x*x;}

//typedef
//------------------------------------------
typedef vector<int> VI;
typedef vector<VI> VVI;
typedef vector<string> VS;
typedef pair<int, int> PII;
typedef pair<long, long> PLL;
typedef long long LL;
//container util
//------------------------------------------
#define ALL(a)  (a).begin(),(a).end()
#define RALL(a) (a).rbegin(), (a).rend()
#define PB push_back
#define MP make_pair
#define SZ(a) int((a).size())
#define EACH(i,c) for(typeof((c).begin()) i=(c).begin(); i!=(c).end(); ++i)
#define EXIST(s,e) ((s).find(e)!=(s).end())
#define SORT(c) sort((c).begin(),(c).end())

//repetition
//------------------------------------------
#define FOR(i,a,b) for(int i=(a);i<(b);++i)
#define REP(i,n)  FOR(i,0,n)

//constant
//--------------------------------------------

//clear memory
#define CLR(a) memset((a), 0 ,sizeof(a))

const long double EPS = 1e-12;
const int INF = 10000;

char mp[52][52];

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

bool bfs(int s,int t){
	if(flag[s][t])return false;
	int sum=0;
	queue<PII>pi;
	pi.push(MP(s,t));
	flag[s][t]=true;
	while(!pi.empty()){
		PII p=pi.front();pi.pop();
		sum++;
		REP(i,8){
			int y=p.first+dy[i],x=p.second+dx[i];
			if(mp[y][x]=='1'&&!flag[y][x]){
				flag[y][x]=true;
				pi.push(MP(y,x));
			}
		}
	}
	return true;
}
int main(){
	int W,H;
	while(cin>>W>>H){
		if(W==0&&H==0)break;
		REP(i,52)REP(j,52){
			mp[i][j]='0';
		}
		CLR(flag);
		int y,x;
		REP(i,H)REP(j,W){
			cin>>mp[i+1][j+1];
		}
		int sum=0;
		REP(i,H)REP(j,W){
			if(mp[i+1][j+1]=='1'&&bfs(i+1,j+1))sum++;
		}
		//cout<<"!"<<y<<" "<<x<<endl;
		cout<<sum<<endl;
	}
	return 0;
}

C++: #include <iostream>
using namespace std;
int mp[51][51];
void init(){
  for(int i = 0; 51 > i; i++){
    for(int j = 0; 51 > j; j++){
      mp[i][j] == 0;
    }
  }
}
int tansa(int h,int w, int nh, int nw){
  if(mp[nh][nw] == 1)mp[nh][nw] = 0;
  if(nh != 0 && nw != 0 && mp[nh-1][nw-1] == 1)tansa(h,w,nh-1,nw-1);
  if(           nw != 0 && mp[nh  ][nw-1] == 1)tansa(h,w,nh  ,nw-1);
  if(nh!=h-1 && nw != 0 && mp[nh+1][nw-1] == 1)tansa(h,w,nh+1,nw-1);
  if(nh != 0 &&            mp[nh-1][nw  ] == 1)tansa(h,w,nh-1,nw  );
  if(nh!=h-1 &&            mp[nh+1][nw  ] == 1)tansa(h,w,nh+1,nw  );
  if(nh != 0 && nw!=w-1 && mp[nh-1][nw+1] == 1)tansa(h,w,nh-1,nw+1);
  if(           nw!=w-1 && mp[nh  ][nw+1] == 1)tansa(h,w,nh  ,nw+1);
  if(nh!=h-1 && nw!=w-1 && mp[nh+1][nw+1] == 1)tansa(h,w,nh+1,nw+1);
  return 0;
}
int main(){
  while(true){
    int w,h;cin>>w>>h;
    if(w == 0)break;
    for(int i = 0; h > i; i++){
      for(int j = 0; w > j; j++){
        cin >> mp[i][j];
      }
    }
    int ret = 0;
    for(int i = 0; h > i; i++){
      for(int j = 0; w > j; j++){
        if(mp[i][j] == 1){
          tansa(h,w,i,j);
          ret++;
        }
      }
    }
    cout << ret << endl;
    init();
  }
}

C++: #include <iostream>
#include <vector>
#include <array>
#include <list>
#include <queue>
#include <set>
#include <map>
#include <unordered_map>
#include <unordered_set>
#include <utility>
#include <string>
#include <sstream>
#include <algorithm>
#include <random>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <ctime>
#include <cmath>
#include <cassert>
#include <climits>
#include <thread>

using namespace std;

void add_one(queue<pair<int, int>>& dst, int c_h, int c_w, int h, int w)
{
	if (c_h < 0 || h <= c_h || c_w < 0 || w <= c_w) return;

	dst.push(make_pair(c_h, c_w));
}

void solve_one(vector<vector<bool>>& map, int h, int w, int s_h, int s_w)
{
	queue<pair<int,int>> checks;
	checks.push(make_pair(s_h, s_w));

	while (!checks.empty()) {
		auto cur = checks.front();
		checks.pop();

		if (!map[cur.first][cur.second]) continue;

		map[cur.first][cur.second] = false;

		add_one(checks, cur.first + 1, cur.second    , h, w);
		add_one(checks, cur.first - 1, cur.second    , h, w);
		add_one(checks, cur.first    , cur.second + 1, h, w);
		add_one(checks, cur.first    , cur.second - 1, h, w);
		add_one(checks, cur.first + 1, cur.second + 1, h, w);
		add_one(checks, cur.first + 1, cur.second - 1, h, w);
		add_one(checks, cur.first - 1, cur.second + 1, h, w);
		add_one(checks, cur.first - 1, cur.second - 1, h, w);
	}
}

int solve(vector<vector<bool>>& map, int h, int w)
{
	int count = 0;

	int s_h = 0;
	int s_w = 0;

	while (s_h < h && s_w < w) {
		if (map[s_h][s_w]) {
			solve_one(map, h, w, s_h, s_w);
			count++;
		}

		s_w++;
		if (s_w == w) {
			s_h++;
			s_w = 0;
		}
	}

	return count;
}

int main()
{
	while (true) {
		int h, w; cin >> w >> h;
		if (h == 0 && w == 0) return 0;

		vector<vector<bool>> map(h, vector<bool>(w));
		for (int i = 0; i < h; i++) {
			for (int j = 0; j < w; j++) {
				int s; cin >> s;
				if (s == 0) map[i][j] = false;
				if (s == 1) map[i][j] = true;
			}
		}

		int count = solve(map, h, w);
		cout << count << endl;
	}

	return 0;
}