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;
int w,h;
int c[55][55];
int dx[8]={1,1,1,-1,-1,-1,0,0};
int dy[8]={1,-1,0,1,-1,0,1,-1};
void dfs(int i,int j){
  c[j][i]=0;
  for(int k=0;k<8;k++){
    int nx = i + dx[k], ny = j + dy[k];
    if(nx < 0 || ny < 0 || nx >= w || ny >= h) continue;
    if(c[ny][nx]==1) dfs(nx,ny);
  }
}
int main(){
  while(1){
    cin>>w>>h;
    if(w==0 && h==0) break;
    int cnt=0;
    for(int i=0;i<h;i++){
      for(int j=0;j<w;j++){
	cin>>c[i][j];
      }
    }
    for(int i=0;i<h;i++){
      for(int j=0;j<w;j++){
	if(c[i][j]==1){
	  cnt++;
	  dfs(j,i);
	}
      }
    }

    cout<<cnt<<endl;
  }

  return 0;

    
      
}

C++: #include <cstdio>
#include <queue>
#include <map>
using namespace std;
typedef pair<int,int> P;

int w, h;
int c[50][50];
queue<P> que;

void dfs(){
	P p = que.front(); que.pop();
	for(int i = 0; i < 3; i++){
		for(int j = 0; j < 3; j++){
			if(c[p.first+i-1][p.second+j-1]){
				c[p.first+i-1][p.second+j-1] = false;
				que.push(P(p.first+i-1,p.second+j-1));
				dfs();
			}
		}
	}
}

int main(){
	int w, h;
	while(scanf("%d%d", &w, &h),w||h){
		int ans = 0;
		for(int i = 0; i < h; i++){
			for(int j = 0; j < w; j++){
				scanf("%d", &c[j][i]);
			}
		}
		for(int i = 0; i < w; i++){
			for(int j = 0; j < h; j++){
				if(c[i][j]){
					c[i][j] = false;
					ans++;
					que.push(P(i,j));
					dfs();
				}
			}
		}
		printf("%d\n", ans);
	}
}

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

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

void fillmap(int i,int j,int s){
     if(map[i][j]!=0)return;
     map[i][j]=s;
     for(int x=i-1;x<i+2;x++){
	  for(int y=j-1;y<j+2;y++){
	       if(!(y>=W||y<0||x>=H||x<0)) fillmap(x,y,s);
	  }
     }
}
    
int main(){
     while(cin>>W>>H,W||H){
	  for(int i=0;i<H;i++){
	       for(int j=0;j<W;j++){
		    int temp;
		    cin>>temp;
		    if(temp)map[i][j]=0;
		    else map[i][j]=-1;
	       }
	  }
	  int s=0;
	  for(int i=0;i<H;i++){
	       for(int j=0;j<W;j++){
		    if(map[i][j]==0){
			 fillmap(i,j,++s);
		    }
	       }
	  }
	  cout<<s<<endl;
     }
}

C++: #include <iostream>
#include <string>
#include <vector>
#include <algorithm>
#include <cmath>
#include <stdio.h>
#include <queue>
#include <stack>
#include <climits>
#include <map>
#include <set>

using namespace std;

typedef long long ll;
typedef pair<int, int> P;

const ll mod = 1000000007;
const int inf = 1e9;
const long long INF = 1LL << 60;

int ans;
int w,h;

const int w_max = 60,h_max = 60;
int dx[8] = {-1,-1,-1,0,0,1,1,1};
int dy[8] = {-1,0,1,-1,1,-1,0,1};
int v[h_max][w_max];
int f[h_max][w_max];


void dfs(int y,int x)
{
    if(v[y][x]){
        return ;
    }else{
        v[y][x] = true;
        for(int i = 0; i < 8; i++){
            int ny = y + dy[i];
            int nx = x + dx[i];
            if(ny >= 0 && ny < h && nx >= 0 && nx < w && f[ny][nx] != 0 ){
                dfs(ny,nx);
            }
        }
    }
}

int main()
{
    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 >> f[i][j];
                v[i][j] = false;
            }
        }
        ans = 0;
        for(int i = 0; i < h; i++){
            for(int j = 0; j < w; j++){
                if(f[i][j] == 1){
                    if(!v[i][j]){
                        ans++;
                        dfs(i,j);
                    }
                }
            }
        }
        cout << ans << endl;
    }
}

C++: #include <cstdio>
#include <iostream>
#include <algorithm>
#include <cstdlib>
#include <cmath>
#include <vector>
#include <ctime>
#include <string>
#include <map>
#include <stack>
#include <queue>

using namespace std;

int w, h;
const int MAX = 50;
int c[MAX][MAX];


void check(int x, int y){
	c[x][y] = 0;
    for(int dx = -1; dx <= 1; dx++){
        for(int dy = -1; dy <= 1; dy++){
            if(x + dx >= 0 && x + dx <= h  && y + dy >= 0 && y + dy <= w && c[x + dx][y + dy] == 1){
            	check(x + dx, y + dy);
            } 
        }
    }
	
}

int main(void){

	for(int i = 0; i < MAX ; i++){
		for(int j = 0; j < MAX; j++){
			c[i][j] = 0;
		}
	}

	cin >> w >> h;
	while(w != 0 && h != 0){
		int ans = 0;
		for(int i = 0; i < h ; i++){
			for(int j = 0; j < w; j++){
				cin >> c[i][j];
			}
		}

		for(int i = 0; i < h ; i++){
			for(int j = 0; j < w; j++){
				if(c[i][j] == 1){
					check(i, j);
					ans++;
				}
			}
		}
		cout << ans << endl;

		cin >> w >> h;
	}
	
    return 0;
}

C++: #include<iostream>

using namespace std;

const int MAX = 50;
int data[MAX][MAX];
int w,h;

int x[8] = {1,0,-1,0,1,-1,-1,1};
int y[8] = {0,1,0,-1,1,-1,1,-1};

void dfs(int i, int j){
  data[i][j] = 0;
  for(int k = 0; k < 8; k++){
    int ni = i+y[k], nj = j+x[k];
    if(ni < 0 || ni>= h || nj < 0 || nj>= w) continue;
    if(data[ni][nj]) dfs(ni,nj);
  }
}

int main(){

  while(cin >> w >> h && w+h){
    int ans = 0;
    for(int i = 0; i < h; i++)
      for(int j = 0; j < w; j++) cin >> data[i][j];

    for(int i = 0; i < h; i++){
      for(int j = 0; j < w; j++){
	if(data[i][j]){
	  dfs(i,j);
	  ans++;
	}
      }
    }
    cout << ans << endl;
  }
  return 0;
}