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 int ll;
typedef pair<int,int> P;

#define rep(i,n) for(int i=0;i<(n);++i)
#define repi(i,a,b) for(int i=int(a);i<(b);i++)
#define all(x) x.begin(),x.end()

const ll mod = 1e9+7;
const ll INF = 1e9;

ll gcd(ll a,ll b){return b?gcd(b,a%b):a;}
int dx[4]={1,0,-1,0};
int dy[4]={0,1,0,-1};

bool valid(int lx,int ux,int ly,int uy,int x,int y){
	return (lx<=x&&x<ux&&ly<=y&&y<uy);
}

void dfs(vector<vector<int>> &s,int w,int h,int x,int y){
	s[y][x]=0;
	repi(dx,-1,2){
		repi(dy,-1,2){
			if(valid(0,w,0,h,x+dx,y+dy)&&s[y+dy][x+dx]==1){
				dfs(s,w,h,x+dx,y+dy);
			}
		}
	}
}

int main()
{
	int w,h;
	while(cin>>w>>h,w!=0&&h!=0){
		vector<vector<int>> s(h,vector<int>(w,0));
		rep(y,h)rep(x,w)cin>>s[y][x];

		int ans=0;
		rep(y,h){
			rep(x,w){
				if(s[y][x]==1)ans++,dfs(s,w,h,x,y);
			}
		}
		cout<<ans<<endl;
	}
	return 0;
}

C++: #include<iostream>
#include<vector>
#include<map>
#include<string>
#include<cstring>
#include<bitset>
#include<stack>
#include<queue>
#include<cmath>
#include<algorithm>
#include<cstdio>
#include<numeric>
using namespace std;

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

#define rep(i,x) for(int i=0;i<x;i++)
#define re(i,x,y) for(int i=x;i<y;i++)

long long INF=1e9;
const long long mod=2019;
#define ll long long

int H,W;
vector<vector<int>>field;
void dfs(int h,int w){
  field[h][w]=0;
  for(int dh=-1;dh<=1;dh++){
    for(int dw=-1;dw<=1;dw++){
      int nh=h+dh,nw=w+dw;
      if(nh<0||nh>=H||nw<0||nw>=W)continue;
      if(field[nh][nw]==0)continue;
      dfs(nh,nw);
    }
  }
}

int main(){
  while(cin>>W>>H){
    if(H==0||W==0)break;
    field.assign(H,vector<int>(W,0));
    for(int h=0;h<H;h++){
      for(int w=0;w<W;w++){
        cin>>field[h][w];
      }
    }
    int count=0;
    for(int h=0;h<H;h++){
      for(int w=0;w<W;w++){
        if(field[h][w]==0)continue;
        dfs(h,w);
        count++;
      }
    }
    cout<<count<<endl;
  }
}

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[i/99][i%99]=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();
}

C++: #include <bits/stdc++.h>
#define rep(i,n) for(int i = 0; i < n; i++)

using namespace std;

    int w,h;
    int c[55][55];
    vector<vector<bool> > seen;
    int mx[8] = {-1,1,0,-1,1,-1,0,1};
    int my[8] = {0,0,1,1,1,-1,-1,-1};
    
    void dfs(int x,int y){
    	::seen[x][y] = true;
    	
    	for(int i = 0; i < 8; i++){
			int nx = x + mx[i];
			int ny = y + my[i];
			
			if(nx < 0 || nx >= h || ny < 0 || ny >= w)	continue;
			if(::seen[nx][ny])	continue;
			if(c[nx][ny] == 0)	continue;
			
			dfs(nx,ny);
		}
    }
int main(){
	while(true){
	cin >> w >> h;
	if(w == 0 && h == 0) break;
	::seen.assign(55,vector<bool>(55,false)); //initilaization
	
	
	int count = 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++){
			if(::seen[i][j])	continue;
			if(c[i][j] == 0)	continue;
			
			dfs(i,j);
			count++;
		}
	}
	cout << count << endl;
	}
}

C++: #include <bits/stdc++.h>
#define REP(i,n) for(int i = 0 ; i < n ; ++i)

using namespace std;

#ifdef _DEBUG
#define debug_io fstream cin("input.txt");ofstream cout("output.txt");
#else
#define debug_io ;
#endif

int main()
{
	debug_io
	int w, h;
	while (cin >> w >> h, w + h) {
		int tile[55][55];
		REP(i, h) {
			REP(j, w) {
				cin >> tile[i][j];
			}
		}

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

		int ans = 0;
		REP(i, h) {
			REP(j, w) {
				if (tile[i][j] == 1) {
					vector<pair<int, int>> bfs;
					bfs.push_back({ i,j });

					while (bfs.size()) {
						auto now = bfs.front();
						bfs.erase(bfs.begin());
						int x = now.second;
						int y = now.first;
						tile[y][x] = 2;
						REP(k, 8) {
							int nx = max(0,min(x + dx[k],w-1));
							int ny = max(0, min(y + dy[k], h - 1));
							if (tile[ny][nx] == 1) {
								bfs.push_back({ ny,nx });
							}
						}
					}
					ans++;
					
				}
			}
		}
		cout << ans << endl;
	}
}