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 <iostream>
#include <vector>
#include <algorithm>

typedef long long ll;
using namespace std;

int H, W;
vector<vector<int>> C;
vector<vector<int>> visited;
int ans = 0;





void dfs(int &i, int &j){
    visited[i][j] = 1;
    for(int y=-1; y<=1; y++){
        for(int x=-1; x<=1; x++){
            int ny = i+y;
            int nx = j+x;
            
            if(ny>=0 && ny<H && nx>=0 && nx<W && C[ny][nx] && !visited[ny][nx]) dfs(ny, nx);
        }
    }
    return;
}




int main() {
    while(1){
        ans = 0;
        visited.clear();
        
        cin >> W >> H; if((W&H)==0) break;
        C.resize(H);
        for(vector<int> &v:C) v.resize(W);
        for(vector<int> &row:C)
            for(int &c:row) cin >> c;

        visited.resize(H);
        for(vector<int> &v:visited) v.resize(W);

        for(int i=0; i<H; i++){
            for(int j=0; j<W; j++){
                if(C[i][j] && !visited[i][j]){
                    dfs(i,j);
                    ans ++;
                }
            }
        }
        
        
        cout << ans << endl;
    }
    return 0;
}

C++: #include<bits/stdc++.h>
#define rep(i,n) for(int i = 0; i < n; ++i)
#define rrep(i,n) for(int i = 1; i <= n; ++i)
using namespace std;
//using ll=long long;
const int MX = 2200000;
//const ll LINF = 1000000000000000000ll;
//const long long MOD = 1000000007;
//long long INF = 90000000001000;
int w,h;
bool seen[50][50];
int c[50][50];
int d[2]={1,-1};
void dfs(int i,int j){
    seen[i][j]=true;
    for (int k = 0; k < 2; ++k)
    {
       if (!seen[i+d[k]][j]&&i+d[k]<h&&i+d[k]>=0&&c[i+d[k]][j])dfs(i+d[k],j);
       if (!seen[i][j+d[k]]&&j+d[k]<w&&j+d[k]>=0&&c[i][j+d[k]])dfs(i,j+d[k]);
       if (!seen[i+d[k]][j-d[k]]&&i+d[k]<h&&i+d[k]>=0&&j-d[k]>=0&&j-d[k]<w&&c[i+d[k]][j-d[k]])dfs(i+d[k],j-d[k]);
       if (!seen[i+d[k]][j+d[k]]&&i+d[k]<h&&i+d[k]>=0&&j+d[k]<w&&j+d[k]>=0&&c[i+d[k]][j+d[k]])dfs(i+d[k],j+d[k]); 
    }

}


int main(){


while(1){
cin >> w >> h;
if(w==0&&h==0) break;
for (int i = 0; i < h; ++i)
{
   for (int j = 0; j < w; ++j)
   {
       cin >> c[i][j];
       seen[i][j]=false;
   }
}
int cnt=0;
for (int i = 0; i < h; ++i)
{
  for (int j = 0; j < w; ++j)
  {
    if(c[i][j]==1&&!seen[i][j]){cnt++;dfs(i,j);}
  }
}
cout << cnt << endl;
}

return 0;

}

C++: #include<cstdio>
#include<stack>

using namespace std;

struct Coord {
  int row;
  int col;
};

int solve (int W, int H);
  
int main () {
  int W, H;

  while(1)
    {
      scanf("%d%d", &W, &H);
      if(W==0 && H==0) break;	
      printf("%d\n", solve(W,H));	
    }
}

int solve (int W, int H) {
  int n=0;
  char M[H+2][W+2];
  Coord start;
  for (int i=0; i<=W+1; i++) { M[0][i] = '0'; M[H+1][i] = '0'; }
  for (int i=1; i<=H; i++) { M[i][0] = '0'; M[i][W+1] = '0'; } 
  for (int i=1; i<=H; i++) {
    for (int j=1; j<=W; j++) {
      scanf(" %c", &M[i][j]);
    }
  }
  for(int i=1 ; i<=H ; i++) {
    for(int j=1 ; j<=W ; j++){
      if(M[i][j]=='1'){	
        start.row=i;
        start.col=j;
        M[start.row][start.col]='@';

        stack<Coord> S;
        S.push(start);
        while (!S.empty()) {
          Coord current = S.top(); S.pop();
          int D1[]={1,1,0,-1,-1,-1,0,1}; int D2[]={0,-1,-1,-1,0,1,1,1};
          for (int d=0; d<8; d++) {
            Coord nc = current;
            nc.row += D1[d];
            nc.col += D2[d];
            if(M[nc.row][nc.col]!='0' && M[nc.row][nc.col]!='*' && M[nc.row][nc.col]!='@')
	      {
	        M[nc.row][nc.col] = '*';
	        S.push(nc);
	      }
          } 
        }
        n++;
      }
    }
  }
  return n;
}

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

int fill(int field[50][50], int x, int y, int w, int h){
    if(field[x][y] == 1){
        field[x][y] = 0;
        if(x+1<w) fill(field, x+1, y, w, h);
        if(y+1<h) fill(field, x, y+1, w, h);
        if(x-1>=0) fill(field, x-1, y, w, h);
        if(y-1>=0) fill(field, x, y-1, w, h);
        if(x+1<w && y+1<h) fill(field, x+1, y+1, w, h);
        if(x+1<w && y-1>=0) fill(field, x+1, y-1, w, h);
        if(x-1>=0 && y+1<h) fill(field, x-1, y+1, w, h);
        if(x-1>=0 && y-1>=0) fill(field, x-1, y-1, w, h);
        return 1;
    }
    return 0;
}

int main(){
    int w, h;
    int field[50][50];
    int result;
    cin >> w >> h;
    while(w || h){
        result = 0;
        for(int i=0;i<h;++i){
            for(int j=0;j<w;++j){
                cin >> field[j][i];
            }
        }
        for(int i=0;i<h;++i){
            for(int j=0;j<w;++j){
                result += fill(field, j, i, w, h);
            }
        }
        cout << result << endl;
        cin >> w >> h;
    }
}

C++: #include <iostream>
#include <cmath>
#include <vector>

using namespace std;

#define loop(i,a,b) for(int i=(a);i<int(b);i++)
#define rep(i,b) loop(i,0,b)

int c[200][200];
int vis[200][200];
int w,h;


void dfs(int ci, int cj){
    vis[ci][cj] = true;
    int di[] = {0,1,0,-1, 1,1,-1,-1};
    int dj[] = {1,0,-1,0, -1,1,-1,1};
    rep(d,8){
        int ni = ci+di[d];
        int nj = cj+dj[d];
        if(ni < 0 || nj < 0) continue;
        if(ni >= h || nj >= w) continue;
        if(vis[ni][nj]) continue;
        if(c[ni][nj]==0) continue;
        dfs(ni,nj);
    }
}

int main(){
    while(cin >> w >> h){
        rep(i,h)rep(j,w) cin >> c[i][j], vis[i][j] = 0;
        int ans = 0;
        rep(i,h)rep(j,w){
            if(vis[i][j]) continue;
            if(c[i][j] == 0) continue;
            dfs(i,j);
            ans++;
        }
        cout << ans << endl;
    }
}