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>
using namespace std;

int islands=0;
int w,h;
int field[49][49];
void bfs(int, int);

int main(){

  while(cin>>w>>h&&w!=0&&h!=0){

    for(int i=0;i<50;i++){
      for(int j=0;j<50;j++){
        field[i][j] = 0;
      }
    }

    for(int j=0;j<h;j++){
      for(int i=0;i<w;i++){
        cin >> field[i][j];
//        cout << field[i][j] << " ";
      }
  //    cout << endl;
    }
   // cout << endl;
    int ans=0; 

    for(int i=0;i<w;i++){
      for(int j=0;j<h;j++){
        if(field[i][j]==1){
          field[i][j]=0;
          bfs(i,j);
          ans++;
        }
      }
    }
    /*   if(w==5&&h==4){
         for(int i=0;i<w;i++){
         for(int j=0;j<h;j++){
         cout << field[i][j] << " ";
         }
         cout << endl;
         }
         }*/
    cout << ans << endl;
  }

  return 0;
}

void bfs(int x, int y){
  if(x-1>=0&&field[x-1][y]==1){
    field[x-1][y]=0;
    bfs(x-1,y);
  }
  if(x+1<w&&field[x+1][y]==1){
    field[x+1][y]=0;
    bfs(x+1,y);
  }
  if(y-1>=0&&field[x][y-1]==1){
    field[x][y-1]=0;
    bfs(x,y-1);
  }
  if(y+1<h&&field[x][y+1]==1){
    field[x][y+1]=0;
    bfs(x, y+1);
  }
  if(x-1>=0&&y-1>=0&&field[x-1][y-1]==1){
    field[x-1][y-1]=0;
    bfs(x-1,y-1);
  }
  if(x+1<w&&y-1>=0&&field[x+1][y-1]==1){
    field[x+1][y-1]=0;
    bfs(x+1,y-1);
  }
  if(x-1>=0&&y+1<h&&field[x-1][y+1]==1){
    field[x-1][y+1]=0;
    bfs(x-1,y+1);
  }
  if(x+1<w&&y+1<h&&field[x+1][y+1]==1){
    field[x+1][y+1]=0;
    bfs(x+1,y+1);
  }
  return;
}

C++: #include <bits/stdc++.h>

#define ll long long

using namespace std;

int w,h;

int f[100][100]={};
int cf[100][100] = {};



int main(){

    while(1){
        scanf("%d %d",&w,&h);
        if(w == 0 && h==0)break;

        for(int i=0;i<h;i++){
            for(int j=0;j<w;j++){
                scanf("%d",&f[i][j]);
            }
        }
        int ans = 0;
        for(int i=0;i<h;i++){
            for(int j=0;j<w;j++){
                
                if(f[i][j] == 1 && cf[i][j]==0){
                    ans++;
                }
                for(int k=-1;k<2;k++){
                    if(i+k<0 || h <= i+k)continue;
                    for(int t=-1;t<2;t++){
                        if(j+t<0 || w <= j+t)continue;
                        if(f[i][j]==1&&f[i+k][j+t]==1 && cf[i+k][j+t]==0){
                            cf[i+k][j+t]=1;
                        }
                    }
                }
            }
        }
        cout << ans << endl;
    }

}

C++: #include <iostream>
#include <math.h>
#include <algorithm>
#include <vector>
using std::cin;
using std::cout;
using std::endl;
using std::string;
using std::vector;
				
				
vector<vector<int>> del_island(int i,int j,vector<vector<int>> v){	
	v[i][j] = 0;
	vector<vector<int>> answer = v;
	if(v[i-1][j] == 1){
		answer = del_island(i-1,j,answer);
	}
	if(v[i+1][j] == 1){
		answer = del_island(i+1,j,answer);
	}
	if(v[i][j-1] == 1){
		answer = del_island(i,j-1,answer);
	}
	if(v[i][j+1] == 1){
		answer = del_island(i,j+1,answer);
	}
	if(v[i-1][j-1] == 1){
		answer = del_island(i-1,j-1,answer);
	}
	if(v[i-1][j+1] == 1){
		answer = del_island(i-1,j+1,answer);
	}
	if(v[i+1][j-1] == 1){
		answer = del_island(i+1,j-1,answer);
	}
	if(v[i+1][j+1] == 1){
		answer = del_island(i+1,j+1,answer);
	}
	return answer;
}


int count_island(int w, int h,vector<vector<int>> v){
	int count = 0;
	for(int i = 1 ; i <= h ;i++){
		for(int j = 1 ;j <= w ;j++){
			//cout << v[i][j] ;
			if(v[i][j] == 1){
				count++;
				//cout << "hello" <<endl;
				v = del_island(i,j,v);
			}
		}
	}
	return count;
}
	
int main(){ 
	int w,h;
	vector<vector<int>> v(60,vector<int>(60,0));
	int count;

	while(1){
		cin >> w >> h;
		if(w == 0 && h == 0){
			break;
		}
		count = 0; 
		for(int i = 0; i < 60;i++){
			for( int j = 0; j < 60;j++){
				v[i][j] = 0;
			}
		}

		for(int i = 1;i <= h; i++){
			for(int j = 1; j <= w; j++){
				cin >> v[i][j];
			}
		}

		count = count_island(w,h,v);


		cout << count << endl;
	}
}

C++: #include<iostream>
#include<queue>
#include<cstring>
using namespace std;
int x[90][90], col[90][90], h, w, c, i, j, k, l, p, q;
queue<int>Q, R;
int main() {
	while (true) {
		c = 0;
		memset(col, 0, sizeof(col));
		cin >> w >> h;
		if (h == 0) { break; }
		for (i = 0; i < h; i++) { for (j = 0; j < w; j++) { cin >> x[i][j]; } }
		for (i = 0; i < 50; i++) {
			for (j = 0; j < 50; j++) {
				if (x[i][j] == 1 && col[i][j] == 0) {
					c++;
					col[i][j] = c;
					Q.push(i * 100 + j);
					while (!Q.empty()) {
						while (!Q.empty()) {
							p = Q.front() / 100;
							q = Q.front() % 100;
							for (k = -1; k < 2; k++) {
								for (l = -1; l < 2; l++) {
									if (x[p + k][q + l] == 1 && col[p + k][q + l] == 0) {
										col[p + k][q + l] = c;
										R.push((p + k) * 100 + q + l);
									}
								}
							}
							Q.pop();
						}
						while (!R.empty()) {
							Q.push(R.front());
							R.pop();
						}
					}
				}
			}
		}
		cout << c << endl;
	}
}

C++: #include <algorithm>
#include <cstdio>
#include <iostream>
#include <map>
#include <cmath>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <vector>
#include <stdlib.h>
#include <stdio.h>
#include <cstring>
using namespace std;
#define FOR(I,A,B) for(int I = (A); I < (B); ++I)
#define CLR(mat) memset(mat, 0, sizeof(mat))
typedef long long ll;
int W, H;
int c[50][50];
int vy[8] = {-1,-1,0,1,1,1,0,-1};
int vx[8] = {0,1,1,1,0,-1,-1,-1};
void calc(int y, int x) {
  if(y < 0 || y >= H || x < 0 || x >= W) return;
  if(c[y][x] == 0) return;
  visited[y][x] = 0;
  FOR(i,0,8) calc(y+vy[i],x+vx[i]);
  return;
}
int main()
{
  while(cin >> W >> H, (W || H)) {
    CLR(visited);
    FOR(y,0,H) FOR(x,0,W) cin >> c[y][x];
    int ans = 0;
    FOR(y,0,H) {
      FOR(x,0,W) {
        if(c[y][x] == 0) continue;
        calc(y, x);
        ans++;
      }
    }
    cout << ans << endl;
  }
  return 0;
}