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 <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 w,h;
int nx,ny;
void dfs(vector<vector<int>> &G,int a,int b){
    G.at(a).at(b)=0;
    
    for(int k=-1;k<=1;k++){
        for(int s=-1;s<=1;s++){
            nx=a+k;
            ny=b+s;
        if(0<=nx&& nx<h && 0<=ny&& ny<w && G.at(nx).at(ny)==1) dfs(G,nx,ny);
        }
    }
    return ;
}



int main(){
    int w,h;
    cin>>w>>h;
    vector<vector<int>>G(h,vector<int>(w));
    for(int i=0;i<h;i++){
        for(int j=0;j<w;j++){
            cin>>G.at(i).at(j);
        }
    }
    int count=0;
    for(int i=0;i<h;i++){
        for(int j=0;j<w;j++){
            if(G.at(i).at(j)==1){
                dfs(G,i,j);
                count++;
            }
        }
    }
cout<<count<<endl;
}

C++: #include<iostream>

using namespace std;

void riku(int,int);
int w,h;
int map[51][51],num;

int main(){
	//テ」ツつ?」ツ?」テ」ツ?アテ」ツδ催」ツδε」ツδ暗」ツ?ォテ」ツつづ」ツ?セテ」ツ?ィテ」ツつづ」ツ?ォテヲツ崢クテ」ツ??」ツ?ヲテ」ツ??」ツつ凝、ツコツコテ」ツ?ッテ」ツ??」ツ??
	//テァツ可ケテ」ツ?ォテ・ツ、ツ嘉」ツ?ェテヲツゥツ淌ィツδステ」ツ?ッテ、ツスツソテ」ツ?」テ」ツ?ヲテ」ツ??」ツ?ェテ」ツ??・ツ?催・ツクツーテ」ツδ療」ツδュテ」ツつーテ」ツδゥテ」ツδ?」ツ??」ツ?」テ」ツ?淌」ツ??
	cin>>w;
	cin>>h;
	while(w!=0 && h!=0){
		//テ・ツ?・テ・ツ環崚」ツ?ィテ・ツ按敕ヲツ慊淌・ツ個?
		for(int lop=0;lop<51;lop++){
			for(int lop2=0;lop2<51;lop2++){
				map[lop][lop2]=0;
			}
		}
		for(int lop=0;lop<h;lop++){
			for(int lop2=0;lop2<w;lop2++){
				cin>>map[lop][lop2];
			}
		}
		num=0;
		
		//テ」ツ?禿」ツ?禿」ツ?凝」ツつ嘉ヲツ慊ャテゥツ。ツ?
		for(int lop=0;lop<h;lop++){
			for(int lop2=0;lop2<w;lop2++){
				if(map[lop][lop2]==1){
					riku(lop,lop2);
					num++;
				}
			}
		}
		cout<<num<<endl;
		
		cin>>w;
		cin>>h;
	}
	return 0;
}

void riku(int hh,int ww){
	//テ」ツ?禿」ツ?ョテゥツ鳴「テヲツ閉ーテ」ツつ津・ツ?催・ツクツーテ」ツ?療」ツ?ヲ
	//テ、ツクツ?」ツ?、テ」ツ?ョテゥツ卍クテ」ツ?ォテ」ツ?、テ」ツ?ェテ」ツ?古」ツ?」テ」ツ?ヲテ」ツ??」ツつ凝」ツ?ィテ」ツ?禿」ツつ催」ツつ津」ツ?、テ」ツ?カテ」ツ?療」ツ?ヲテ」ツ??」ツ??
	//テ・ツ?ィテゥツδィテヲツスツーテ」ツ?療」ツ?淌」ツつ?テ」ツ?、テ」ツ?ョテゥツ卍クテ」ツ?ィテ」ツ?療」ツ?ヲテ」ツつォテ」ツつヲテ」ツδウテ」ツδ暗」ツ?療」ツ?ヲテ」ツ??」ツつ?
	map[hh][ww]=0;
	int hhh,www;
	
	/*for(int lop=-1;lop<=1;lop++){
		for(int lop2=-1;lop2<=1;lop2++){
			hhh=hh+lop2;
			www=ww+lop;
			if(0<=www && www<w && 0<=hhh && hhh<h && map[hhh][www]==1){
				riku(hhh,www);
			}
		}
	}*/
	
	for(int lop=-1;lop<=1;lop++){
		for(int lop2=-1;lop2<=1;lop2++){
			if(hh+lop>=0 && ww+lop2>=0){//テ・ツキツヲテァツォツッorテ、ツクツ甘ァツォツッテ」ツ?ョテヲツ卍づ」ツ?ッテ」ツつケテ」ツδォテ」ツδシ
				if(hh+lop<h && ww+lop2<w){//テ・ツ渉ウテァツォツッorテ、ツクツ凝ァツォツッテ」ツ?ェテ」ツつ嘉」ツつケテ」ツδォテ」ツδシ
					if(map[hh+lop][ww+lop2]==1){
						//テゥツ堋」テヲツ篠・テ」ツ?療」ツ?ヲテ」ツ??」ツつ凝」ツδ榲」ツつケテ」ツ??テ」ツ?ョテヲツ卍?
						//テ・ツ?催・ツクツーテ」ツ?療」ツ?ヲテ」ツ?敕」ツ?ョテ」ツδ榲」ツつケテ」ツ?ョテゥツ堋」テヲツ篠・テ」ツδ榲」ツつケテ」ツつ津ィツェツソテ」ツ?ケテ」ツつ?
						riku(hh+lop,ww+lop2);
					}
				}
			}
		}
	}
	
}