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>

using namespace std;

const int maxWH = 52;
int H, W;
int C[maxWH][maxWH] = {0};

void dfs(int hh, int ww) {
  int dh[] = {-1, 0, 1, 0, -1, -1, 1, 1};
  int dw[] = {0, -1, 0, 1, -1, 1, -1, 1};
  //cout<<hh<<" "<<ww<<endl;
  if(0<=hh && 0<=ww && hh<H && ww<W) {
    if(C[hh][ww] == 1) {
      C[hh][ww] = 0; // false
      /*
      cout<<"now "<<hh<<" "<<ww<<endl;
      for(int i=0; i<8; i++) {
        cout<<hh+dh[i]<<" "<<ww+dw[i]<<endl;
      }
      */
      for(int i=0; i<8; i++) {
        dfs(hh+dh[i], ww+dw[i]);
      }
    }
  }
}

int main() {
  while(cin>>W>>H && !(W==0 && H==0)) {
    for(int i=0; i<H; i++) {
      for(int j=0; j<W; j++) {
	cin>>C[i][j];
      }
    }

    /*
    cout<<endl;
    for(int i=0; i<H; i++) {
      for(int j=0; j<W; j++) {
	cout<<C[i][j]<<" ";
      }
      cout<<endl;
    }
    */
    
    int ans = 0;
    for(int i=0; i<H; i++) {
      for(int j=0; j<W; j++) {
	if(C[i][j] == 1) {
	  ans++;
	  dfs(i,j);
	}
      }
    }
    cout<<ans<<endl;
  }
}

C++: #include <bits/stdc++.h>
#ifdef _DEBUG
#include "debug.hpp"
#else
#define debug(...)
#endif
#define REP(i, m, n) for(int (i) = (m); (i) < (n); ++i)
#define rep(i, n) REP(i, 0, n)
#define rrep(i, n) for(int (i) = (n)-1; (i) >= 0; --i)
#define all(x) (x).begin(), (x).end()
#define FI first
#define SE second
using namespace std;
typedef long long ll;
typedef pair<int, int> P;
const int INF = 1e9+7;
const ll LINF = 1LL<<60;
template <class T, class U>
inline bool chmax(T &a, const U &b) {
  if(a < b) {
    a = b;
    return true;
  }
  return false;
}
template <class T, class U>
inline bool chmin(T &a, const U &b) {
  if(a > b) {
    a = b;
    return true;
  }
  return false;
}

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

int h, w;
vector<vector<bool>> vis;
vector<vector<int>> c;

void dfs(int y, int x) {
  if(vis[y][x]) return;
  vis[y][x] = true;
  rep(i,8) {
    int nx = x+dx[i];
    int ny = y+dy[i];
    if(nx < 0 || w <= nx || ny < 0 || h <= ny || !c[ny][nx]) continue;
    dfs(ny, nx);
  }
}

int main() {
  cin.tie(0); ios::sync_with_stdio(false);
  while(true) {
    cin >> w >> h;
    if(w == 0 && h == 0) break;
    c.assign(h, vector<int>(w));
    rep(i,h) rep(j,w) cin >> c[i][j];
    vis.assign(h, vector<bool>(w, false));
    int ans = 0;
    rep(i,h) rep(j,w) if(c[i][j] && !vis[i][j]) {
      ans++;
      dfs(i, j);
    }
    cout << ans << '\n';
  }
  return 0;
}

C++: #include <iostream>
#include <algorithm>
#include <stdlib.h>
#include <string>
#include <queue>
#include <map>
#include <stack>
using namespace std;
 
int dx[8]={1,-1,0,0,1,1,-1,-1};
int dy[8]={0,0,1,-1,-1,1,-1,1};
 
 
int main() {
int yoko,tate,px,py;
while(cin>>yoko>>tate &&yoko){
 
    int m[55][55]={0};
 
string st;
//cin>>yoko>>tate;
for(int i=1;i<tate+1;i++){
for(int j=1;j<yoko+1;j++){
cin>>m[i][j];
}
}
int ans=0;

for(int i=0;i<tate+1;i++){
for(int j=0;j<yoko+1;j++){
if (m[i][j]==0) continue;  

stack<pair<int,int> > s;
s.push(make_pair(i,j));
m[i][j]=0;
//cout<<"pxpy "<<px<<" "<<py<<endl;
while(s.empty()==false){
px=s.top().first;
py=s.top().second;
s.pop();
 
for(int i=0;i<8;i++){
//cout<<px+dx[i]<<" "<<py+dy[i]<<endl;
    if(0<px+dx[i] && px+dx[i]<tate+1 && py+dy[i]>0 && py+dy[i]<yoko+1){
     
        if(m[px+dx[i]][py+dy[i]]==1){
            m[px+dx[i]][py+dy[i]]=0;
            s.push(make_pair(px+dx[i],py+dy[i]));
            
//cout<<px+dx[i]<<" "<<py+dy[i]<<" "<<ans<<endl;
        }
 
    }}
}
 ans++;
}}
cout<<ans<<endl;
 
}
 
 
 
    return 0;
}

C++: #include <bits/stdc++.h>
#include<iostream>
#include <string>
#include <cstring>
using namespace std;
const int dy[8] = {1, 1, 1, 0, -1, -1, -1, 0};
const int dx[8] = {-1, 0, 1, 1, 1, 0, -1, -1};

int H, W;
vector<vector<bool>>seen(50, vector<bool>(50));
vector<vector<int>>field(50, vector<int>(50, 0));

void dfs(int h, int w) {
    if (seen[h][w]) {
        return;
    }

    seen[h][w] = true;
    for (int dir=0; dir < 8; dir++) {
        int nh = h + dy[dir];
        int nw = w + dx[dir];
        if (nh < 0 || nw < 0 || nh >= H || nw >= W) {
            continue;
        }

        if (field[nh][nw] == 0) {
            continue;
        }

        dfs(nh, nw);
    }
}

int main() {
    while(true){
    cin >> W >> H;
    if (W == 0 && H == 0) { break; }

    for (int i = 0; i < H; i++)
    {
        for (int j = 0; j < W; j++)
        {
            seen.at(i).at(j) = 0;
            cin >> field.at(i).at(j);
        }
    }

    int island_count = 0;
    for (int i = 0; i < H; i++)
    {
        for (int j = 0; j < W; j++)
        {
            if (seen[i][j]) {
                continue;
            }

            if (field[i][j] == 0) {
                continue;
            }

            dfs(i, j);
            island_count += 1;
        }
    }

    cout << island_count << endl;
    }
    return 0;
}

C++: #include <bits/stdc++.h>
using namespace std;
typedef pair<int, int> p;

int field[55][55];
int w, h;
int dx[] = {0, 1, 0, -1, 1, -1, -1, 1};
int dy[] = {1, 0, -1, 0, 1, 1, -1, -1};

void dfs(int x, int y){
  field[x][y] = 0;
  for(int i = 0; i < 8; i++){
    int nx = x + dx[i];
    int ny = y + dy[i];
    if(nx < 0 || nx > h) continue;
    if(ny < 0 || ny > w) continue;
    if(field[nx][ny] == 1) dfs(nx, ny);
  }
}

int main(){

  while(cin >> w >> h, w|h){
    for(int i = 0; i < h; i++){
      for(int j = 0; j < w; j++){
        cin >> field[i][j];
      }
    }
    int sum = 0;
    for(int i = 0; i < h; i++){
      for(int j = 0; j < w; j++){
        if(field[i][j] == 1){
          sum++;
          dfs(i, j);
        }
      }
    }
    cout << sum << endl;
  }
  return 0;
}