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 <iostream>
#include <algorithm> // min()
#include <string>
#include <vector>
#include <climits> // INT_MAX
#include <cmath> // ceil()
#define rep(i, n) for (int i = 0; i < n; ++i)

using namespace std;
using ll = long long;
const int INF = INT_MAX;
using Graph = vector<vector<int>>;
using Vi = vector<int>;
using Vpii = vector<pair<int, int>>;
using Pii = pair<int, int>;


vector<bool> seen;
int ans = 0;
int land = 0;
bool is_tree = false;
Pii region;


void dfs(Graph &g, int x, int y) {
    if (x < 0 || x >= region.first) return;
    if (y < 0 || y >= region.second) return;

    if (g[y][x] == 0) return;

    if (g[y][x] == 1) {
        g[y][x] = 0;
        land--;
    }

    dfs(g, x + 1, y);
    dfs(g, x - 1, y);
    dfs(g, x, y + 1);
    dfs(g, x, y - 1);
    dfs(g, x + 1, y + 1);
    dfs(g, x + 1, y - 1);
    dfs(g, x - 1, y + 1);
    dfs(g, x - 1, y - 1);
}


int main() {
    Pii start;
    Vi answers;
    vector<Graph> graphs;
    int w, h, c;

    while (true)
    {
        Graph graph;
        land = 0;
        ans = 0;

        cin >> w >> h;
        if (w == 0 && h == 0) break;
        region = { w, h };

        rep(i, h) {
            Vi temp;
            rep(j, w) {
                cin >> c;
                temp.emplace_back(c);
                land += c;
            }
            graph.emplace_back(temp);
        }

        while (land) {
            int x;
            rep(y, h) {
                auto vec = graph[y];
                auto iter = find(vec.begin(), vec.end(), 1);
                if (iter != vec.end()) {
                    x = distance(vec.begin(), iter);
                    start = { x, y };
                    break;
                }
            }
            dfs(graph, start.first, start.second);
            ans++;
        }

        answers.emplace_back(ans);

    }

    for (auto&& a : answers) {
        cout << a << endl;
    }

}

C++: #include <iostream>
#include <string>
#include <vector>
#include <algorithm>
#include <cmath>
#include <cstdio>
#include <functional>
#include <numeric>
#include <stack>
#include <queue>
#include <map>
#include <set>
#include <utility>
#include <sstream>
#include <complex>

using namespace std;

#define FOR(i,a,b) for(long long i=(a);i<(b);i++)
#define REP(i,N) for(long long i=0;i<(N);i++)
#define ALL(s) (s).begin(),(s).end()
#define fi first
#define se second

#define PI acos(-1.0)
#define INF 1000000007
#define EPS 1e-10

typedef long long ll;
typedef pair<ll, ll> P;
typedef pair<double, double> PD;
typedef pair<string, ll> PS;
typedef vector<ll> V;
typedef pair<P, char> PC;

int w, h;
int c[100][100];
int ans;

void solve(int y, int x){
	c[y][x] = 0;

	for (int i = -1; i <= 1; i++){
		for (int j = -1; j <= 1; j++){
			int ny = y + i, nx = x + j;
			if (ny >= 0 && ny < h&&nx >= 0 && nx < w){
				if (c[ny][nx] == 1)solve(ny, nx);
			}
		}
	}
}

int main(){
	while (cin >> w >> h&&w + h){
		REP(i, h){
			REP(j, w){
				cin >> c[i][j];
			}
		}
		ans = 0;
		REP(i, h){
			REP(j, w){
				if (c[i][j] == 1){
					solve(i, j);
					ans++;
				}
			}
		}
		cout << ans << endl;
	}
}

C++: #include <iostream>
#include <string.h>

using namespace std;
char Field[52][52];
int w, h;

void DFS(int y, int x)
{
	Field[y][x] = 0;
	for(int i = -1; i <= 1; ++i)
	{
		for(int j = -1; j <= 1; ++j)
		{
			int X = x + j;
			int Y = y + i;
			if((0 < X && X <= w) && (0 < Y && Y <= h) && Field[Y][X] == '1')
			{
				DFS(Y, X);
			}
		}
	}
}

void solve()
{
	while(cin >> w >> h, w || h)
	{
		memset(Field, 0, sizeof(Field));
		for(int i = 1; i <= h; ++i)
		{
			for(int j = 1; j <= w; ++j)
			{
				cin >> Field[i][j];
			}
		}

		int count = 0;
		for(int i = 1; i <= h; ++i)
		{
			for(int j = 1; j <= w; ++j)
			{
				if(Field[i][j] == '1')
				{
					count++;
					DFS(i, j);
				}
			}
		}
		cout << count << endl;
	}
}

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

C++: #include <bits/stdc++.h>
using namespace std;
#include <bits/stdc++.h>
using namespace std;
struct Graph{
  using ver_t=int32_t; using edge_t=pair<ver_t,ver_t>;
  vector<vector<ver_t>> field;
  const edge_t dr[8]={{1,1},{1,0},{1,-1},{0,1},{0,-1},{-1,1},{-1,0},{-1,-1}};
  void dfs(edge_t frm){
    // preorder
    field[frm.first][frm.second]=0;
    for(const auto& v:dr){
      auto x=frm.first+v.first; auto y=frm.second+v.second;
      if(x>=0&&x<field.size()&&y>=0&&y<field.front().size()&&field[x][y]==1){
        dfs(make_pair(x,y));
      }
    }
    // postorder
  }
  // void bfs(ver_t frm){
  //   seen[frm]=0;
  //   deque<ver_t> que(1,frm); // dfs,bfs
  //   while(!que.empty()){
  //     auto u=que.front(); que.pop_front(); // bfs
  //     for(const auto& v:edge[u]){
  //       if(seen[v]==NO){
  //         seen[v]=seen[u]+1;
  //         que.push_back(v); // dfs,bfs
  //       }
  //     }
  //   }
  // }
};
int32_t main(){
  Graph g;
  // size_t n; cin>>n;
  // for(size_t i=0; i<n; ++i){
  size_t w,h; cin>>w>>h;
  while(w!=0||h!=0){
    g.field.resize(h);
    for(auto&& x:g.field){
      x.resize(w);
      for(auto&& y:x) cin>>y;
    }
    size_t ret=0;
    for(size_t i=0; i<h; ++i){
      for(size_t j=0; j<w; ++j){
        if(g.field[i][j]==1){ ++ret; g.dfs(make_pair(i,j)); }
      }
    }
    cout<<ret<<'\n';
    cin>>w>>h;
  }
}