Go: package main

import (
	"bufio"
	"fmt"
	"os"
	"strconv"
)

var sc = bufio.NewScanner(os.Stdin)
var route [][]int

func scanInt() int {
	num, _ := strconv.Atoi(scanString())
	return num
}

func scanString() string {
	sc.Scan()
	return sc.Text()
}

func init() {
	sc.Split(bufio.ScanWords)
}

func dfs(h, w int) {
	if route[h][w] == 0 {
		return
	}

	route[h][w] = 0
	for _, x := range []int{-1, 0, 1} {
		for _, y := range []int{-1, 0, 1} {
			if route[h+x][w+y] == 0 {
				continue
			}
			dfs(h+x, w+y)
		}
	}
}

func main() {
	writer := bufio.NewWriter(os.Stdout)
	for {
		w, h := scanInt(), scanInt()
		if w == 0 && h == 0 {
			break
		}

		route = make([][]int, h+2)
		route[0] = make([]int, w+2)
		for i := 1; i <= h; i++ {
			route[i] = make([]int, w+2)
			for j := 1; j <= w; j++ {
				route[i][j] = scanInt()
			}
		}
		route[h+1] = make([]int, w+2)

		total := 0
		for i := 1; i <= h; i++ {
			for j := 1; j <= w; j++ {
				if route[i][j] == 1 {
					total++
					dfs(i, j)
				}
			}
		}
		fmt.Fprintln(writer, total)
	}
	writer.Flush()
}

C++: #include <iostream>
#include <vector>
#include <algorithm>
#include <cmath>
#define rep(i, n) for(int i = 0; i < (int)(n); ++i)
#define REP(i, n) for(int i = 1; i < (int)(n); ++i)
using namespace std;

int w, h;
vector<vector<int>> m(50, vector<int>(50));
vector<vector<bool>> searched(50, vector<bool>(50, false));
vector<int> dx = {1, 1, 1, 0, -1, -1, -1, 0};
vector<int> dy = {-1, 0, 1, 1, 1, 0, -1, -1};

void DFS(int i, int j) {
    if (i < 0 || h <= i || j < 0 || w <= j) return;
    if (m[i][j] == 0) return;
    if (searched[i][j]) return;
    
    searched[i][j] = true;
    rep(k, 8) DFS(i + dx[k], j + dy[k]);
    return;
}

int main() {
    while (true) {
        cin >> w >> h;
        if (w == 0 && h == 0) break;
        rep(i, h) rep(j, w) cin >> m[i][j];
        rep(i, h) rep(j, w) searched[i][j] = false;
        
        int cnt = 0;
        rep(i, h) rep(j, w) {
            if (m[i][j] == 0) continue;
            if (searched[i][j]) continue;
            ++cnt;
            DFS(i, j);
        }
        
        cout << cnt << endl;
    }
    return 0;
}

C++: #include<iostream>
using namespace std;
void dfs(int,int);
int a[50][50]={};

int main(){
  while(1){
    int count=0,w,h;
    cin>>w>>h;
    if(w+h==0)break;
    
    for(int i=0;i<h;i++)
      for(int j=0;j<w;j++)
	cin>>a[i][j];

    for(int i=0;i<h;i++)
      for(int j=0;j<w;j++)
	if(a[i][j]==1){
	  dfs(j,i);
	  count++;
	}
    cout<<count<<endl;  
  }
  return 0;
}

  void dfs(int x,int y){
    a[y][x]=0;
    int nx,ny;
    int dx[8]={1,1,1,0,-1,-1,-1,0};
    int dy[8]={1,0,-1,-1,-1,0,1,1};
    for(int k=0;k<8;k++){
	nx=x+dx[k];
	ny=y+dy[k];

	if(nx>=0&&nx<50&&ny>=0&&ny<50&&a[ny][nx]==1)
	  dfs(nx,ny);
    }
  }

C++: #include <iostream>
#include <string>
#include <algorithm>
#include <vector>
#include <cmath>
#include <cstdlib>
#include <tuple>
using namespace std;
using ll=long long;
using R=double;

int w,h;

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

void dfs(int x, int y){
	field[y][x] = 3;

	for(int i = 0; i < 8; i++){
		int nx = x + dx[i];
		int ny = y + dy[i];
		if(nx >= 0 and nx <= w-1 and ny >= 0 and ny <= h and field[ny][nx]==1){
			dfs(nx, ny);
		}
	}
	return;
}

int main(){
	while(1){
		cin >> w >> h;
		if(w==0 and h==0){
			return 0;
		}
		for(int i = 0; i < h; i++){
			for(int j = 0; j < w; j++){
				cin >> field[i][j];
			}
		}

		int count = 0;

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

C++: #include<iostream>
#include<algorithm>
#include<set>
#include<map>
#include<vector>
#include<queue>
#include<cstring>
#include<cmath>
#include<cstdio>
#include<cstdlib>
using namespace std;
#define REP(i,a,n) for(int i = a ; i < n ; i++)
#define rep(i,n) REP(i,0,n)

typedef long long ll;

int w,h;
int t[52][52];
int dx[] = {-1,0,1,-1,1,-1,0,1};
int dy[] = {-1,-1,-1,0,0,1,1,1};


void solve(int x,int y){
  t[y][x] = 0;
  rep(i,8){
    int nx = x + dx[i];
    int ny = y + dy[i];
    if(w>nx && nx>=0 && h>ny && ny>=0 && t[ny][nx]){
      solve(nx,ny);
    }
  }
}

int main(){
  while(cin>>w>>h,w||h){
    rep(i,h){
      rep(j,w){
        cin>>t[i][j];
      }
    }

    int cnt = 0;
    rep(i,h){
      rep(j,w){
        if(t[i][j]){
          solve(j,i);
          cnt++;
        }
      }
    }
    cout<<cnt<<endl;
  }
}

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

using namespace std;

int c[52][52];

void er(int i,int j){
    c[i][j]--;
    if(c[i+1][j-1])
        er(i+1,j-1);
    else if(c[i][j+1])
        er(i,j+1);
    else if(c[i+1][j])
        er(i+1,j);
    else if(c[i+1][j+1])
        er(i+1,j+1);
    else if(c[i-1][j+1])
        er(i-1,j+1);
    else if(c[i-1][j])
        er(i-1,j);
    else if(c[i-1][j-1])
        er(i-1,j-1);
    else if(c[i][j-1])
        er(i,j-1);
}

int main(void){
    int w,h;
    int i,j;
    int a;
    while(true){
        cin >> w >> h;
        if(w == 0){
            break;
        }
        for(i = 0;i < 52;i++){
            for(j = 0;j < 52;j++){
                c[i][j] = 0;
            }
        }
        for(i = 1;i <= w;i++){
            for(j = 1;j <= h;j++){
                cin >> c[i][j];
            }
        }
        a = 0;
        for(i = 1;i <= w;i++){
            for(j = 1;j <= h;j++){
                if(c[i][j]){
                    er(i,j);
                    a++;
                }
            }
        }
        cout << a << endl;
    }
}

C++: #include <bits/stdc++.h>

using visited = std::vector<std::vector<bool>>;
using marine_map = std::vector<std::vector<int>>;

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

void dfs(int x, int y, visited& v, marine_map& m, int& c) {
    if (v[y][x]) {
        return;
    }
    v[y][x] = true;
    for (int p = 0; p < 8; p++) {
        int cy = y + dy[p];
        int cx = x + dx[p];
        if (cx < 0 || cx >= w || cy < 0 || cy >= h || m[cy][cx] != 1) {
            continue;
        }
        dfs(cx, cy, v, m, c);
    }
}


int main() {
    while (true) {
        std::cin >> w >> h;
        if (w == 0 && h == 0) {
            break;
        }
        c = 0; // reset counter for new map;
        std::vector<std::vector<int>> vec(h, std::vector<int>(w, 0));
        std::vector<std::vector<bool>> bools(h, std::vector<bool>(w, false));
        for (int i = 0; i < h; i++) {
            for (int p = 0; p < w; p++) {
                std::cin >> vec[i][p];
            }
        }
        for (int i = 0; i < h; i++) {
            for (int p = 0; p < w; p++) {
                if (vec[i][p] == 1 && !bools[i][p]) {
                    c++;
                    dfs(p, i, bools, vec, c);
                }
            }
        }
        std::cout << c << std::endl;
    }
}

C++: #include <bits/stdc++.h>

using namespace std;

typedef long long ll;
typedef pair<int,int> P;

int c[100][100];//0??????
int h,w;

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

void bfs(int i,int j){
    queue<P> q;
    q.push(P(i,j));
    c[i][j]=0;
    while(!q.empty()){
        int x=q.front().first,y=q.front().second;
        q.pop();
        for(int i=0;i<8;i++){
            int nx=x+dx[i],ny=y+dy[i];
            if(0<=nx&&nx<h&&0<=ny&&ny<w&&c[nx][ny]==1){
                q.push(P(nx,ny));
                c[nx][ny]=0;
            }
        }
    }
}

int main(){
  cin.tie(0);
  ios::sync_with_stdio(false);

  while(true){
      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];
      }
      int res=0;
      for(int i=0;i<h;i++)for(int j=0;j<w;j++){
          if(c[i][j]==1){
              bfs(i,j);
              res++;
          }
      }
      cout<<res<<endl;
  }
}