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 <vector>
#include <queue>
using namespace std;
int main() {
  int w, h, s, t, x, y, ans;
  queue<vector<int>> que;
  while (cin >> w >> h, w) {
    ans = 0;
    vector< vector<int> > m(h+2, vector<int>(w+2, 0));
    for (int i = 0; i < h; ++i) {
      for (int j = 0; j < w; ++j) {
	cin >> m[i+1][j+1];
      }
    }
    s = 0; t = 0;
    while (s != h+1 || t != w+1) {
      if (!m[s][t]) {
	if (t < w+1) ++t;
	else {
	  t = 0;
	  ++s;
	}
      } else {
	que.push({s, t});
	m[s][t] = 0;
	while (!que.empty()) {
	  x = que.front()[0];
	  y = que.front()[1];
	  que.pop();
	  if (m[x-1][y]) {
	    m[x-1][y] = 0;
	    que.push({x-1, y});
	  }
	  if (m[x+1][y]) {
	    m[x+1][y] = 0;
	    que.push({x+1, y});
	  }
	  if (m[x][y-1]) {
	    m[x][y-1] = 0;
	    que.push({x, y-1});
	  }
	  if (m[x][y+1]) {
	    m[x][y+1] = 0;
	    que.push({x, y+1});
	  }
	  if (m[x+1][y+1]) {
	    m[x+1][y+1] = 0;
	    que.push({x+1, y+1});
	  }
	  if (m[x+1][y-1]) {
	    m[x+1][y-1] = 0;
	    que.push({x+1, y-1});
	  }
	  if (m[x-1][y+1]) {
	    m[x-1][y+1] = 0;
	    que.push({x-1, y+1});
	  }
	  if (m[x-1][y-1]) {
	    m[x-1][y-1] = 0;
	    que.push({x-1, y-1});
	  }
	}
	++ans;
      }
    }
    cout << ans << endl;
    m.erase(m.begin(), m.end());
  }
  return 0;
}

C++: #include <vector>
#include <list>
#include <map>
#include <set>
#include <deque>
#include <stack>
#include <bitset>
#include <algorithm>
#include <functional>
#include <numeric>
#include <utility>
#include <sstream>
#include <queue>
#include <iostream>
#include <iomanip>
#include <cstdio>
#include <cmath>
#include <cstdlib>
#include <cctype>
#include <string>
#include <cstring>
#include <ctime>
#include <complex>
#include <fstream>
#include <stdio.h>

using namespace std;

//conversion
//------------------------------------------
inline int toInt(string s) {int v; istringstream sin(s);sin>>v;return v;}
template<class T> inline string toString(T x) {ostringstream sout;sout<<x;return sout.str();}

//typedef
//------------------------------------------
typedef vector<int> VI;
typedef vector<VI> VVI;
typedef vector<double> VEC;
typedef vector<VEC> MAT;
typedef vector<string> VS;
typedef pair<int, int> PII;
typedef pair<int, PII> TIII;
typedef long long LL;
typedef vector<LL> VLL;

//container util

//------------------------------------------
#define ALL(a)  (a).begin(),(a).end()
#define RALL(a) (a).rbegin(), (a).rend()
#define PB push_back
#define MP make_pair
#define SZ(a) int((a).size())
#define EACH(i,c) for(typeof((c).begin()) i=(c).begin(); i!=(c).end(); ++i)
#define EXIST(s,e) ((s).find(e)!=(s).end())
#define SORT(c) sort((c).begin(),(c).end())
#define MT(a,b,c) MP(a, MP(b, c))
#define T1 first
#define T2 second.first
#define T3 second.second


//repetition
//------------------------------------------
#define FOR(i,a,b) for(int i=(a);i<(b);++i)
#define REP(i,n)  FOR(i,0,n)
#define EPS 1e-8

int dx[8]={1,1,0,-1,-1,-1,0,1};
int dy[8]={0,1,1,1,0,-1,-1,-1};

int m[55][55];
int main(){
	while(1){
		int h,w,ret=0;
		cin>>w>>h;
		if(w==0)return 0;
		memset(m, 0, sizeof(m));
		REP(i,w+2){
			m[0][i]=0;
			m[h+1][i]=0;
		}
		REP(i,h+2){
			m[i][0]=0;
			m[i][w+1]=0;
		}
		REP(i,h)REP(j,w)cin>>m[i+1][j+1];
		priority_queue<PII> q;
		FOR(i,1,h+1){
			FOR(j,1,w+1){
				if(m[i][j]==1){
					ret++;
					q.push(MP(i,j));
					while(!q.empty()){
						PII tmp = q.top();
						q.pop();
						if(m[tmp.first][tmp.second]==0)continue;
						m[tmp.first][tmp.second] = 0;
						REP(i,8)q.push(MP(tmp.first+dy[i], tmp.second+dx[i]));
					}
				}
			}
		}
		cout << ret << endl;
	}
}

C++: #include<iostream>
#include<vector>
#include <unordered_map>  
#include <algorithm>
#include <functional>
#include <map>
#include <string>
#include <sstream>
#include <cmath>
#include <float.h>
using namespace std;

bool checked[50][50];
int island[50][50];
int count1 = 0;
int w, h;

void checkIsland(int x, int y) {
	if (x < 0 || x >= w || y >= h || y < 0) { return; }
	if (!island[x][y]) return;
	checked[x][y] = true;
	checkIsland(x + 1, y);
	checkIsland(x + 1, y + 1);
	checkIsland(x + 1, y - 1);
	checkIsland(x - 1, y + 1);
	checkIsland(x, y + 1);
}

int main() {
	cin >> w >> h;
	for (int i = 0; i < w; ++i) {
		for (int j = 0; j < h; ++j) {
			checked[i][j] = false;
			cin >> island[i][j];
		}
	}
	for (int i = 0; i < w; ++i) {
		for (int j = 0; j < h; ++j) {
			if (checked[i][j]) continue;
			if (island[i][j]) {
				count1++;
				checkIsland(i, j);
			}
			else {
				checked[i][j] = true;
			}
		}
	}
	cout << count1 << endl;
	return 0;
}