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 <vector>

void dfs(std::vector<std::vector<int> >& map, std::vector<std::vector<int> >& visited, int& island, int x, int y, int w, int h)
{
	visited.at(x).at(y) = island;
	int dx[8] = {1, 1, 0, -1, -1, 0, 1, -1};
	int dy[8] = {1, 0, 1, -1, 0, -1, -1, 1};
	for (int i = 0; i < 8; i++)
	{
		int nex = dx[i] + x;
		int ney = dy[i] + y;
		if (nex < 0 || nex >= w || ney < 0 || ney >= h)
			continue;
		if (map.at(nex).at(ney) == 1 && visited.at(nex).at(ney) != island)
		{	
			dfs(map, visited, island, nex, ney, w, h);
		}
	}
}


int main()
{
	std::vector<int> I;
	while (true)
	{
		int w, h;
		std::cin >> w >> h;
		if (w == 0 && h == 0)
			break;
		std::vector<std::vector<int> > map(h, std::vector<int> (w));
		std::vector<std::vector<int> > visited(h, std::vector<int> (w, 0));
		for (int i = 0; i < h; i++)
		{
			for (int j = 0; j < w; j++)
			{
				std::cin >> map.at(i).at(j);
			}
		}
		int island = 1;
		for (int i = 0; i < h; i++)
		{
			for (int j = 0; j < w; j++)
			{
				if (map.at(i).at(j) == 1 && visited.at(i).at(j) == 0)
				{
					dfs(map, visited, island, i, j, h, w);
					++island;	
				}
			}
		}
		I.push_back(island - 1);
	}
	for (std::vector<int>::iterator i = I.begin(); i != I.end(); i++)
		std::cout << *i << '\n';

	return 0;
}

C++: #include <bits/stdc++.h>
using namespace std;
const int size = 12;
//const int size = 14;
char a[size][size];
int cnt = 0;
void dfs(int x, int y){
    //現在地を０にする
    a[x][y] = 0;
    //4近傍を探索
    //x+1, y
    int nx = x+1, ny=y;
    if(0 <= nx and nx < 12 and 0 <= ny and ny < 12 and a[nx][ny] == '1') dfs(nx, ny);
    //x-1, y
    nx = x-1, ny = y;
    if(0 <= nx and nx < 12 and 0 <= ny and ny < 12 and a[nx][ny] == '1') dfs(nx, ny);
    //x, y+1
    nx = x, ny = y+1;
    if(0 <= nx and nx < 12 and 0 <= ny and ny < 12 and a[nx][ny] == '1') dfs(nx, ny);
    //x, y-1
    nx = x, ny = y - 1;
    if(0 <= nx and nx < 12 and 0 <= ny and ny < 12 and a[nx][ny] == '1') dfs(nx, ny);
    return;
}

int main(){
    while (1) {
    cnt = 0;
    //マップを入力
    for(int i=0; i < 12; i++){
        for(int j=0; j < 12; j++) if(!(cin >> a[i][j])) return 0;
    }


    //サーチ
    for(int i=0; i < 12; i++){
        for(int j=0; j < 12; j++){
            if(a[i][j] == '1'){
                cnt += 1;
                dfs(i, j);
            }
        }
    }

    cout << cnt << endl;
}
    return 0;
}

C++: #include<iostream>
#include<vector>
#include <unordered_map>
#include <algorithm>
#include <functional>
#include <map>
#include <string>
#include <sstream>
#include <cmath>
#include <float.h>
using namespace std;



bool checked[50][50];
int island[50][50];
int count1 = 0;
int w, h;
vector<int> num;
unsigned long long int mod = 1000000007;

void checkIsland(int x, int y) {
    if (x < 0 || x >= h || y >= w || y < 0) { return; }
    if (!island[x][y] || checked[x][y]) {
        checked[x][y] = true;
        return;
    }
    checked[x][y] = true;
    checkIsland(x + 1, y);
    checkIsland(x + 1, y + 1);
    checkIsland(x + 1, y - 1);
    checkIsland(x, y + 1);
}

void runtime() {
    count1 = 0;
    for (int i = 0; i < w; ++i) {
        for (int j = 0; j < h; ++j) {
            checked[i][j] = false;
            cin >> island[i][j];
        }
    }
    for (int i = 0; i < w; ++i) {
        for (int j = 0; j < h; ++j) {
            if (checked[i][j]) continue;
            if (island[i][j]) {
                count1++;
                checkIsland(i, j);
            }
            else {
                checked[i][j] = true;
            }
        }
    }
    num.push_back(count1);
}


int main() {
    cin >> w >> h;
    while (!(w == 0 && h == 0)) {
        runtime();
        cin >> w >> h;
    }
    for (auto i : num) {
        cout << i << endl;
    }
    return 0;
}

C++: #include <bits/stdc++.h>
using namespace std;
using ll=long long;
typedef pair<int,int> P;
template<class T> inline bool chmax(T& a, T b) { if (a < b) { a = b; return 1; } return 0; }
template<class T> inline bool chmin(T& a, T b) { if (a > b) { a = b; return 1; } return 0; }
int main(){
    int w,h;
    cin>>w>>h;
    vector<vector<int>>G(h,vector<int>(w));
    queue<P> Q;
    for(int i=0;i<h;i++){
        for(int j=0;j<w;j++){
            cin>>G.at(i).at(j);
        }
    }
    int count=0,x,y,nx,ny;
    P p;
    for(int i=0;i<h;i++){
        for(int j=0;j<w;j++){
            if(G.at(i).at(j)==1){
            Q.push(P(i,j));
            while(!Q.empty()){
        p=Q.front();
        Q.pop();
        x=p.first;
        y=p.second;
        G.at(x).at(y)=0;
            for(int i=-1;i<=1;i++){
        for(int j=-1;j<=1;j++){
            nx=x+i;
            ny=y+j;
        if(0<=nx&& nx<h && 0<=ny&& ny<w && G.at(nx).at(ny)==1) Q.push(P(nx,ny));
        }
    }
    }
    count++;
    }
    }
    }
cout<<count<<endl;
}

C++: #include <stdio.h>

int w, h;
int island[50][50], map[50][50], ans;

void dfs(int x, int y, int flg) {
		int i, j;
	if(x < 0 || w <= x || y < 0 || h <= y || map[x][y] == 1) {
		return;
	}
	else if(map[x][y] == 0) {
		if(island[y][x] == 0) {
			if(flg == 0) {
			 	map[x][y] = 1;
		
				for(i = -1; i <= 1; i ++) {
					for(j = -1; j <= 1; j ++) {
						dfs(x + i, y + j, 0);
					}
				}
			}
			else if(flg == 1) {
				return;
			}
		}
		else if(island[y][x] == 1) {
			map[x][y] = 1;  
					
			if(flg == 0) {
				ans ++; 
			}
			for(i = -1; i <= 1; i ++) {
				for(j = -1; j <= 1; j ++) {
					dfs(x + i, y + j, 1);
				}
			}
		}
	}
	return;
}

int main() {
	
	int i, j;
	while(scanf("%d %d", &w, &h)) {
		if(w == 0 && h == 0) { break; }
		
		ans = 0;
		for(i = 0; i < 50; i ++) {
			for(j = 0; j < 50; j ++) {
				map[i][j] = -1;
				island[i][j] = 0;
			}
		}
	
		for(i = 0; i < h; i ++) {
			for(j = 0; j < w; j ++) {
				scanf("%d", &island[i][j]);
				map[i][j] = 0;
			}
		}
		
		for(i = 0; i < h; i ++) {
			for(j = 0; j < w; j ++) {
				dfs(i, j, 0);
			}
		}
		printf("%d\n", ans);
	}

	return 0;
}