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;
using ull = unsigned long long;
using st = string;
using db = double;
using vll = vector<long long>;
using vb = vector<bool>;
using vvll = vector<vll>;
using vst = vector<st>;
using vchar = vector<char>;
#define rep(i, m, n) for (int i = (int)(m); i < (int)(n); i++)
#define vrep(i, vec) for(auto& i : vec)
#define vin(vec) for(auto& i : vec) cin >> i
#define all(v) v.begin(), v.end()
ll mod = 1000000007;

ll w, h;
vvll graph;

void dfs(vvll &graph, ll x, ll y){
  graph[y][x] = 2;
  rep(dx, -1, 2) rep(dy, -1, 2)if(x + dx >= 0 && x + dx < w && y + dy >= 0 && y + dy < h)if(graph[y + dy][x + dx] == 1) dfs(graph, x + dx, y + dy);
}

int main(){
  while(true){
  cin >> w >> h;
  if(w == 0 && h == 0) break;
  graph.assign(h, vll(w));
  rep(y, 0, h)rep(x, 0, w) cin >> graph[y][x];
  ll ans = 0;
  rep(y, 0, h)rep(x, 0, w){
    if(graph[y][x] == 1){
      dfs(graph, x, y);
      ans++;
    }
  }
  cout << ans << endl;}
}

C++: #include <stdio.h>

int W,H,ans;
int map[100][100];

void search_island(int x, int y){
	map[x][y] = 0;
	if(x > 0){
		if(map[x - 1][y]){
			search_island(x - 1, y);
		}
	}
	if(x < H - 1){
		if(map[x + 1][y]){
			search_island(x + 1, y);
		}
	}
	if(y > 0){
		if(map[x][y - 1]){
			search_island(x, y - 1);
		}
	}
	if(y < W - 1){
		if(map[x][y + 1]){
			search_island(x, y + 1);
		}
	}
}


int main()
{
	scanf("%d %d",&W,&H);
	while(W != 0 && H != 0){
		for(int i = 0; i < H; i++){
			for(int k = 0; k < W; k++){
				scanf("%d",&map[i][k]);
			}
		}

		ans = 0;

		for(int j = 0; j < H; j++){
			for(int k = 0; k < W; k++){
				if(map[j][k]){
					ans++;
					search_island(j,k);
				}
			}
		}

		printf("%d\n",ans);
		scanf("%d %d",&W,&H);
	}
	return 0;
}

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

typedef std::int_fast32_t  s32;
typedef std::uint_fast32_t u32;
typedef std::int_fast64_t  s64;
typedef std::uint_fast64_t u64;

typedef std::pair<int, int> P;

int map[64][64];
int H, W;

void dfs_erase(int x, int y) {
  if( not ( 0 <= x and x < H and 0 <= y and y < W ) ) return;
  if( not ( map[x][y] == 1 ) ) return;

  map[x][y] = 0;
  
  int dx[8] = {1, 1, 1, 0, 0, -1, -1, -1};
  int dy[8] = {1, 0, -1, 1, -1, 1, 0, -1};
  for(int i = 0; i < 8; ++i) {
    dfs_erase(x + dx[i], y + dy[i]);
  }
  return;
}
  
int main(void) {

  for(;;) {
    std::cin >> W >> H;
    if( W == 0 and H == 0 ) break;

    for(int i = 0; i < H; ++i) {
      for(int j = 0; j < W; ++j) {
        std::cin >> map[i][j];
      }
    }

    int res = 0;
    for(int i = 0; i < H; ++i) {
      for(int j = 0; j < W; ++j) {
        if( map[i][j] == 1 ) {
          dfs_erase(i, j);
          res += 1;
        }
      }
    }

    std::cout << res << std::endl;
  }
  
  return 0;
}

C++: #include<stdio.h>
void printmap();
void draw(int x,int y);
int w,h,c[50][50];

int main(){
	int i,j;
	int count;


	scanf("%d %d",&w,&h);


	while((w!=0)&&(h!=0)){
		count=0;
	
		for(j=0;j<h;j++){
			for(i=0;i<w;i++){
				scanf("%d",&c[j][i]);
			}
		}
		//printf("\n");
		//printmap();
	
		for(i=0;i<w;i++){
			c[h][i]=0;
		}
		for(i=0;i<h;i++){
			c[i][w]=0;
		}

		for(i=0;i<w;i++){
			for(j=0;j<h;j++){
				if(c[j][i]==1){
					count++;
					//printf("count up count=%d\n",count);
					draw(j,i);
				}
			}
		}

		printf("%d\n",count);
		scanf("%d %d",&w,&h);
	}
	return 0;
}

void draw(int x,int y){
	//printf("delete %d %d\n",x,y);
	c[x][y]=0;
	if(c[x+1][y]==1){
		draw(x+1,y);
	}else if(c[x-1][y]==1){
		draw(x-1,y);
	}else if(c[x][y-1]==1){
		draw(x,y-1);
	}else if(c[x][y+1]==1){
		draw(x,y+1);
	}

}

void printmap(){
	int i,j;
	for(j=0;j<h;j++){
		for(i=0;i<w;i++){
			printf("%d ",c[j][i]);
		}
		printf("\n");
	}
}