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;
using vi=vector<int>;
using vvi=vector<vi>;
using vs=vector<string>;
using msi=map<string,int>;
#define rep(i,n) for(int i=0;i<n;i++)
#define all(a) a.begin(),a.end()
#define rall(a) a.rbegin(),a.rend()
vvi f;
int ans=0;
void search(int x,int y){
    vi v={1,0,-1,0,1,1,-1,-1,1};
    rep(i,8){
        if(f[y+v[i]][x+v[i+1]]==1){
            f[y+v[i]][x+v[i+1]]=0;
            search(x+v[i+1],y+v[i]);
        }
    }
}

int main(void){
    int w,h;
    while(cin>>w>>h,w){
        ans=0;
        f=vvi(h+2,vi(w+2,0));
        rep(i,h)rep(j,w){
            cin>>f[i+1][j+1];
        }
        rep(i,h)rep(j,w){
            if(f[i+1][j+1]==1){
                search(j+1,i+1);
                ans++;
            }
        }
        cout<<ans<<endl;
    }
}

C++: #include<iostream>
using namespace std;
int w,h,i,j;
int map[52][52];

void rec(int i,int j){
 
  if(map[i][j]==0)return;
  map[i][j]=0;
  rec(i+1,j);
  rec(i-1,j);
  rec(i,j-1);
  rec(i,j+1);
  rec(i-1,j-1);
  rec(i-1,j+1);
  rec(i+1,j-1);
  rec(i+1,j+1);
}
int main(){
  
  
  while(1){
    cin >> w >> h;
    if(w==0 && h==0)break;
    
    for(i=1;i<=h;i++){
      for(j=1;j<=w;j++){
	cin >> map[i][j];
	
      }
    }
    int count=0;
    for(int k=1;k<=h;k++){
      for(int l=1;l<=w;l++){
	if(map[k][l]==1){
	  count++;
	  rec(k,l);
	}
      }
    }
    cout << count << endl;
  }
  
  return 0;
}

C++: #include<bits/stdc++.h>
using namespace std;
using ll = long long;
#define rep(i, n) for(int i = 0; i < (n); i++)
#define pb push_back
const int maxn = 10000;
const int INF32 = 1'050'000'000;
const long long INF64 = 4'000'000'000'000'000'000;
const int MOD7 = 1'000'000'007;
const int MOD9 = 1'000'000'009;

ll gcd(ll a, ll b) { return b ? gcd(b, a%b) : a; }

int h, w;
using Graph = vector<vector<int>>;
Graph G;
vector<pair<int,int>> vis;
int cnt;

void dfs(int x, int y)
{
	G[x][y] = cnt;
	for(int dx = -1; dx <= 1; dx++){
		for(int dy = -1; dy <= 1; dy++){
			int nx = x + dx;
			int ny = y + dy;
			if(nx<0 || nx>=h || ny<0 || ny>=w || 
				G[nx][ny]!=1)
				continue;
			dfs(nx,ny);
		}
	}
}

int main()
{
	int time = 0;
	while(1){
		cin >> w >> h;
		if(h==0&&w==0) break;
		G.clear();
		G.resize(h);
		rep(i,h) G[i].resize(w);
		rep(i,h)rep(j,w){//input
			cin >> G[i][j];
		}
		cnt = 1;
		rep(i,h)rep(j,w){
			if(G[i][j]==1){ 
				cnt++;
				dfs(i,j);
			}
		}
		// rep(i,h)rep(j,w){
		// 	if(j!=w-1) printf("%d ", G[i][j]);
		// 	else printf("%d\n", G[i][j]);
		// }
		time++;
		cout << cnt-1 << endl;
	}
}

C++: #include <bits/stdc++.h>
using namespace std;
using ll=long long;
typedef pair<int,int> P;
template<class T> inline bool chmax(T& a, T b) { if (a < b) { a = b; return 1; } return 0; }
template<class T> inline bool chmin(T& a, T b) { if (a > b) { a = b; return 1; } return 0; }
int w,h;
int nx,ny;
void dfs(vector<vector<int>> &G,int a,int b){
     if(0>nx|| nx>=h || 0>ny|| ny>=w) return ;
     if( G.at(nx).at(ny)==0) return ; 
     G.at(a).at(b)=0;
    for(int k=-1;k<=1;k++){
        for(int s=-1;s<=1;s++){
            nx=a+k;
            ny=b+s;
            dfs(G,nx,ny);
        }
    }
    return ;
}
 
 
 
int main(){
    cin>>w>>h;
    vector<vector<int>>G(h,vector<int>(w));
    for(int i=0;i<h;i++){
        for(int j=0;j<w;j++){
            cin>>G.at(i).at(j);
        }
    }
    int count=0;
    for(int i=0;i<h;i++){
        for(int j=0;j<w;j++){
            if(G.at(i).at(j)==1){
                count++;
                dfs(G,i,j);
            }
        }
    }
cout<<count<<endl;
}

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

vector<vector<bool>> seen;
int H, W;
int d_h[8] = {1,1,1,0,0,-1,-1,-1};
int d_w[8] = {1,0,-1,1,-1,1,0,-1};

void dfs(const vector<vector<int>> &m, int h, int w){
    seen[h][w] = true;

    //cout << h << ' ' << w <<  ' ' << m[h][w] << endl;
    
    for(int i = 0; i < 8; i++){
        if(1 <= h+d_h[i] && h+d_h[i] <= H && 1 <= w+d_w[i] && w+d_w[i] <= W && (!seen[h+d_h[i]][w+d_w[i]]) && m[h+d_h[i]][w+d_w[i]] == 1){
            dfs(m, h+d_h[i], w+d_w[i]);
        }
    }
}

int main(){
    while(1){
        cin >> W >> H;
        seen.assign(H+1, vector<bool>(W+1, false));
        if(H == 0 && W == 0) break;
        vector<vector<int>> m(H+1, vector<int>(W+1,0));
        for(int i = 1; i <= H; i++){
            for(int j = 1; j <= W; j++){
                cin >> m[i][j];
            }
        }

        int cnt = 0;
        for(int i = 1; i <= H; i++){
            for(int j = 1; j <= W; j++){
                if(!seen[i][j] && m[i][j] == 1) {
                    //cout << i << ' ' << j << endl;
                    cnt++;
                    dfs(m, i, j);
                }
            }
        }
        cout << cnt << endl;
    }
}