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;
#define rp(i,j) for(long long i=0;i<j;i++)
#define rep(i,j,k) for(long long i=j;i<k;i++)
#define mod 1000000007
#define NIL -1
#define MAX 50
const int INFTY=(1<<21);
typedef long long ll;
int H,W;
int c[MAX][MAX];
int checked[MAX][MAX]={0};
void dfs(int x,int y){
    if(x<0||x>=H||y<0||y>=W||c[x][y]==0)return;
    if(checked[x][y]==1)return;
    checked[x][y]=1;
    rep(i,-1,2){
        rep(j,-1,2){
            dfs(x+i,y+j);
        }
    }
}
int main(){
    while(1){
        int ans=0;
        cin>>W>>H;
        if(H==0&&W==0)break;
        else {
            rp(i,H){
                rp(j,W){
                    cin>>c[i][j];
                    checked[i][j]=0;
                }
            }
            rp(i,H){
                rp(j,W){
                    if(c[i][j]==1&&checked[i][j]==0){
                        dfs(i,j);
                        ans++;
                    }
                }
            }
        }
        cout<<ans<<endl;
    }
}

C++: #include<iostream>
#include<algorithm>
using namespace std;
int H,W,k[100][100],n[100][100],color=1,i,j,l,m,o,r;
int main(){cin>>H>>W;for(i=0;i<H;i++){for(j=0;j<W;j++){cin>>k[i][j];}}
for(i=0;i<H;i++){for(j=0;j<W;j++){if(r==1){color++;}r=0;if(k[i][j]==1 && n[i][j]==0){n[i][j]=color;for(l=0;l<100;l++){
	for(m=0;m<H;m++){for(o=0;o<W;o++){if(n[m][o]==color){
		for(int p=-1;p<=1;p++){for(int q=-1;q<=1;q++){if(m-p>=0 && m-p<H){if(o-q>=0 && o-q<W){n[m][o]=color;}}}}
	}}}
}}}}
cout<<color<<endl;
}

C++: #include<iostream>
#include<algorithm>
#include<vector>
#include<string>
#include<utility>
#include<map>
#include<set>
#include<queue>
using namespace std;
#define N (1000000000+7)
typedef pair<int,int> P;
typedef long long ll;

int h,w;
int board[60][60];
bool check(int i,int j){
    return (i>=1)&&(i<=h)&&(j>=1)&&(j<=w)&&(board[i][j]==1);
}

void dfs(int i,int j){
    if(check(i,j)){
        board[i][j]=-1;
        for(int x=i-1;x<=i+1;++x){
            for(int y=j-1;y<=j+1;++y){
                dfs(x,y);
            }
        }
    }
    else return;
}

int main(void){
    while(true){
        cin>>w>>h;
        int sx=0,sy=0;
        int ans=0;
        if((h==0)&&(w==0))break;
        for(int i=1;i<=h;i++){
            for(int j=1;j<=w;j++){
                cin>>board[i][j];
            }
        }
        for(int i=1;i<=h;i++){
            for(int j=1;j<=w;j++){
                if(board[i][j]==1){
                    dfs(i,j);
                    ans++;
                }
            }
        }
        cout<<ans<<endl;
    }

    return 0;
}

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

vector<int> xw = {-1,  0,  1, -1,  1, -1,  0,  1 };
vector<int> yh = {-1, -1, -1,  0,  0,  1,  1,  1 };
void dfs( vector<vector<int>> &vec, int X, int Y, int &w, int &h, vector<vector<bool>> &ck ){
  if ( ck.at(Y).at(X) ) return;
  
  ck.at(Y).at(X) = true;
  for (int i=0; i<8; i++) {
    int x = X + xw.at(i);
    int y = Y + yh.at(i);
    if( x < 0 || w <= x ) continue;
    if( y < 0 || h <= y ) continue;
    if ( vec.at(y).at(x) ) dfs( vec, x, y, w, h, ck);
  }
}

int main() {
  
  while (true) {
  int W,H;
  cin >> W >> H;
  if( W == 0 || H == 0) break;
    
  vector<vector<int>> P(H, vector<int>(W));
  for (int i=0; i<H; i++) {
    for (int j=0; j<W; j++) {
      cin >> P.at(i).at(j);  
    }
  }
  
  vector<vector<bool>> checked(H, vector<bool>(W, false));
  int res = 0;
  for (int h=0; h<H; h++) {
    for (int w=0; w<W; w++) {
      if( !checked.at(h).at(w) && P.at(h).at(w) ) {
         res++; dfs( P, w, h, W, H, checked);
      }
    }
  }
  
  cout << res << endl;
    
  }
  return 0;
}

C++: #include <iostream>
#include <cstdlib>
#include <queue>
#include <math.h>
#include <vector>
#include <algorithm>
#include <string>
#include <bitset>
using namespace std;

vector<vector<int>> c;
int w, h;

void search(int i, int j){
    if(i-1 >= 0 && j-1 >= 0 && c[i-1][j-1] == 1){
        c[i-1][j-1] = 0;
        search(i-1, j-1);
    }
    if(j-1 >= 0 && c[i][j-1] == 1){
        c[i][j-1] = 0;
        search(i, j-1);
    }
    if(i+1 < h && j-1 >= 0 && c[i+1][j-1] == 1){
        c[i+1][j-1] = 0;
        search(i+1, j-1);
    }
    if(i-1 >= 0 && c[i-1][j] == 1){
        c[i-1][j] = 0;
        search(i-1, j);
    }
    if(i+1 < h && c[i+1][j] == 1){
        c[i+1][j] = 0;
        search(i+1, j);
    }
    if(i-1 >= 0 && j+1 < w && c[i-1][j+1] == 1){
        c[i-1][j+1] = 0;
        search(i-1, j+1);
    }
    if(j+1 < w && c[i][j+1] == 1){
        c[i][j+1] = 0;
        search(i, j+1);
    }
    if(i+1 < h && j+1 < w && c[i+1][j+1] == 1){
        c[i+1][j+1] = 0;
        search(i+1, j+1);
    }
    return;
}

int main()
{
    int ans;
    while(true){
        cin >> w >> h;
        if(w == 0 && h == 0){
            break;
        }
        // init
        ans = 0;
        c.resize(h);
        for(int i=0; i<h; ++i){
            c[i].resize(w);
            for(int j=0; j<w; ++j){
                cin >> c[i][j];
            }
        }
        // search
        for(int i=0; i<h; ++i){
            for(int j=0; j<w; ++j){
                if(c[i][j] == 1){
                    ans++;
                    c[i][j] = 0;
                    search(i, j);
                }
            }
        }
        // output
        cout << ans << endl;
        // clear
        for(int i=0; i<h; ++i){
            c[i].clear();
        }
        c.clear();
    }
    return 0;
}