Go: package main

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

func getScanner(fp *os.File) *bufio.Scanner {
	scanner := bufio.NewScanner(fp)
	scanner.Split(bufio.ScanWords)
	scanner.Buffer(make([]byte, 1000005), 1000005)
	return scanner
}

func getNextString(scanner *bufio.Scanner) string {
	scanner.Scan()
	return scanner.Text()
}

func getNextInt(scanner *bufio.Scanner) int {
	i, _ := strconv.Atoi(getNextString(scanner))
	return i
}

func getNextInt64(scanner *bufio.Scanner) int64 {
	i, _ := strconv.ParseInt(getNextString(scanner), 10, 64)
	return i
}

func getNextUint64(scanner *bufio.Scanner) uint64 {
	i, _ := strconv.ParseUint(getNextString(scanner), 10, 64)
	return i
}

func getNextFloat64(scanner *bufio.Scanner) float64 {
	i, _ := strconv.ParseFloat(getNextString(scanner), 64)
	return i
}

func main() {
	fp := os.Stdin
	wfp := os.Stdout
	cnt := 0

	if os.Getenv("MASPY") == "ますピ" {
		fp, _ = os.Open(os.Getenv("BEET_THE_HARMONY_OF_PERFECT"))
		cnt = 0
	}
	if os.Getenv("MASPYPY") == "ますピッピ" {
		wfp, _ = os.Create(os.Getenv("NGTKANA_IS_GENIUS10"))
	}

	scanner := getScanner(fp)
	writer := bufio.NewWriter(wfp)
	for solve(scanner, writer) {

	}
	for i := 0; i < cnt; i++ {
		fmt.Fprintln(writer, "-----------------------------------")
		solve(scanner, writer)
	}
	writer.Flush()
}

func solve(scanner *bufio.Scanner, writer *bufio.Writer) bool {
	sol := solver{}
	sol.w = getNextInt(scanner)
	sol.h = getNextInt(scanner)
	if sol.w == 0 && sol.h == 0 {
		return false
	}
	sol.land = makeInts(sol.h, sol.w)
	for y := 0; y < sol.h; y++ {
		for x := 0; x < sol.w; x++ {
			sol.land[y][x] = getNextInt(scanner)
		}
	}
	ans := 0
	for y := 0; y < sol.h; y++ {
		for x := 0; x < sol.w; x++ {
			if sol.land[y][x] == 0 {
				continue
			}
			ans++
			sol.dfs(y, x)
		}
	}
	fmt.Fprintln(writer, ans)
	return true
}

type solver struct {
	w, h int
	land [][]int
}

func (sol *solver) dfs(y, x int) {
	if y < 0 {
		return
	}
	if y >= sol.h {
		return
	}
	if x < 0 {
		return
	}
	if x >= sol.w {
		return
	}
	if sol.land[y][x] == 0 {
		return
	}
	sol.land[y][x] = 0
	dy := [8]int{-1, -1, -1, 0, 0, 1, 1, 1}
	dx := [8]int{-1, 0, 1, -1, 1, -1, 0, 1}
	for i := 0; i < 8; i++ {
		sol.dfs(y+dy[i], x+dx[i])
	}
}
func makeInts(h, w int) [][]int {
	index := make([][]int, h, h)
	data := make([]int, h*w, h*w)
	for i := 0; i < h; i++ {
		index[i] = data[i*w : (i+1)*w]
	}
	return index
}

C++: /*
    Problem 25
    https://onlinejudge.u-aizu.ac.jp/problems/1160
*/
#include <bits/stdc++.h>
using namespace std;
/* typedef */
typedef long long ll;
typedef pair<int, int> pii;
/* constant */
const int INF = 1 << 30;
const ll LINF = 1LL << 50;
const int NIL = -1;
const int MAX = 55;
const int mod = 1000000007;
const double pi = 3.141592653589;
/* global variables */
int h, w;
vector<vector<bool>> visited(MAX, vector<bool>(MAX));
vector<vector<int>> G(MAX, vector<int>(MAX));
/* function */
void init() {
    // for (int i = 0; i < h; i++) for (int j = 0; j < w; j++) G[i][j] = false;
    // for (int i = 0; i < h; i++) for (int j = 0; j < w; j++) visited[i][j] = false;
    for (int i = 0; i < h*w; i++) G[i/w][i%w] = false;
    for (int i = 0; i < h*w; i++) visited[i/w][i%w] = false;
}
void inputG() { // for (int i = 0; i < h; i++) for (int j = 0; j < w; j++) cin >> G[i][j];
    for (int i = 0; i < h*w; i++) cin >> G[i/w][i%w];}
bool check(int x, int y) {
    if (x < 0 || h < x || y < 0 || w < y) return false;
    if (visited[x][y] || !G[x][y]) return false;
    return true;
}
void bfs(int sx, int sy) {
    queue<pii> q;
    q.push(pii(sx, sy));
    visited[sx][sy] = true;
    while (!q.empty()) {
        pii p = q.front(); q.pop();
        int x = p.first, y = p.second;
        // 八方向を探索
        for (int dx = -1; dx <= 1; dx++) for (int dy = -1; dy <= 1; dy++) {
            int nx = x + dx, ny = y + dy;
            if (check(nx, ny)) {
                q.push(pii(nx, ny));
                visited[nx][ny] = true;
            }
        }
    }
}
void dfs(int x, int y) {
    // 訪れた
    visited[x][y] = true;
    // 八方向を探索
    for (int dx = -1; dx <= 1; dx++) for (int dy = -1; dy <= 1; dy++) {
        int nx = x + dx, ny = y + dy;
        // 範囲外 or 訪れたことがある(visited[i][j] == true) or 海(G[i][j] = 0)
        if (check(nx, ny))
            // 再帰的に探索
            dfs(nx, ny);
    }
}
void solve() {
    int ans = 0;
    for (int i = 0; i < h; i++) for (int j = 0; j < w; j++) {
        if (!visited[i][j] && G[i][j]) {
            ans++;
            //bfs(i, j);
            dfs(i, j);
        }
    }
    cout << ans << '\n';
}

/* main */
int main(){
    while (1) {
        cin >> w >> h;
        if (h == 0) return 0;
        init();
        inputG();
        solve();
    }
}

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

#define FOR(i,a,b) for(int i=(a);i<(b);++i)
#define REP(i,n)   FOR(i,0,n)
#define ALL(a)     (a).begin(),(a).end()
#define VI         vector<int>
#define MOD 1000000007

typedef long long int ll;

template<class T>bool chmax(T &a, const T &b) { if (a<b) { a=b; return 1; } return 0; }
template<class T>bool chmin(T &a, const T &b) { if (b<a) { a=b; return 1; } return 0; }

const ll INF=(ll)1e19;
//const int INF=(1<<30);
int h,w;
int m[50][50];

int dx[] = {0,1,-1,0,1,-1,1,-1};
int dy[] = {1,0,0,-1,1,-1,-1,1};
bool visited[50][50];

int dfs(int x, int y, int d){
  if(m[x][y]==0)return d;
  if(visited[x][y])return d;
  visited[x][y]=true;

  REP(i,8){
    int nx = x + dx[i];
    int ny = y + dy[i];

    if(nx < 0 || nx >= h || ny <0 || ny >= w)continue;
    if(m[nx][ny]==0)continue;
    if(visited[nx][ny])continue;

    dfs(nx, ny, d);
  }

  return d+1;
}

int main(){
  while(true){

    cin >> w >> h;
    if(h+w==0)break;
    REP(i,h)REP(j,w)cin >> m[i][j];
    REP(i,h)REP(j,w)visited[i][j]=false;

    int d = 0;
    REP(i,h)REP(j,w){
      d = dfs(i,j,d);
    }
    cout << d << endl;
  }
}

C++: #include <bits/stdc++.h>
using namespace std;
using Int = long long;
Int const INF = 1e10;

int w,h; int fie[50][50];
void dfs(int y,int x){
    fie[y][x]=0;
    for (int i = -1; i <= 1; i++){
        for (int j = -1; j <= 1; j++){
            int ny=y+i,nx=x+j;
            if(ny>=0&&ny<h&&nx>=0&&nx<w&&fie[ny][nx]==1)dfs(ny,nx);
        }
    }
}

int main(){
    cin.tie(0); ios::sync_with_stdio(false);

    while(true){
        cin>>w>>h; if(w+h==0)break;
        for (int i = 0; i < h; i++){
            for (int j = 0; j < w; j++) cin>>fie[i][j];
        }
        int ans=0;
        for (int i = 0; i < h; i++){
            for (int j = 0; j < w; j++){
                if(fie[i][j]==1){
                    dfs(i,j);
                    ans++;
                }
            }
        }
        cout<<ans<<'\n';
    }
}

C++: #include <cstdio>
#include <vector>
using namespace std;


int islands[50][100];
int w,h;

void dfs(int y,int x){

  islands[y][x] = 0;

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

  for(int i = 0;i < 8;i++){
    int nx = x + dx[i];
    int ny = y + dy[i];
    if(0 <= nx && nx < w && 0 <= ny && ny < h && islands[ny][nx] == 1)dfs(ny,nx);
  }

  return ;
}


int main(){

  vector<int> v;

  while(scanf("%d %d",&w,&h),w != 0 && h != 0){

    int ans = 0;

    for(int i = 0;i < h;i++){
      for(int j = 0;j < w;j++){
        scanf("%d",&islands[i][j]);
      }
    }

    for(int i = 0;i < h;i++){
      for(int j = 0;j < w;j++){
        if(islands[i][j] == 1){
          dfs(i,j);
          ans++;
        }
      }
    }


    v.push_back(ans);
  }

  for(int i = 0;i < v.size();i++){
    printf("%d\n",v[i]);
  }

  return 0;
}