Go: package main

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

const (
	initialBufSize = 100000
	maxBufSize     = 1000000
)

var sc *bufio.Scanner

func initScanner(r io.Reader) *bufio.Scanner {
	buf := make([]byte, initialBufSize)

	sc := bufio.NewScanner(r)
	sc.Buffer(buf, maxBufSize)
	sc.Split(bufio.ScanWords) // bufio.ScanLines
	return sc
}

func main() {
	sc = initScanner(os.Stdin)
	resolve()
}

func resolve() {
	results := []int{}
	for true {
		w, h := scanInt(sc), scanInt(sc)
		if w == 0 && h == 0 {
			break
		}
		grid := make([][]int, h)
		for i := 0; i < h; i++ {
			grid[i] = make([]int, w)
			for j := 0; j < w; j++ {
				grid[i][j] = scanInt(sc)
			}
		}
		num := 0
		for i := range loop0(h) {
			for j := range loop0(w) {
				start := pos{y: i, x: j}
				if grid[start.y][start.x] == 0 {
					continue
				}
				num++
				_dfs(grid, w, h, start)
			}
		}
		results = append(results, num)
	}
	for _, v := range results {
		fmt.Println(v)
	}
	return
}

func _dfs(grid [][]int, w, h int, start pos) {
	grid[start.y][start.x] = 0
	for _, df := range []pos{
		pos{1, 0}, pos{-1, 0},
		pos{0, -1}, pos{0, 1},
		pos{1, 1}, pos{-1, -1},
		pos{-1, 1}, pos{1, -1},
	} {
		np := addPos(start, df)
		if np.y < 0 || h <= np.y || np.x < 0 || w <= np.x {
			continue
		}
		if grid[np.y][np.x] == 0 {
			continue
		}
		_dfs(grid, w, h, np)
	}
}

type pos struct{ x, y int }

func addPos(p, d pos) pos {
	return pos{
		x: p.x + d.x,
		y: p.y + d.y,
	}
}

func loop0(n int) chan int {
	return loop(0, n-1, 1)
}

func loop1(n int) chan int {
	return loop(1, n, 1)
}

func loop(s, e, d int) chan int {
	ch := make(chan int)
	go func() {
		for i := s; i <= e; i += d {
			ch <- i
		}
		close(ch)
	}()
	return ch
}

// snip-scan-funcs
func scanInt(sc *bufio.Scanner) int {
	sc.Scan()
	i, _ := strconv.Atoi(sc.Text())
	return int(i)
}
func scanString(sc *bufio.Scanner) string {
	sc.Scan()
	return sc.Text()
}

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

int W, H;

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

void solve(int cx, int cy, vector<vector<int>>& v) {
    v[cx][cy] = 0;
    for(int i=0; i<8; ++i) {
        int nx = cx + dx[i];
        int ny = cy + dy[i];
        if(nx >= 0 && nx < W && ny >= 0 && ny < H && v[nx][ny] == 1) {
            solve(nx, ny, v);
        }
    }
}


int main() {
    while(cin >> W >> H, W) {
        vector<vector<int>> v(W, vector<int>(H));
        for(int i=0; i<H; ++i) {
            for(int j=0; j<W; ++j) {
                cin >> v[j][i]; // 0: sea, 1:island
            }
        }
        int ans=0;
        for(int i=0; i<W; ++i) {
            for(int j=0; j<H; ++j) {
                if(v[i][j] == 1) {
                    solve(i, j, v);
                    ++ans;
                }
            }
        }
        cout << ans << endl;
    }
}

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

//#define int long long
//struct Fast {Fast(){cin.tie(0);ios::sync_with_stdio(0);}} fast;
//const int MOD = 1e9 + 7;
using intpair = pair<int, int>;

#define rep(i, n) for (int i = 0; i < (int)(n); i++)
#define reps(i, n, m) for (int i = (int)(n); i <= (int)(m); i++)
#define ALL(obj) (obj).begin(), (obj).end()
#define rALL(obj) (obj).rbegin(), (obj).rend()
#define ceiv(a, b) ((int)((a) + (b) - 1)/(int)(b))

int H, W;
bool is_safe(int x, int y){
    return (0 <= x && x < W)&&(0 <= y && y < H);
}

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

void solve(){
    bool field[H][W], visited[H][W];
    rep(i,H)rep(j,W) visited[i][j] = false;
    rep(i,H)rep(j,W) cin >> field[i][j];
    
    int ans = 0;
    
    rep(y,H)rep(x,W){
        if(visited[y][x] || !field[y][x]) continue;
        stack<intpair> s;
        s.push({x,y});
        ans++;
        while(!s.empty()){
            int x2 = s.top().first, y2 = s.top().second;
            visited[y2][x2] = true;
            s.pop();
            rep(i,8){
                if(!is_safe(x2+dx[i], y2+dy[i])) continue;
                if(!field[y2+dy[i]][x2+dx[i]])   continue;
                if(visited[y2+dy[i]][x2+dx[i]])  continue;
                s.push({x2+dx[i],y2+dy[i]});
            }
        }
    }
    cout << ans << endl;
}

signed main(){
    while(true){
        cin >> W >> H;
        if(H==0 && W==0) break;
        solve();
    }
}

C++: #include <bits/stdc++.h>
#define rep(i,a,b) for(int i=int(a);i<int(b);++i)
#define SIZE 200005
#define INF 1000000005LL
#define MOD 1000000007

using namespace std;
typedef long long int ll;
typedef pair <int,int> P;

int w,h;
int c[55][55];
int d[55][55];
int a;

void dfs(int i,int j);
void dfs_(int i,int j){
  if(i>=0 && j>=0) if(c[i][j]==1) dfs(i,j);
}

void dfs(int i,int j){
  if(d[i][j]==1)return;
  d[i][j] = 1;
  rep(l,0,3) rep(r,0,3) dfs_(i-1+l,j-1+r);
}


int imain(){
  a=0;
  rep(i,0,52) rep(j,0,52) c[i][j]=d[i][j]=0;
  rep(i,0,h) rep(j,0,w) {
    cin >> c[i][j];
  }

  rep(i,0,h) rep(j,0,w) if(c[i][j]==1 && d[i][j]==0) {
    dfs(i,j);
    a++;
  }
  cout << a << endl;

  return 0;
}

int main(){
  for(;;){
    cin >> w >> h;
    if(w==0 && h==0)break;
    imain();
  }
  return 0;
}

C++: #include <bits/stdc++.h>
using namespace std;
vector<string>v;
vector<int>ansl;
string s;
int ary[100005] = {0};
int mp[51][51];
int cnt = 0;
int d_x[] = {0,1,0,-1};
int d_y[] = {1,0,-1,0};
int W,H;

void dfs(int x,int y){
  mp[y][x] = 0;
  for(int dx = -1;dx<2;dx++){
    for(int dy = -1;dy<2;dy++){
    int nx = x+dx,ny = y+dy;
    if((nx>=0)&&(ny>=0)&&(ny<H)&&(nx<W)&&(mp[ny][nx] == 1)){
      dfs(nx,ny);
    }
  }
}
}
int main(){
  cin >>W>>H;

  while((W!=0)&&(H!=0)){
    cnt = 0;
    for(int i = 0;i<51;i++){
      for(int j = 0;j<51;j++){
        mp[i][j] = 0;
      }
    }
    for(int i = 0;i<H;i++){
      for(int j = 0;j<W;j++){
        cin >>mp[i][j];
      }
    }
    for (int y = 0; y < H; y++){
      for (int x = 0; x < W; x++) {
        if (mp[y][x] == 1) {
          dfs(x, y);
          cnt++;
        }
      }
    }
    cout <<cnt<<endl;
    cin >>W>>H;
  }
  return 0;
}

C++: //default
#define _GLIBCXX_DEBUG
#include<bits/stdc++.h>
using namespace std;
#define int long long
#define double long double
typedef pair<int, int> P_i;
typedef pair<double,double> P_d;
#define rep(i,n) for(int i=0;i<(n);++i)
#define alrep(i,j,n) for(int i=0;i<(n);++i)for(int j=i+1;j<(n);++j)
#define Yes cout << "Yes" << endl;
#define No cout << "No" << endl;
#define answer cout << ans << endl;
template<class T> inline bool chmin(T& a, T b) { if (a > b) { a = b; return true; } return false; }
template<class T> inline bool chmax(T& a, T b) { if (a < b) { a = b; return true; } return false; }
const int MOD=1e9+7;
const long long INF = 1LL << 60;

bool seen[55][55];
int h,w,ans=0;
char field[55][55];
int dx[8]={1,1,0,-1,-1,-1,0,1};
int dy[8]={0,1,1,1,0,-1,-1,-1};

void dfs(int x,int y) {
	seen[x][y] = true; // v を訪問済にする
	// v から行ける各頂点 next_v について
	rep(i,8){
		int nx=x+dx[i];
		int ny=y+dy[i];
		if(nx<0 || nx>=w || ny<0 || ny>=h) continue;
		if(seen[nx][ny]==true) continue;
		if(field[nx][ny]=='0') continue;
		dfs(nx,ny);
	}
}

signed main(){
	while(cin >> w >> h && (w!=0 || h!=0)){
		ans=0;
		rep(y,h)rep(x,w){
			cin >> field[x][y];
		}
		memset(seen,false,sizeof(seen));
		rep(y,h)rep(x,w){
			if(seen[x][y]==true) continue;
			if(field[x][y]=='0') continue;
			ans++;
			dfs(x,y);
		}
		cout << ans << endl;
	}

	return 0;
}