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 <string>
#include <vector>
#include <algorithm>
#include <stdio.h>
#include <cmath>
#include <map>
#include <queue>
using namespace std;
#define rep(i, n) for (int i = 0; i < (int)(n); i++)

int W,H;
vector<vector<int>> C;
void dfs(int v, int w) {
  C[v][w] = 0;
  for(int dh = -1; dh <= 1; dh++) {
    for(int dw = -1; dw <= 1; dw++) {
      if(v+dh < 0 || v+dh >= H || w+dw < 0 || w+dw >= W) {continue;}
      if(C[v+dh][w+dw] == 0) {continue;}
      dfs(v+dh, w+dw);
    }
  }
}


int main() {

  while(cin >> W >> H) {
    if(W == 0 && H == 0) {break;}
    C.assign(H, vector<int>(W, 0));
    rep(i,H) {
      rep(j, W) {
        //int a;
        cin >> C[i][j];
      }
    }
    int count = 0;
    rep(i,H) {
      rep(j, W) {
        if(C[i][j] == 0) {continue;}
        dfs(i,j);
        count++;
      }
    }
    cout << count << endl;
  }
}

C++: #include "bits/stdc++.h"
using namespace std;
#define rep(i,a,n) for(ll i=a;i<n;i++)
#define ALL(s) s.begin(),s.end()
#define P pair<int,int>
#define print(n) cout<<n<<endl
const int M=100010;
const int MOD=1000000007;
const int INF=1000000007;
using ll=long long;


int w,h;
char c[55][55];
int ddx[8]={-1,-1,-1,0,0,1,1,1};
int ddy[8]={-1,0,1,-1,1,-1,0,1};
int dx[4]={-1,0,1,0},dy[4]={0,-1,0,1};
bool used[55][55]={};
void dfs(int W,int H){
	if(W<0||W>=w||H<0||H>=h||used[W][H])return;
	used[W][H]=true;
	if(c[W][H]=='1'){
		rep(i,0,8)dfs(W+ddx[i],H+ddy[i]);
	}
}
int main(){
	while(1){
		int land=0;
		cin>>w>>h;
		if(w==0)break;
		rep(j,0,h){
			rep(i,0,w){
				cin>>c[i][j];
				used[i][j]=false;
			}
		}
		rep(j,0,h){
			rep(i,0,w){
				if(used[i][j]==false&&c[i][j]=='1'){
					dfs(i,j);
					land++;
				}
			}
		}
		print(land);
	}
	return 0;
}

C++: #include<iostream>
#include<map>
#include<vector>
#include<algorithm>
#include<cmath>
#include<climits>
#include<ctime>
#include<cstring>

#define ALL(v) (v).begin(),(v).end()
#define REP(i,p,n) for(int i=p;i<(int)(n);++i)
#define rep(i,n) REP(i,0,n)
#define DUMP(list) cout << "{ "; for(auto nth : list){ cout << nth << " "; } cout << "}" << endl;

using namespace std;

int W,H;

bool sima[52][52];
bool checkmap[52][52];

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


int saiki(int x, int y) 
{
	if(sima[x][y]==1){sima[x][y]=0;}
	else if(sima[x][y]==0){return 0;}

	rep(i,9){
		saiki(x+dx[i], y+dy[i]);
	}

	return 1;
}

int main() {

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

		rep(h,H) {
			rep(w,W) {
				sima[w][h] = 0;
				checkmap[w][h] = 0;
			}
		}

		rep(h,H) {
			rep(w,W) {
				cin >> sima[w+1][h+1];
			}
		}

		REP(h,1,H+1){
			REP(w,1,W+1){
				if(sima[w][h]==1){
					saiki(w,h);
					//cout << w << ":" << h << endl; 
					count++;	
				}
			}
		}
/*
	cout << "map" << endl; 

		rep(h,51){
			rep(w,51){
				cout << sima[w][h] << " ";
			}
			cout << endl;
		}


	
	cout << "checkmap" << endl; 

		rep(h,51){
			rep(w,51){
				cout << checkmap[w][h] << " ";
			}
			cout << endl;
		}
*/

		cout << count << endl;
	}

	return 0;
}

C++: #include <iostream>
#include <algorithm>
#include <vector>
using namespace std;

vector<int> route;
#define P 50
#define Q 50
char a[P+2][Q+2];


void search(int place1,int place2){
    a[place1][place2] = '0';
    if(a[place1][place2-1] == '1'){//左
        route.push_back(1);
        search(place1,place2-1);
    }
    else if(a[place1+1][place2] == '1'){//下
        route.push_back(2);
        search(place1+1,place2);
    }
    else if(a[place1][place2+1] == '1'){//右
        route.push_back(3);
        search(place1,place2+1);
    }
    else if(a[place1-1][place2] == '1'){//上
        route.push_back(4);
        search(place1-1,place2);
    }
    else if(a[place1+1][place2-1] == '1'){//左下
        route.push_back(5);
        search(place1+1,place2-1);
    }
    else if(a[place1+1][place2+1] == '1'){//右下
        route.push_back(6);
        search(place1,place2+1);
    }
    else if(a[place1-1][place2+1] == '1'){//右上
        route.push_back(7);
        search(place1-1,place2);
    }
    else if(a[place1-1][place2-1] == '1'){//左上
        route.push_back(8);
        search(place1-1,place2);
    }
    else{
        int bk = route.back();
        route.pop_back();//戻る
        if(bk == 0) return;
        else if(bk == 1)search(place1,place2+1);
        else if(bk == 2)search(place1-1,place2);
        else if(bk == 3)search(place1,place2-1);
        else if(bk == 4)search(place1+1,place2);
        else if(bk == 5)search(place1-1,place2+1);
        else if(bk == 6)search(place1-1,place2-1);
        else if(bk == 7)search(place1+1,place2-1);
        else if(bk == 8)search(place1+1,place2+1);
    }
}

int main(void){
    int ct;
    int x,y;
    while(1){
        ct = 0;
        cin >> y >> x;
        if(x == 0 & y == 0) break;
        for(int i = 0;i < x+2;i++){
            for(int j = 0;j < y+2;j++){
                if(i == 0||j == 0||i == x+1||j == y+1)a[i][j] = '0';
                else cin >> a[i][j];
                
            }
            
        }
        for(int i = 1;i < x+1;i++){
            for(int j = 1;j < y+1;j++){
                if(a[i][j] == '1'){
                    route.push_back(0);
                    search(i,j);
                    ct++;
                }
            }
        }
        cout << ct << endl;
    }
    return 0;
}