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 <cassert>
#include <cmath>
#include <cstdio>
#include <cstring>
#include <iomanip>
#include <iostream>
#include <map>
#include <memory>
#include <numeric>
#include <set>
#include <stack>
#include <string>
#include <vector>

#define INF 1000000000
#define MOD 1000000007
typedef long long ll;
typedef unsigned long long ull;
using namespace std;
#define int long long

struct Setup {
    Setup() {
        cin.tie(0);
        ios::sync_with_stdio(false);
        cout << fixed << setprecision(20);
    }
} SETUP;

vector<vector<int>> g_graph;
vector<vector<int>> g_visited;
int dy[] = {-1, -1, -1, 0, 0, 1, 1, 1};
int dx[] = {-1, 0, 1, -1, 1, -1, 0, 1};

void dfs(int y, int x, int h, int w) {
    g_visited[y][x] = true;
    for (int i = 0; i < 8; i++) {
        int ny = y + dy[i], nx = x + dx[i];
        if (ny < 0 || ny >= h || nx < 0 || nx >= w) {
            continue;
        }
        if (g_visited[ny][nx]) {
            continue;
        }
        if (g_graph[ny][nx] == 0) {
            continue;
        }
        dfs(ny, nx, h, w);
    }
}

void go(int y, int x, int h, int w, int &island) {
    if (g_visited[y][x]) {
        return;
    }
    if (g_graph[y][x] == 0) {
        return;
    }
    dfs(y, x, h, w);
    island += 1;
}

void solve(int h, int w) {
    g_graph.assign(h, vector<int>(w, 0));
    g_visited.assign(h, vector<int>(w, 0));
    for (int i = 0; i < h; i++) {
        for (int j = 0; j < w; j++) {
            cin >> g_graph[i][j];
        }
    }

    int island = 0;
    for (int i = 0; i < h; i++) {
        for (int j = 0; j < w; j++) {
            go(i, j, h, w, island);
        }
    }
    cout << island << endl;
}

signed main() {
    while (1) {
        int w, h;
        cin >> w >> h;
        if (w == 0 && h == 0) {
            return 0;
        } else {
            solve(h, w);
        }
    }
}

C++: /*
 * B.cpp
 *
 *  Created on: 2017/05/27
 *      Author: aihara
 */

#define MYDEBUG
#include <bits/stdc++.h>

#ifdef MYDEBUG
#define dbp(x) cout << "#x" << ": " << x << endl;
#else
#define dbp(x)
#endif
#define ll long long
using namespace std;

const int maxh = 60;
int w, h;
int c[maxh][maxh];
int di[9] = { -1, -1, -1, 0, 0, 1, 1, 1 };
int dj[9] = { -1, 0, 1, -1, 1, -1, 0, 1 };

int ans = 0;
int dfs(int i, int j) {
	if (c[i][j] != 1) {
		return 0;
	}
	c[i][j] = -1;
	for (int k = 0; k < 9; ++k) {
		int ni = i + di[k], nj = j + dj[k];
		if (0 <= ni && ni < h && 0 <= nj && nj < w && c[ni][nj] == 1) {
			dfs(ni, nj);
		}
	}
	return 1;
}

int main() {
	while (cin >> w >> h, w || h) {
		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) {
				ans += dfs(i, j);
			}
		}
		cout << ans << endl;
	}
	return 0;
}

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

void dfs(int h, int w, int(*A)[50], int W, int H) {
  A[h][w] = 0;
  for (int i = -1; i <= 1; ++i) {
    for (int j = -1; j <= 1; ++j) {
      int hp = h + i;
      int wp = w + j;
      if (0 <= hp && hp < H && 0 <= wp && wp < W && A[hp][wp] == 1) {
        dfs(hp, wp, A, W, H);
      }
    }
  }
}

int main() {
  int A[50][50];
  int W, H, ans;

  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 >> A[i][j];
      }
    }

    ans = 0;
    for (int i = 0; i < H; ++i) {
      for (int j = 0; j < W; ++j) {
        if (A[i][j] == 1) {
          dfs(i, j, A, W, H);
          ++ans;
        }
      }
    }
    cout << ans << endl;
  }  

  return 0;
}

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

void trip(int y,int x,vector<vector<int>>&mp){
	mp.at(y).at(x)=0;
	for(int dy=-1;dy<=1;++dy){
		for(int dx=-1;dx<=1;++dx){
			int nxy=y+dy,nxx=x+dx;
			if(nxy<0||nxy>=mp.size()||nxx<0||nxx>=mp.at(nxy).size()||!mp.at(nxy).at(nxx))continue;
			trip(nxy,nxx,mp);
		}
	}
}

int main(){
	cin.tie(0),ios::sync_with_stdio(false);
	while(true){
		int w,h; cin>>w>>h;
		if(w==0&&h==0)break;
		vector<vector<int>>mp(h,vector<int>(w));
		for(auto&&i:mp)for(auto&&j:i)cin>>j;
		int cnt=0;
		for(int y=0;y<h;++y){
			for(int x=0;x<w;++x){
				if(mp.at(y).at(x)){
					trip(y,x,mp);
					++cnt;
				}
			}
		}
		cout<<cnt<<"\n"s;
	}
}