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>
#include <string>
#include <vector>
#define rep(i,n) for (int i = 0;i < (n); ++i)
using namespace std;
using ll = long long;
using P = pair<int,int>;


//AOJ How many islands?
//http://judge.u-aizu.ac.jp/onlinejudge/description.jsp?id=1160&lang=jp

int dx[8] = {1,-1,0,0,1,1,-1,-1};
int dy[8] = {0,0,1,-1,1,-1,1,-1};
int h,w;
using Graph = vector<vector<int>>;
vector<vector<bool>> seen;
vector<vector<int>> maze;

void dfs(P p){
    seen[p.first][p.second] = true;

    for(int i = 0;i < 8;i++){
        int nx = p.second + dx[i];
        int ny = p.first + dy[i];
        if(nx < 0 || ny < 0 || h <= ny || w <= nx) continue;
        if(seen[ny][nx]) continue;
        if(maze[ny][nx] == 0) continue;

        dfs(make_pair(ny,nx));
    }
}

void recur(int w,int h) {
    int count = 0;
    maze.assign(h,vector<int>(w,0));
    seen.assign(h,vector<bool>(w,false));
    for(int i = 0;i < h;i++){
        for(int j = 0;j < w;j++){
            cin >> maze[i][j];
        }
    }

    for(int i = 0;i < h;i++){
        for(int j = 0;j < w;j++){
            if(maze[i][j]==0) continue;
            if(seen[i][j]) continue;
            dfs(make_pair(i,j));
            count++;
        }
    }
    
    cout << count << endl;
    w = 0;
    h = 0;
}

int main(){
    while(true){
        cin >> w >> h;
        if(w*w + h*h == 0) break;
        recur(w,h);
    }
    return 0;
}



// #include <bits/stdc++.h>
// #include <string>
// #include <vector>
// #define rep(i,n) for (int i = 0;i < (n); ++i)
// using namespace std;
// using ll = long long;
// using P = pair<int,int>;


// //AOJ How many islands?
// //http://judge.u-aizu.ac.jp/onlinejudge/description.jsp?id=1160&lang=jp

// int dx[8] = {1,-1,0,0,1,1,-1,-1};
// int dy[8] = {0,0,1,-1,1,-1,1,-1};
// int h,w;
// using Graph = vector<vector<int>>;
// vector<vector<bool>> seen;
// vector<vector<int>> maze;

// void dfs(P p){
//     seen[p.first][p.second] = true;

//     for(int i = 0;i < 8;i++){
//         int nx = p.second + dx[i];
//         int ny = p.first + dy[i];
//         if(nx < 0 || ny < 0 || h <= ny || w <= nx) continue;
//         if(seen[ny][nx]) continue;
//         if(maze[ny][nx] == 0) continue;

//         dfs(make_pair(ny,nx));
//     }
// }

// int main() {
//     cin >> w >> h;
//     int count = 0;
//     maze.assign(h,vector<int>(w,0));
//     seen.assign(h,vector<bool>(w,false));
//     for(int i = 0;i < h;i++){
//         for(int j = 0;j < w;j++){
//             cin >> maze[i][j];
//         }
//     }

//     for(int i = 0;i < h;i++){
//         for(int j = 0;j < w;j++){
//             if(maze[i][j]==0) continue;
//             if(seen[i][j]) continue;
//             dfs(make_pair(i,j));
//             count++;
//         }
//     }
    
//     cout << count << endl;
// }

C++: #include <bits/stdc++.h>
#define rep(i,a,b) for (int64_t i = (a); i < (b); i++)
#define REP(i,n) rep(i,0,n)
#define ALL(a) begin(a),end(a)
#define sz(s) (s).size()
#define pb push_back
#define mp make_pair
#define fi first
#define se second
using namespace std;
typedef long long ll;
int dx[]={1,-1,0,0,1,-1,1,-1};
int dy[]={0,0,1,-1,1,-1,-1,1};

int w,h,ans=0;
bool c[55][55];

void bfs(int y,int x)
{
    c[y][x]=0;
    REP(i,8) 
    {
        int ny=y+dy[i];int nx=x+dx[i];
        if(c[ny][nx]&&ny>=0&&ny<h&&nx>=0&&nx<w)bfs(ny,nx);
    }
}

int main()
{

    while(cin>>w>>h,w>0)
    {
        REP(i,h)REP(j,w)cin>>c[i][j];
        ans=0;c[55][55]={};
        REP(i,h)REP(j,w)if(c[i][j]){bfs(i,j);ans++;}
        cout<<ans<<endl;
    }
}

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<bits/stdc++.h>
using namespace std;

//  四方向へのベクトル
const int dx[8] = {-1,0,1,0,1,1,-1,-1};
const int dy[8] = {0,-1,0,1,1,-1,1,-1};

int H,W;
//探索用
bool seen[501][5001];
int MAX = 0;
//using Graph = vector<vector<int>>;
bool roop;
int dfs(const vector<vector<int>> &G,int nh,int nw){
    seen[nh][nw] = true;
    
    for(int i=0;i<8;i++){
        int next_h = nh + dy[i];//8方向へ移動
        int next_w = nw + dx[i];

        if(next_h<0 || next_h >=H ||next_w<0||next_w >= W) continue;
        if(seen[next_h][next_w]) continue;
        if(G[next_h][next_w] == 0) continue;

        dfs(G,next_h,next_w);
    }

}
int main(){
    while(true){
        int cnt=0;
        cin >> W >> H;
        if(H+W == 0) break;

        vector<vector<int>> G(H);
        for(int i=0;i<H;i++){
            for(int k=0;k<W;k++){
                int a;
                cin >> a;
                G[i].push_back(a);
            }
        }
        memset(seen,0,sizeof(seen));

        for(int i=0;i<H;i++){
            for(int k=0;k<W;k++){
                if(G[i][k] == 1&& !seen[i][k]){
                    dfs(G,i,k);
                    cnt++;
                }
            }
        }

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