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++: #include <bits/stdc++.h>
using namespace std;
typedef long long ll;
#define i_7 (ll)(1E9+7)
#define i_5 (ll)(1E9+5)
ll mod(ll a){
    ll c=a%i_7;
    if(c>=0)return c;
    else return c+i_7;
}
typedef pair<int,int> i_i;
typedef pair<ll,ll> l_l;
ll inf=(ll)1E12;//10^12
#define rep(i,l,r) for(ll i=l;i<=r;i++)
#define pb push_back
ll max(ll a,ll b){if(a<b)return b;else return a;}
ll min(ll a,ll b){if(a>b)return b;else return a;}
const double EPS=1E-8;
////////////////////////////////////////

int w,h;
bool m[55][55];

void dfs(int k,int l){
    
    rep(i,-1,1)rep(j,-1,1){
        int x=k+i;int y=l+j;
        if(0<=x&&x<=h-1&&0<=y&&y<=w-1&&m[x][y]){
            
            m[x][y]=false;
            dfs(x,y);
        }
    }
}


int main() {
    ios::sync_with_stdio(false);cin.tie(0);
    
    while(cin>>w>>h){
        if(w==0)break;
        rep(i,0,h-1)rep(j,0,w-1)cin>>m[i][j];
        int ans=0;
        rep(i,0,h-1)rep(j,0,w-1){
            if(m[i][j]){
                ans++;
                m[i][j]=false;
                dfs(i,j);
            }
        }
        cout<<ans<<endl;
    }
    return 0;
}

C++: #include <bits/stdc++.h>
#define int long long
#define double long double
#define INF 1e18
using namespace std;
int grid[100][100];
int H, W;

void dfs(int x, int y) {
  int mx[8] = { 1, 1, 0, -1, -1, -1, 0, 1 };
  int my[8] = { 0, 1, 1, 1, 0, -1, -1, -1 };
  for (int i = 0; i < 8; i++) {
    int _x = x + mx[i];
    int _y = y + my[i];
    if (grid[_x][_y] == 1 && _x >= 0 && _y >= 0 && _x < W && _y < H) {
      grid[_x][_y] = 0;
      dfs(_x, _y);
    }
  }
}

signed main() {
  while (cin >> W >> H, W+H!=0) {
    for (int i = 0; i < H; i++) {
      for (int j = 0; j < W; j++) {
        cin >> grid[j][i];
      }
    }
 
    int ans = 0; 
    for (int i = 0; i < H; i++) {
      for (int j = 0; j < W; j++) {
        if (grid[j][i] == 1) {
          dfs(j,i);
          ans++;
        }
      }
    }

    cout << ans << endl;
  }
}

C++: #include<bits/stdc++.h>
using namespace std;
int m[50][50];
void del(int a,int b){
	m[a][b]=0;
	if(m[a-1][b]==1)del(a-1,b);
	if(m[a][b-1]==1)del(a,b-1);
	if(m[a+1][b]==1)del(a+1,b);
	if(m[a][b+1]==1)del(a,b+1);
	if(m[a-1][b-1]==1)del(a-1,b-1);
	if(m[a-1][b+1]==1)del(a-1,b+1);
	if(m[a+1][b-1]==1)del(a+1,b-1);
	if(m[a+1][b+1]==1)del(a+1,b+1);
}
int main(){
	int a,b,i,j,w,q;
	while(cin>>a>>b,a!=0&&b!=0){
		int c=0;
		for(i=0;i<b;i++)for(j=0;j<a;j++)cin>>m[i][j];
        for(i=0;i<b;i++)for(j=0;j<a;j++)if(m[i][j]==1){del(i,j);c++;}
        	cout<<c<<endl;
    }
}

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

int w,h;
int g[100][100];

void dfs(int y, int x){
  g[y][x] = 0;

  for(int i=-1;i<=1;i++){
    for(int j=-1;j<=1;j++){
      if(y+i<0 || x+j<0 || y+i>=h || x+j>=w)continue;
      if(g[y+i][x+j])dfs(y+i,x+j);
    }
  }
}

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

    int cnt = 0;
    for(int i=0;i<h;i++){
      for(int j=0;j<w;j++){
	if(g[i][j])cnt++, dfs(i,j);
      }
    }
    cout << cnt << endl;
  }
}

C++: #include <algorithm>
#include <iostream>

using namespace std;
#define REP(i, n) for (int i = 0; i < n; i++)

int w, h;
int C[50][50] = {};

void visit(int x, int y) {
  C[x][y] = 0;
  for (int ix = x - 1; ix <= x + 1; ix++) {
    for (int iy = y - 1; iy <= y + 1; iy++) {
      if (ix >= 0 && iy >= 0 && ix < w && iy < h && C[ix][iy] == 1) {
        visit(ix, iy);
      }
    }
  }
}

int main() {
  while (cin >> w >> h, w) {
    int ans = 0;
    C[50][50] = {};
    REP(j, h) {
      REP(i, w) { cin >> C[i][j]; }
    }
    REP(i, w) {
      REP(j, h) {
        if (C[i][j] == 1) {
          ans++;
          visit(i, j);
        }
      }
    }
    cout << ans << endl;
  }
}

C++: #include <bits/stdc++.h>
#define rep(i,n) for (int i = 0; i < (n); ++i)
#define all(v) v.begin(), v.end()
using namespace std;
typedef long long ll;

int W, H;
// dfs
// https://qiita.com/drken/items/4a7869c5e304883f539b
void dfs(vector<vector<int>> &g, int h, int w) {
	g[h][w] = 0;
	for(int dh = -1; dh <= 1; dh++) {
		for(int dw = -1; dw <= 1; dw++) {
			int next_h = h + dh;
			int next_w = w + dw;
			if(next_h < 0 || next_h >= H || next_w < 0 || next_w >= W) continue;
			if(g[next_h][next_w] == 0) continue;
			dfs(g, next_h, next_w);
		}
	}
}

int main() {
	while(1) {
		cin >> W >> H;
		if(W == 0 && H == 0) break;
		vector<vector<int>> c(H, vector<int>(W));
		rep(i, H) rep(j, W) {
			cin >> c[i][j];
		}
		int cnt = 0;
		rep(i, H) rep(j, W) {
			if(c[i][j] == 0) continue;
			dfs(c, i, j);
			cnt++;
		}
		cout << cnt << endl;
	}
	return 0;
}

C++: #include <cstdio>
#define f(n)for(int i=0;i<n;i++)
#define s(w)scanf("%d",&w)
int a,n,m,d[99][99];
int u[16]={-1,-1,-1,0,0,1,1,1,-1,0,1,-1,1,-1,0,1};
void k(int x,int y){
	if(d[x][y]){f(8)k(x+u[i],y+u[i+8]);d[x][y]=0;}
}
int main(){
	a=0;
	s(n);s(m);
	f(9801)d[0][i]=0;
	f(n*m)s(d[i/n+1][i%n+1]);
	//f(n*m)if(d[i/n+1][i%n+1])a++,k(i/n+1,i%n+1);
	//printf("%d\n",a);
	//main();
}