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++: #ifndef _GLIBCXX_NO_ASSERT
#include <cassert>
#endif
#include <cctype>
#include <cerrno>
#include <cfloat>
#include <ciso646>
#include <climits>
#include <clocale>
#include <cmath>
#include <csetjmp>
#include <csignal>
#include <cstdarg>
#include <cstddef>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <ctime>

#if __cplusplus >= 201103L
#include <ccomplex>
#include <cfenv>
#include <cinttypes>
#include <cstdbool>
#include <cstdint>
#include <ctgmath>
#include <cwchar>
#include <cwctype>
#endif

// C++
#include <algorithm>
#include <bitset>
#include <complex>
#include <deque>
#include <exception>
#include <fstream>
#include <functional>
#include <iomanip>
#include <ios>
#include <iosfwd>
#include <iostream>
#include <istream>
#include <iterator>
#include <limits>
#include <list>
#include <locale>
#include <map>
#include <memory>
#include <new>
#include <numeric>
#include <ostream>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <stdexcept>
#include <streambuf>
#include <string>
#include <typeinfo>
#include <utility>
#include <valarray>
#include <vector>

#if __cplusplus >= 201103L
#include <array>
#include <atomic>
#include <chrono>
#include <condition_variable>
#include <forward_list>
#include <future>
#include <initializer_list>
#include <mutex>
#include <random>
#include <ratio>
#include <regex>
#include <scoped_allocator>
#include <system_error>
#include <thread>
#include <tuple>
#include <typeindex>
#include <type_traits>
#include <unordered_map>
#include <unordered_set>
#endif




#define int long long
#define rng(i,l,r) for(size_t i=(l);i<(r);++i)
#define rep(i,n) rng(i,0,n)
#define gnr(i,l,r) for(size_t i=(r)-1;i>=(l);i--)
#define per(i,b) gnr(i,0,b)
#define ALL(obj) (obj).begin(), (obj).end()   //1,2,3,...
#define rALL(obj) (obj).rbegin(), (obj).rend()//...,3,2,1

using namespace std;


//constant
const int MOD = 1e9+7;
const int INF = 1e18;
const int dx4[4] = {1, 0, -1, 0};
const int dy4[4] = {0, 1, 0, -1};
const int dx8[8] = {1,1,0,-1,-1,-1,0,1};
const int dy8[8] = {0,1,1,1,0,-1,-1,-1};
const double pi = acos(-1);


template<class T> inline bool chmin(T& a, T b) { if (a > b) { a = b; return true; } return false; }
template<class T> inline bool chmax(T& a, T b) { if (a < b) { a = b; return true; } return false; }

int gcd(int a,int b){return b?gcd(b,a%b):a;}
//*max_element(ALL(vector))
//count(ALL(vector),num) -> n
//memset(obj,value,sizeof(obj));
int H,W;


void dfs(int h,int w,int map[60][60],int seen[60][60]){
    
    if(seen[h][w]) return;
    
    seen[h][w] = true;
    
    for (int i = 0; i < 8; i++) {
        int nh = h+dy8[i];
        int nw = w+dx8[i];
        bool ishok = (0<=nh && nh<H);
        bool iswok = (0<=nw && nw<W);
        bool island= (map[nh][nw]);
        
        if(seen[nh][nw]) continue;
        if(!(ishok&&iswok&&island)) continue;
        
        dfs(nh,nw,map,seen);
        
    }
}

void solve(){
    
    // remove the bottom 3 lines when you submit this code.
    std::ifstream in("sample.txt");
    std::cin.rdbuf(in.rdbuf());

    cin.tie(0);
    ios::sync_with_stdio(false);
    
    
    while(cin >> W >> H){
        if(W==0&&H==0) break;
        int map[60][60]; memset(map,0,sizeof(map));
        int seen[60][60];memset(seen,false,sizeof(seen));
        for (int h = 0; h < H; h++) {for (int w = 0; w < W; w++) cin >> map[h][w];}
        
        int ans = 0;
        for (int h = 0; h < H; h++) {
            for (int w = 0; w < W; w++) {
                if(!map[h][w] || seen[h][w]) continue;
                dfs(h,w,map,seen);
                ans++;
            }
        }
        
        cout << ans << endl;
        
        
    }
    
}





signed main() {
    solve();
    return 0;
}

C++: #include <bits/stdc++.h>
#define INF 1000000000
using namespace std;

vector<pair<int, int>> bfs(vector<pair<int, int>> not_yet, vector<vector<int>> maze)
{
  pair<int, int> check = not_yet.at(0);
  queue<pair<int, int>> a;
  a.push(check);
  maze.at(check.first).at(check.second) = 0;
  while (!a.empty())
  {
    pair<int, int> now = a.front();
    a.pop();
    not_yet.erase(find(not_yet.begin(), not_yet.end(), now));
    vector<int> move = {1, 0, -1};
    for (int i = 0; i < 3; ++i)
    {
      for (int j = 0; j < 3; ++j)
      {
        if (i == 1 && j == 1)
        {
          continue;
        }
        if (maze.at(now.first + move.at(i)).at(now.second + move.at(j)) == 1)
        {
          a.push({now.first + move.at(i), now.second + move.at(j)});
          maze.at(now.first + move.at(i)).at(now.second + move.at(j)) = 0;
        }
      }
    }
  }
  return not_yet;
}

int main()
{
  while (true)
  {
    int w, h;
    cin >> w >> h;
    if (w == h && w == 0)
    {
      break;
    }
    vector<vector<int>> maze(h + 2, vector<int>(w + 2, -1));
    vector<pair<int, int>> not_yet(0);// h w
    for (int i = 1; i < h + 1; ++i)
    {
      for (int j = 1; j < w + 1; ++j)
      {
        int point;
        cin >> point;
        maze.at(i).at(j) = point;
        if (point == 1)
        {
          not_yet.push_back({i, j});
        }
      }
    }

    int ans = 0;
    while (not_yet.size() != 0)
    {
      not_yet = bfs(not_yet, maze);
      ans += 1;
    }
    cout << ans << endl;
  }
}