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 <algorithm>
#include <iostream>
#include <limits.h>
#include <stdlib.h>
#include <string>
#include <vector>

#define el endl
#define fd fixed
#define INF INT_MAX/2-1
#define pb push_back

using namespace std;

bool island[50][50];
bool visited[50][50];

void visit(int x, int y) {
  visited[y][x] = true;
  if (x > 0 && island[y][x-1] && !visited[y][x-1]) visit(x-1, y);
  if (x < 49 && island[y][x+1] && !visited[y][x+1]) visit(x+1, y);
  if (y > 0 && island[y-1][x] && !visited[y-1][x]) visit(x, y-1);
  if (y < 49 && island[y+1][x] && !visited[y+1][x]) visit(x, y+1);
  if (x > 0 && y > 0 && island[y-1][x-1] && !visited[y-1][x-1]) visit(x-1, y-1);
  if (x > 0 && y < 49 && island[y+1][x-1] && !visited[y+1][x-1]) visit(x-1, y+1);
  if (x < 49 && y > 0 && island[y-1][x+1] && !visited[y-1][x+1]) visit(x+1, y-1);
  if (x < 49 && y < 49 && island[y+1][x+1] && !visited[y+1][x+1]) visit(x+1, y+1);
}

int main() {
  int w, h, tmp, cnt;
  while (cin >> w >> h, w | h) {
    for (int i = 0; i < 50; i++) {
      for (int j = 0; j < 50; j++) {
        island[i][j] = false;
        visited[i][j] = false;
      }
    }
    cnt = 0;

    for (int i = 0; i < h; i++) {
      for (int j = 0; j < w; j++) {
        if (cin >> tmp, tmp)
          island[i][j] = true;
      }
    }
    for (int i = 0; i < h; i++) {
      for (int j = 0; j < w; j++) {
        if (island[i][j] && !visited[i][j]) {
          visit(j, i);
          cnt++;
        }
      }
    }
    cout << cnt << el;
  }
}

C++: #include <iostream>
#include <map>
// #include <cstdio>
#include <stacK>

using namespace std;

int main() {
	while(1) {
	int w, h, data[52][52]={{0}};
	cin >> w >> h;
	if (w == 0 && h ==0) break;
	// memset(data, 0, sizeof(data));
	for (int i=1; i<=h; i++) {
		for (int j=1; j<=w; j++) {
			cin >> data[i][j];
		}
	}
	// for (int i=0; i<h; i++) {
	// 	for (int j=0; j<w; j++) {
	// 		cout << data[i][j] << "," ;
	// 	}cout <<endl;
	// }
	stack<pair<int, int> > s;
	int count=0;
	int H[8] = {-1, -1, -1, 0, 0, 1, 1, 1}, W[8] ={-1, 0, 1, -1, 1, -1, 0, 1};
	for(int i=1; i<=h; i++) {
		for (int j=1; j<=w; j++) {
			if (data[i][j]) {
				count++;
				s.push(make_pair(i, j));
				while (!(s.empty())) {
					pair<int, int> now;
					now = s.top();
					s.pop();
					data[now.first][now.second]=0;
					for (int k=0; k<8; k++) {
						if (data[H[k]+now.first][W[k]+now.second])
							s.push(make_pair(H[k]+now.first, W[k]+now.second));
					}
				}
			}
		}
	}
	cout << count <<endl;
}
}

C++: #include<iostream>
#include<algorithm>
#include<vector>
#include<map>
#include<set>
#include<string>
#include<queue>
#include<stack>
using namespace std;
#define INF (1 << 30) - 1
#define LINF 1LL << 60
const int MOD = 1000000007;
using ll = long long;
using P = pair<int, int>;

vector<int> dx{1, 0, -1, 0, 1, -1, 1, -1};
vector<int> dy{0, 1, 0, -1, -1, 1, 1, -1};

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

vector<vector<bool>> seen;
void dfs(int x, int y){
  seen[y][x] = true;
  for(int i = 0; i < 8; ++i){
    int nx = x + dx[i];
    int ny = y + dy[i];
    if(nx < 0 || w <= nx || ny < 0 || h <= ny)continue;
    else if(!g[ny][nx])continue;
    else if(seen[ny][nx])continue;
    dfs(nx, ny);
  }
}

int main(){
  while(true){
    cin >> w >> h;
    if(w == 0 && h == 0)return 0;
    g = vector<vector<int>>(h, vector<int>(w));
    for(int i = 0; i < h; ++i){
      for(int j = 0; j < w; ++j){
        cin >> g[i][j];
      }
    }

    seen = vector<vector<bool>>(h, vector<bool>(w, false));
    int cnt = 0;
    for(int i = 0; i < h; ++i){
      for(int j = 0; j < w; ++j){
        if(seen[i][j])continue;
        if(!g[i][j])continue;
        dfs(j, i);
        ++cnt;
      }
    }

    cout << cnt << endl;
  }

  return 0;
}

C++: #include <iostream>
#include <string>
#include <algorithm>
using namespace std;
#define FOR(i,a,b) for(int i=a;i<b;i++)
#define REP(i,b) FOR(i,0,b)
#define INF 1e9

int w,h,c[50][50],i_num;
int dx[] = {1,0,-1}, dy[] = {1,0,-1};

void search(int x, int y){
  if(c[y][x] == 1 && x >=0 && x < w && y >=0 && y < h){
    c[y][x] = i_num;
    REP(i,3) REP(j,3){
      if(i != 0 || j != 0 || x-1 >= 0 || x+1 < w || y-1 >= 0 || y+1 < h)
      search(x+dx[i],y+dy[j]);
    }
  }
}

void solve(int X, int Y){
  i_num = 2;
  REP(i,20) REP(j,20) c[i][j] = 0;
  //0:ocean, 1:land, 2~:island_number

  REP(i,Y){
    REP(j,X){
      cin>>c[i][j];
    }
  }

  REP(i,Y){
    REP(j,X){
      if(c[i][j] == 1){
        search(j,i);
        i_num++;
      }
    }
  }

  cout<<i_num-2<<endl;
  /*REP(i,20){
     REP(j,20) cout<<c[i][j];
     cout<<endl;
  }*/
}

int main(){

  while(1) {
    cin>>w>>h;
    if(w == 0 && h == 0) break;
    solve(w,h);
  }

  return 0;
}