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 <algorithm>
# include <vector>
# include <string>
# include <set>
# include <map>
# include <cmath>
# include <iomanip>
# include <functional>
# include <utility>
# include <stack>
# include <queue>
# include <list>
using namespace std;
using LL = long long;
constexpr int MOD = 1000000000 + 7;
//?¶????????????????????????????????????§??????????????????????¢????????????????
constexpr int INF = 2000000000;
const double PI = acos(-1);

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

void dfs(int hpos, int wpos) {
	maze[hpos][wpos] = 0;
	for (int x = -1; x <= 1; x++) {
		for (int y = -1; y <= 1; y++) {
			int nx = wpos + x;
			int ny = hpos + y;
			if (0 <= nx&&nx < w && 0 <= ny&&ny < h&&maze[ny][nx] == 1)dfs(ny, nx);
		}
	}
}

int main() {
	while (cin >> w >> h&&w&&h) {
		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) {
					ans++;
					dfs(i, j);
				}
			}
		}
		cout << ans << endl;
	}

}

C++: #include <iostream>
using namespace std;
int w,h;
int in[100][100];
bool seen[100][100];
int ans =0;
bool flag = false;

void solve(int i,int j){
    //cout << i << " " << j << endl;
    if(i < 0 || j < 0 || i >= h || j >= w){
        return;
    }
    if(seen[i][j]){
        return;
    }
    if(in[i][j] == 0){
        return;
    }
    seen[i][j] = true;
    solve(i-1,j-1);
    solve(i-1,j);
    solve(i-1,j+1);
    solve(i,j-1);
    solve(i,j+1);
    solve(i+1,j-1);
    solve(i+1,j);
    solve(i+1,j+1);

}
int main(){
    cin >> w >> h;
    for(int i=0;i<h;i++){
        for(int j=0;j<w;j++){
            cin >> in[i][j];
        }
    }
    for(int i=0;i<h;i++){
        for(int j=0;j<w;j++){
            if(seen[i][j] == true || in[i][j] == 0){
            }
            else{
                solve(i,j);
                ans++;
            }
            
        }
    }
    if(w != 0 && h != 0){
        cout << ans << endl;
    }
    return 0;
}

C++: #include <iostream>
#define MAX 70
#define rep(i,p) for(int i=0;i<p;i++)
#define pb push_back
using namespace std;
int w,h,ans,yoko,tate;
int hoge;
bool c[MAX][MAX];
bool visit[MAX][MAX];
// ?§???? {?????????????????????????????????????????????????????????}
const int dx[] = {1,0,-1,0,1,1,-1,-1};
const int dy[] = {0,-1,0,1,1,-1,1,-1};

bool dfs (int x, int y) {
  if(!c[x][y]) return false;
  visit[x][y] = true;
  rep(i,8) {
    yoko = x+dx[i];
    tate = y+dy[i];
    if ( c[yoko][tate] and !visit[yoko][tate] ) dfs(yoko,tate);
  }
  return true;
}
int main(){
  while (1) {
    cin >> w >> h;
    if (w == 0 and h == 0) break;
    rep(i,MAX) rep(j,MAX) c[i][j] = false;
    rep(i,MAX) rep(j,MAX) visit[i][j] = false;
    ans = 0;
    // ?????? 0 false
    // ?????? 1 true
    // ??§?¨? [1,w]x[1,h]????????´??????(false)
    for(int i=1;i<=h;i++)
      for(int j=1;j<=w;j++) {
        cin >> hoge;
        if (hoge == 1) c[i][j] = true;
      }
    for(int i=1;i<=h;i++)
      for(int j=1;j<=w;j++)
        if (!visit[i][j])
          // dfs ????????¢?´¢???????????\???????????¨??????false?????????
          if (dfs(i,j)) ans++;
    cout << ans << endl;
  }
  return 0;
}

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

int w,h;
int maps[50][50];

void dfs(int y, int x)
{
  int dx[]={1,-1,0,0};
  int dy[]={0,0,1,-1};

  maps[y][x]=0;

  for(int i=0;i < 4;i++){
    for(int j=0;j < 4;j++){
      int xx = x + dx[j];
      int yy = y + dy[i];
      if(maps[yy][xx] == 1 && xx <= 0 && xx < w && yy <= 0 && yy < h) dfs(yy,xx);
    }
  }
}

int main(){
  while(cin >> w >> h && w)
    {
      for(int i=0;i < h;i++){
	for(int j=0;j < w;j++){
	  cin >> maps[i][j];
	}
      }
      int island=0;

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

C++: #include <iostream>
#include <algorithm>
#include <string>
#include <string.h>
#include <stdlib.h>
#include <math.h>
#include <vector>
#include <queue>
#include <stack>
#include <cmath>
#include <map>
#include <iomanip>
#include <set>
#include <ctime>
#include <tuple>
#include <bitset>
using namespace std;

/*
sort(sp.begin(), sp.end(), [](PAIR l, PAIR r){
        return l.first<r.first || (l.first==r.first && l.second > r.second);
        });
*/

typedef long long  ll;

#define fi first
#define se second

using Graph = vector<vector<int>>;

vector<int>dx={-1,-1,-1,0,0,1,1,1};
vector<int>dy={-1,0,1,-1,1,-1,0,1};

void dfs(vector<vector<int>>&c,vector<vector<int>>&d, int x, int y,int w,int h){
    d[x][y]=1;
    for(int i=0;i<8;i++){
        int newx=x+dx[i];
        int newy=y+dy[i];
        if(newx<0 || newx>=h || newy<0 || newy>=w)continue;
        if(d[newx][newy]==0 && c[newx][newy]==1){
            dfs(c,d,newx,newy,w,h);
        }
    }
}

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


    return 0;
}

C++: // Tazoe

#include <iostream>
using namespace std;

void DFS(int M[52][52], int y, int x)
{
	M[y][x] = 0;

	if(M[y-1][x-1]==1)
		DFS(M, y-1, x-1);
	if(M[y-1][x]==1)
		DFS(M, y-1, x);
	if(M[y-1][x+1]==1)
		DFS(M, y-1, x+1);
	if(M[y][x-1]==1)
		DFS(M, y, x-1);
	if(M[y][x+1]==1)
		DFS(M, y, x+1);
	if(M[y+1][x-1]==1)
		DFS(M, y+1, x-1);
	if(M[y+1][x]==1)
		DFS(M, y+1, x);
	if(M[y+1][x+1]==1)
		DFS(M, y+1, x+1);
}

int main()
{
	while(true){
		int w, h;
		cin >> w >> h;

		if(w==0&&h==0)
			break;

		int M[52][52];
		for(int y=0; y<=h+1; y++)
			for(int x=0; x<=w+1; x++)
				M[y][x] = 0;

		for(int y=1; y<=h; y++)
			for(int x=1; x<=w; x++)
				cin >> M[y][x];

		int cnt = 0;

		for(int y=1; y<=h; y++)
			for(int x=1; x<=w; x++)
				if(M[y][x]==1){
					DFS(M, y, x);

					cnt++;
				}

		cout << cnt << endl;
	}

	return 0;
}