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

using namespace std;

struct locate{
  int x;
  int y;
};

int graph[64][64] = {{}};
int flag[64][64] = {{}};
int w,h;

int dfs(locate );


int main(){

  int sum = 0;
  locate in;

  while(1){
  cin >> w >> h ;
  if(w == 0 && h == 0) break;
  for(int i = 0;i < h;i++){
    for(int j = 0;j < w;j++){
      cin >> graph[i][j];
    }
  }
    for(int i = 0;i < h;i++){
      for(int j = 0;j < w;j++){
	if(graph[i][j] == 1 && flag[i][j] == 0){
	  sum++;
	  in.x = j;
	  in.y = i; 
	  if(dfs(in) == -1){
	    sum-= 1;
	  }
	  //	cout << "sum = " << sum << endl;
	}
      }
    }

    cout << sum << endl;
    sum = 0;
    for(int i = 0;i < 64;i++){
      for(int j = 0;j < 64;j++){
	graph[i][j] = 0;
	flag[i][j] = 0;
      }
    }
  }
  return 0;
}



int dfs(locate in){

  stack<locate> S;
  locate u,v,re;
  int  dx,dy,a = 0;

  S.push(in);

  while(!S.empty()){
    v = S.top();S.pop();
    for(dy = -1;dy <= 1;dy++){
      if(v.y + dy < 0 || v.y + dy > 63) continue;
      for(dx = -1;dx <= 1;dx++){
	if(v.x + dx < 0 || v.x + dx > 63 || (dx == 0 && dy == 0)) continue;
	//	cout << "x = " << v.x << "y = " << v.y << endl; 
	if(graph[v.y + dy][v.x + dx] == 1 && flag[v.y + dy][v.x + dx] == 0){
	  // cout << 'a' << endl;
	  flag[v.y + dy][v.x + dx] = 1;
	  re.x = v.x + dx;
	  re.y = v.y + dy;
	  a = 1;
	  dfs(re);
	}
	/*else if(graph[v.y + dy][v.x + dx] == 1 && flag[v.y + dy][v.x + dx] == 1){
	  cout << 'b' << endl;
	  a = 0;
	  return -1;
	  }*/
      }
    }
  }
  return a;
}

C++: #include<iostream>
#include<algorithm>
#include<vector>
#include<string>
#include<utility>
#include<map>
#include<set>
#include<queue>
#include<math.h>
#include<stdio.h>
using namespace std;
#define mod (1000000000+7)
#define N (10007)
#define INF 1e16
typedef long long ll;
typedef pair<ll,ll> P;
int h,w;
int board[100][100];

bool check(int i, int j) {
	return (i >= 0) && (i < h) && (j >= 0) && (j < w) && (board[i][j] == 1);
}

void dfs(int i,int j){
	for(int dx=i-1;dx<=i+1;dx++){
		for(int dy=j-1;dy<=j+1;dy++){
			if(check(dx,dy)){
				board[dx][dy]=0;
				dfs(dx,dy);
			}
		}
	}
}

int main(void){
	while(true){
		cin>>w>>h;
		if(w==0&&h==0)break;
		for(int i=0;i<h;i++){
			for(int j=0;j<w;j++){
				cin>>board[i][j];
			}
		}
		int ans=0;
		for(int i=0;i<h;i++){
			for(int j=0;j<w;j++){
				if(board[i][j]){
					dfs(i,j);
					ans++;
				}
			}
		}
		cout<<ans<<endl;
	}
    return 0;
}

C++: #include <bits/stdc++.h>
using namespace std;

vector<vector<int>> m;
int xv[]={1,1,1,0,-1,-1,-1,0},yv[]={1,0,-1,-1,-1,0,1,1};


void bfs(int x,int y){
    stack<pair<int,int>> stk;
    m[x][y]=0;
    for(int i=0;i<8;i++){
        if(m[x+xv[i]][y+yv[i]]){
            stk.push(make_pair(x+xv[i],y+yv[i]));
        }
    }
    while(!stk.empty()){
        pair<int,int> tg=stk.top();
        stk.pop();
        bfs(tg.first,tg.second);
    }
    
}


void solve(int c,int r){
    int in,ans=0;
    m=vector<vector<int>>(r+2,vector<int>(c+2,0));
    for(int i=1;i<=r;i++){
        for(int j=1;j<=c;j++){
            cin>>in;
            m[i][j]=in;
        }
    }
    stack<pair<int,int>> stk;
    
    for(int i=1;i<=r;i++){
        for(int j=1;j<=c;j++){
            if(m[i][j]){
                ans++;
                bfs(i,j);
            }
        }
    }
    
    cout<<ans<<endl;
}

int main(void){
    int a,b;
    while(cin>>a>>b,a){
        solve(a,b);
    }
}

C++: #include <iostream>
#include <vector>

using namespace std;


int read_map(vector< vector <int> >& map,int &w, int &h){
  cin >> w >>  h;
  if (w == 0 && h == 0){
    return 0;
  }
  map.reserve(h);
  for (int i = 0; i < h ; ++i) map[i].resize(w);
  for (int i = 0; i < h; ++i)
  {
    for (int j = 0; j < w; ++j)
    {
      cin >> map[i][j];
    }
  }
  return 1;
}


bool island_check(vector<vector<int> >& map, int i, int j){
  if (map[i][j] == 1){
    return true;
  }
  return false;
}

void walk_island(vector<vector<int> >& map, int i, int j, int w, int h){
  int start_i, start_j;
  start_i = i;
  start_j = j;
  while (++j < w){
    if (island_check(map, i, j))
    {
      map[i][j] = 2;
    }else{
      break;
    }
  }
  j = start_j;
  while (--j >= 0){
    if (island_check(map, i, j))
    {
      map[i][j] = 2;
    }else{
      break;
    }
  }
  while (++i < h){
    if (island_check(map, i, j))
    {
      map[i][j] = 2;
    }else{
      break;
    }
  }
  i = start_i;
  while (--i >= 0){
    if (island_check(map, i, j))
    {
      map[i][j] = 2;
    }else{
      break;
    }
  }
  i = start_i;
  j = start_j;
  while (++i < h && ++j < w){
    if (island_check(map, i, j))
    {
      map[i][j] = 2;
    }else{
      break;
    }
  }
  i = start_i;
  j = start_j;
  while (--i >= 0 && ++j < w){
    if (island_check(map, i, j))
    {
      map[i][j] = 2;
    }else{
      break;
    }
  }
  i = start_i;
  j = start_j;
  while (--i >= 0 && --j >= 0){
    if (island_check(map, i, j))
    {
      map[i][j] = 2;
    }else{
      break;
    }
  }
  while (++i < h && --j >= 0){
    if (island_check(map, i, j))
    {
      map[i][j] = 2;
    }else{
      break;
    }
  }
}

int search_island_num(vector< vector <int> >& map, int w, int h){
  int island_count = 0;
  for (int i = 0; i < h; ++i)
  {
    for (int j = 0; j < w; ++j)
    {
      if(map[i][j] == 1){
        island_count++ ;
        map[i][j] = 2;
        walk_island(map, i, j, w, h);
      }
    }
  }
  return island_count;
}

int main(){
  vector< vector<int> > map;
  int w, h;
  while (read_map(map, w, h) != 0) {
    cout << search_island_num(map, w, h) << endl;
    map.clear();
  }
}