Go: package main

import (
	"bufio"
	"fmt"
	"io"
	"math"
	"os"
	"strconv"
)

var (
	readString func() string
	readBytes  func() []byte
	stdout     *bufio.Writer
)

func init() {
	readString, readBytes = newReadString(os.Stdin)
	stdout = bufio.NewWriter(os.Stdout)
}

func newReadString(ior io.Reader) (func() string, func() []byte) {
	r := bufio.NewScanner(ior)
	r.Buffer(make([]byte, 1024), math.MaxInt32)
	r.Split(bufio.ScanWords)

	f1 := func() string {
		if !r.Scan() {
			panic("Scan failed")
		}
		return r.Text()
	}
	f2 := func() []byte {
		return []byte(f1())
	}
	return f1, f2
}

func readInt() int {
	return int(readInt64())
}

func readInt64() int64 {
	i, err := strconv.ParseInt(readString(), 10, 64)
	if err != nil {
		panic(err.Error())
	}
	return i
}

func readFloat64() float64 {
	f, err := strconv.ParseFloat(readString(), 64)
	if err != nil {
		panic(err.Error())
	}
	return f
}

func printf(f string, args ...interface{}) (int, error) {
	return fmt.Fprintf(stdout, f, args...)
}

func println(args ...interface{}) (int, error) {
	return fmt.Fprintln(stdout, args...)
}

func eprintln(args ...interface{}) (int, error) {
	return fmt.Fprintln(os.Stderr, args...)
}

func eprintf(f string, args ...interface{}) (int, error) {
	return fmt.Fprintf(os.Stderr, f, args...)
}

func min(a, b int) int {
	if a < b {
		return a
	}
	return b
}

func min64(a, b int64) int64 {
	if a < b {
		return a
	}
	return b
}

func max(a, b int) int {
	if a < b {
		return b
	}
	return a
}

func max64(a, b int64) int64 {
	if a < b {
		return b
	}
	return a
}

func abs(a int) int {
	if a < 0 {
		return -a
	}
	return a
}

func abs64(a int64) int64 {
	if a < 0 {
		return -a
	}
	return a
}

func gcd(a, b int) int {
	if b == 0 {
		return a
	}
	return gcd(b, a%b)
}

func gcd64(a, b int64) int64 {
	if b == 0 {
		return a
	}
	return gcd64(b, a%b)
}

// readString() string
// readInt() int
// readInt64() int64
// readFloat64() float64

// -----------------------------------------------------------------------------

func main() {
	defer stdout.Flush()

	for {
		ans, ok := solve()
		if !ok {
			break
		}
		println(ans)
	}

}

func solve() (int, bool) {
	w := readInt()
	h := readInt()
	if w == 0 && h == 0 {
		return 0, false
	}

	c := make([][]int, h+2)
	c[0] = make([]int, w+2)
	c[h+1] = make([]int, w+2)
	for i := 1; i-1 < h; i++ {
		c[i] = make([]int, w+2)
		for j := 1; j-1 < w; j++ {
			c[i][j] = readInt()
		}
	}

	var ans int
	for i := 1; i-1 < h; i++ {
		for j := 1; j-1 < w; j++ {
			if 0 < c[i][j] {
				ans++
				dfs(c, i, j)
			}
		}
	}
	return ans, true
}

func dfs(c [][]int, y, x int) {
	c[y][x] = 0
	dx := []int{-1, 0, 1, -1, 1, -1, 0, 1}
	dy := []int{-1, -1, -1, 0, 0, 1, 1, 1}
	for i := range dx {
		nx := x + dx[i]
		ny := y + dy[i]
		if 0 < c[ny][nx] {
			dfs(c, ny, nx)
		}
	}
}

// -----------------------------------------------------------------------------

C++: #include<iostream>
#include<algorithm>
#include<vector>
#include<string>
#include<utility>
#include<map>
#include<set>
#include<queue>
using namespace std;
#define N (1000000000+7)
typedef pair<int,int> P;
typedef long long ll;

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

void dfs(int i,int j){
    if(check(i,j)){
        board[i][j]=-1;
        for(int x=i-1;x<=i+1;++x){
            for(int y=j-1;y<=j+1;++y){
                dfs(x,y);
            }
        }
    }
    else return;
}

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

    return 0;
}

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

vector<int> xw = {-1,  0,  1, -1,  1, -1,  0,  1 };
vector<int> yh = {-1, -1, -1,  0,  0,  1,  1,  1 };
void dfs( vector<vector<int>> &vec, int X, int Y, int &w, int &h, vector<vector<bool>> &ck ){
  if ( ck.at(Y).at(X) ) return;
  
  ck.at(Y).at(X) = true;
  for (int i=0; i<8; i++) {
    int x = X + xw.at(i);
    int y = Y + yh.at(i);
    if( x < 0 || w <= x ) continue;
    if( y < 0 || h <= y ) continue;
    if ( vec.at(y).at(x) ) dfs( vec, x, y, w, h, ck);
  }
}

int main() {
  
  while (true) {
  int W,H;
  cin >> W >> H;
  if( W == 0 || H == 0) break;
    
  vector<vector<int>> P(H, vector<int>(W));
  for (int i=0; i<H; i++) {
    for (int j=0; j<W; j++) {
      cin >> P.at(i).at(j);  
    }
  }
  
  vector<vector<bool>> checked(H, vector<bool>(W, false));
  int res = 0;
  for (int h=0; h<H; h++) {
    for (int w=0; w<W; w++) {
      if( !checked.at(h).at(w) && P.at(h).at(w) ) {
         res++; dfs( P, w, h, W, H, checked);
      }
    }
  }
  
  cout << res << endl;
    
  }
  return 0;
}

C++: #include <iostream>
#include <cstdlib>
#include <queue>
#include <math.h>
#include <vector>
#include <algorithm>
#include <string>
#include <bitset>
using namespace std;

vector<vector<int>> c;
int w, h;

void search(int i, int j){
    if(i-1 >= 0 && j-1 >= 0 && c[i-1][j-1] == 1){
        c[i-1][j-1] = 0;
        search(i-1, j-1);
    }
    if(j-1 >= 0 && c[i][j-1] == 1){
        c[i][j-1] = 0;
        search(i, j-1);
    }
    if(i+1 < h && j-1 >= 0 && c[i+1][j-1] == 1){
        c[i+1][j-1] = 0;
        search(i+1, j-1);
    }
    if(i-1 >= 0 && c[i-1][j] == 1){
        c[i-1][j] = 0;
        search(i-1, j);
    }
    if(i+1 < h && c[i+1][j] == 1){
        c[i+1][j] = 0;
        search(i+1, j);
    }
    if(i-1 >= 0 && j+1 < w && c[i-1][j+1] == 1){
        c[i-1][j+1] = 0;
        search(i-1, j+1);
    }
    if(j+1 < w && c[i][j+1] == 1){
        c[i][j+1] = 0;
        search(i, j+1);
    }
    if(i+1 < h && j+1 < w && c[i+1][j+1] == 1){
        c[i+1][j+1] = 0;
        search(i+1, j+1);
    }
    return;
}

int main()
{
    int ans;
    while(true){
        cin >> w >> h;
        if(w == 0 && h == 0){
            break;
        }
        // init
        ans = 0;
        c.resize(h);
        for(int i=0; i<h; ++i){
            c[i].resize(w);
            for(int j=0; j<w; ++j){
                cin >> c[i][j];
            }
        }
        // search
        for(int i=0; i<h; ++i){
            for(int j=0; j<w; ++j){
                if(c[i][j] == 1){
                    ans++;
                    c[i][j] = 0;
                    search(i, j);
                }
            }
        }
        // output
        cout << ans << endl;
        // clear
        for(int i=0; i<h; ++i){
            c[i].clear();
        }
        c.clear();
    }
    return 0;
}

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

int field[50+2][50+2];
int dx[8]={0,1,1,1,0,-1,-1,-1};
int dy[8]={-1,-1,0,1,1,1,0,-1};

int rec(int x, int y)
{
  int i;

  if(field[x][y]!=1) return 0;
  field[x][y] = 0;
  for(i=0;i<8;i++) rec(x+dx[i],y+dy[i]);
  return 1;
}

int main()
{
  int w,h;
  int i,j;
  int n;
  while(cin>>w>>h,w|h){
    for(j=1;j<=h;j++)
      for(i=1;i<=w;i++)
	cin>>field[i][j];
    for(j=1;j<=h+1;j++)
      field[w+1][j]=0;
    for(i=1;i<=w+1;i++)
      field[i][h+1]=0;

    n = 0;
    for(j=1;j<=h;j++) for(i=1;i<=w;i++) n += rec(i,j);

    cout<<n<<endl;
  }
}