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>
#define int long long
#define double long double
#define INF 1e18
using namespace std;

int W, H;
int c[1000][1000];

void dfs(int x, int y) {
  int moveX[3] = {0,-1,1};
  int moveY[3] = {0,-1,1};
  c[x][y] = 0;
  for(int i = 0; i < 3; i++) {
    for(int j = 0; j < 3; j++) {
      int nx=x+moveX[i], ny=y+moveY[j];
      if(nx<0||ny<0) continue;
      if(c[nx][ny] == 1) dfs(nx,ny);
    }
  }
}

signed main() {
  cin>>W>>H;
  while (W+H!=0) {
    // i = y, j = x
    for(int i=0;i<1000;i++)for(int j=0;j<1000;j++)c[i][j]=0;
    for(int i=0;i<H;i++){
      for(int j= 0;j<W;j++){
        cin>>c[i][j];
      }
    }
    
    int ans = 0;
    for (int i = 0; i < H; i++) {
      for (int j = 0; j < W; j++) {
        if (c[i][j] == 1) {
          dfs(i, j);
          ans++;
        }
      }
    }
    cout << ans << endl;
    cin >> W >> H;
  }
}

C++: #include <iostream>
#include <iomanip>
#include <sstream>
#include <vector>
#include <array>
#include <list>
#include <stack>
#include <queue>
#include <set>
#include <map>
#include <unordered_set>
#include <unordered_map>
#include <string>
#include <algorithm>
#include <numeric>
#include <utility>
#include <cstdlib>
#include <ctime>
#include <cstdint>
#include <cmath>

#define FOR(i,k,n)  for(int i = (k);i < (n);++i)
#define REP(i,n)    FOR(i,0,n)
#define EACH(i,x)   for(auto & i : x)
#define ALL(x)      begin(x),end(x)

using namespace std;

typedef vector<int> vecint;

int root(int i,vecint& p){
  if(p[i] != i+1)
    p[i] = root(p[i]-1,p);
  return p[i];
}
void join(int i,int j,vecint& p){
  p[root(i,p)-1] = root(j,p);
}

int main()
{
  while(true){
    int w,h;cin>>w>>h;
    if(!(w||h))break;
    vector<vecint> m(h+2,vecint(w+2));
    REP(i,h)REP(j,w)cin>>m[i+1][j+1];
    int n=(w+2)*(h+2);
    vecint p(n);
    REP(i,n)p[i]=i+1;
    REP(i,h)REP(j,w){
      int di[8] = {1,1,1,0,-1,-1,-1,0};
      int dj[8] = {1,0,-1,-1,-1,0,1,1};
      REP(k,8){
        int ni=i+di[k];
        int nj=j+dj[k];
        if (m[i+1][j+1] && m[ni+1][nj+1]){
          int ijn = (i+1)*(w+2)+(j+1);
          int nijn = (ni+1)*(w+2)+(nj+1);
          join(ijn,nijn,p);
        }
      }
    }
    set<int> s;
    REP(i,h)REP(j,w){
      if(m[i+1][j+1]){
        int ijn = (i+1)*(w+2)+(j+1);
        s.insert(root(ijn,p));
      }
    }
    cout<<s.size()<<endl;
  }
  return 0;
}

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

#define H_MAX 50
#define W_MAX 50

int field[H_MAX][W_MAX];

int w, h;

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

void dfs(int y, int x) {
    field[y][x] = 0;
    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 && field[ny][nx]) {
            dfs(ny, nx);
        }
    }
}

int count() {
    int c = 0;
    for (int i = 0; i < h; i++) {
        for (int j = 0; j < w; j++) {
            if (field[i][j]) {
                c++;
                dfs(i, j);
            }
        }
    }
    return c;
}

int main() {
    memset(field, 0, sizeof(field));
    while (cin >> w >> h) {
        if (w == 0 && h == 0) break;
        for (int i = 0; i < h; i++) {
            for (int j = 0; j < w; j++) {
                cin >> field[i][j];
            }
        }
        cout << count() << endl;
    }
    return 0;
}

C++: #include <iostream>

using namespace std;

int m[51][51];
int w, h;
int isl;
const int dx[] = {0, 1, 1, 1, 0, -1, -1, -1};
const int dy[] = {-1, -1, 0, 1, 1, 1, 0, -1};

void dfs(int x, int y)
{
    m[x][y] = 0;
    for (int i = 0; i < 8; i++) {
        int nx = x + dx[i];
        int ny = y + dy[i];
        
        if (nx < 0 || ny < 0 || nx > w || ny > h)
            continue;
        else if (m[nx][ny] == 1)
            dfs(nx, ny);
    }
}

int main()
{
    while (cin >> w >> h, w || h) {
        isl = 0;

        for (int i = 0; i < h; i++)
            for (int j = 0; j < w; j++)
                cin >> m[i][j];

        for (int i = 0; i < h; i++) 
            for (int j = 0; j < w; j++)
                if (m[i][j] == 1) {
    // for (int y = 0; y < h; y++) {
    //     for (int x = 0; x < w; x++)
    //         cout << m[y][x];
    //     cout << endl;
    // }
    // cout << endl;

                    dfs(i, j);
                    isl++;
                }
            
        cout << isl << endl;
    }

    return 0;
}

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

int w, h;
int maze[55][55];

void dfs(int y, int x){
	maze[y][x] = 0;
	//search8deg
	for(int i=-1; i<=1; i++){
		for(int j=-1; j<=1; j++){
			int nx = x - j;
			int ny = y - i;
			if(0 <= nx and nx < w and 0 <= ny and ny < h and maze[ny][nx] == 1){
				dfs(ny, nx);
			}
		}
	}
}

int main(){
while(1){	
	
	
	cin >> w >> h;
	if(w == 0 and h == 0) break;
	
	for(int i=0; i < h; i++){
		for(int j=0; j < w; j++){
			cin >> maze[i][j]; 
		}
	}
	
	int ans = 0;
	for(int i=0; i < h; i++){
		for(int j=0; j < w; j++){
			if(maze[i][j] == 1){
				dfs(i, j);
				ans += 1;
			}
		}
	}
	
	cout << ans << endl;
	
	
}//END
	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]){d[x][y]=0;f(8)k(x+u[i],y+u[i+8]);}
}
int main(){
	a=0;
	s(n);s(m);
	if(n==0)return 0;
	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();
}