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 <cstdio>
#include <iostream>
#include <string>
#include <sstream>
#include <stack>
#include <algorithm>
#include <cmath>
#include <queue>
#include <map>
#include <set>
#include <cstdlib>
#include <bitset>
#include <tuple>
#include <assert.h>
#include <deque>
#include <bitset>
#include <iomanip>
#include <limits>
#include <chrono>
#include <random>
#include <array>
#include <unordered_map>
#include <functional>
#include <complex>

template<class T> inline bool chmax(T& a, T b) { if (a < b) { a = b; return 1; } return 0; }
template<class T> inline bool chmin(T& a, T b) { if (a > b) { a = b; return 1; } return 0; }

const long long MAX = 5100000;
const long long INF = 1LL << 60;
const long long mod = 1000000007LL;
//const long long mod = 998244353LL;

using namespace std;
typedef unsigned long long ull;
typedef long long ll;

int dw[8] = { 0,1,1,1,0,-1,-1,-1 };
int dh[8] = { 1,1,0,-1,-1,-1,0,1 };
void bfs(int sh, int sw, vector<vector<int>>& a) {
	queue<pair<int, int>> q;
	a[sh][sw] = 0;
	int H = a.size();
	int W = a[0].size();
	q.emplace(sh, sw);
	while (!q.empty()) {
		int h, w; tie(h, w) = q.front();
		q.pop();
		for (int i = 0; i < 8; i++) {
			int nh = h + dh[i];
			int nw = w + dw[i];
			if (nh < 0 || nh >= H || nw < 0 || nw >= W) continue;
			if (a[nh][nw] == 0) continue;
			a[nh][nw] = 0;
			q.emplace(nh, nw);
		}
	}
}

int main()
{
	/*
	cin.tie(nullptr);
	ios::sync_with_stdio(false);
	*/

	while (1) {
		int W, H; scanf("%d %d", &W, &H);
		if (W == 0 && H == 0) break;
		int res = 0;
		vector<vector<int>> a(H, vector<int>(W)); for (ll i = 0; i < H; i++) for (ll j = 0; j < W; j++) scanf("%d", &a[i][j]);
		for (int i = 0; i < H; i++) {
			for (int j = 0; j < W; j++) {
				if (a[i][j]) {
					res++;
					bfs(i, j, a);
				}
			}
		}
		printf("%d\n", res);
	}
	return 0;
}

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

char mp[55][55];

void dfs(int x, int y, int n, int m) {
	mp[x][y] = '0';

	for (int i = -1;i <= 1;i++) {
		for (int j = -1;j <= 1;j++) {
			int ax = x + i;
			int ay = y + j;
			if (ax >= 0 && ay >= 0 && ax < m&&ay < n&&mp[ax][ay] == '1') {
				dfs(ax, ay,n,m);
			}
		}
	}
	return;
}

int main() {
	int n, m;
	while (cin >> n >> m, n, m) {
		for (int i = 0;i < m;i++)
			for(int j=0;j<n;j++)cin >> mp[i][j];

		int al = 0;
		for (int i = 0;i < m;i++) {
			for (int j = 0;j < n;j++) {
				if (mp[i][j] == '1') {
					dfs(i, j, n, m);
					al++;
				}
			}
		}
		cout << al << endl;
	}
	return 0;
}

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

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

void dfs(int x, int y){
	map[y][x] = 0;
	for(int i = -1; i <= 1; i++){
		for(int j = -1; j <= 1; j++){
			int nx = x + i, ny = y + j;
			if(nx >= 0 && nx < w && ny >= 0 && ny < h && map[ny][nx] == 1)	dfs(nx, ny);
		}
	}
	return;
}

int main(void){
	int cnt;
	while(true){
		cin >> w >> h;
		if(!w && !h)	break;
		for(int i = 0; i < h; i++){
			for(int j = 0; j < w; j++){
				cin >> map[i][j];
			}
		}
		cnt = 0;
		for(int i = 0; i < h; i++){
			for(int j = 0; j < w; j++){
				if(map[i][j] == 1){
					dfs(j, i);
					cnt++;
				}
			}
		}
		cout << cnt << endl;
	}
  return 0;
}

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

int w,h;
int mp[50][50];
void dfs(int y,int x){

  mp[y][x] = 0;
  for(int i=-1;i<=1;i++)
    for(int j=-1;j<=1;j++){
      int ny = y + i;
      int nx = x + j;
      if(nx < 0 || ny < 0 || nx >= w || ny >= h) continue;
      if(mp[ny][nx] == 0) continue;
      dfs(ny,nx);
    }
}

signed main(){
  while(1){
    cin>>w>>h;
    if( w == 0 && h == 0) return 0;
    for(int i=0;i<h;i++)
      for(int j=0;j<w;j++) cin>>mp[i][j];

    int ans = 0;
    for(int i=0;i<h;i++)
      for(int j=0;j<w;j++){
        if(mp[i][j] == 0) continue;
        ans++;
        dfs(i, j);
      }

    cout<<ans<<endl;
  }
  return 0;
}

C++: #include<iostream>
using namespace std;
 
int w, h;
int c[50][50];
int dx[8]={-1,0,1,-1,1,-1,0,1,};
int dy[8]={-1,-1,-1,0,0,1,1,1};
 
void dfs(int x,int y){
  c[y][x]=0;
  for(int i=0;i<8;i++){
    int ny=y+dy[i];
    int nx=x+dx[i];
    if(ny>=0&&ny<h&&nx>=0&&nx<w){
      if(c[ny][nx]==1) dfs(nx, ny);
    }
  }
}
 
int main(){
 
  int ans;
 
  while(cin>>w>>h, w){
 
    for(int i=0;i<h;i++){
      for(int j=0;j<w;j++){
	cin>>c[i][j];
      }
    }
     
    ans=0;
     
    for(int i=0;i<h;i++){
      for(int j=0;j<w;j++){
	if(c[i][j]==0) continue;
	if(c[i][j]==1){
	  ans++;
	  dfs(j, i);
	}
      }
    }
     
    cout<<ans<<endl;
  }
     
     
  return 0;
}

C++: #include <algorithm>
#include <iostream>
#include <limits.h>
#include <stdlib.h>
#include <string>
#include <vector>

#define el endl
#define fd fixed
#define INF INT_MAX/2-1
#define pb push_back

using namespace std;

bool island[50][50];
bool visited[50][50];

void visit(int x, int y) {
  visited[y][x] = true;
  if (x > 0 && island[y][x-1] && !visited[y][x-1]) visit(x-1, y);
  if (x < 49 && island[y][x+1] && !visited[y][x+1]) visit(x+1, y);
  if (y > 0 && island[y-1][x] && !visited[y-1][x]) visit(x, y-1);
  if (y < 49 && island[y+1][x] && !visited[y+1][x]) visit(x, y+1);
  if (x > 0 && y > 0 && island[y-1][x-1] && !visited[y-1][x-1]) visit(x-1, y-1);
  if (x > 0 && y < 49 && island[y+1][x-1] && !visited[y+1][x-1]) visit(x-1, y+1);
  if (x < 49 && y > 0 && island[y-1][x+1] && !visited[y-1][x+1]) visit(x+1, y-1);
  if (x < 49 && y < 49 && island[y+1][x+1] && !visited[y+1][x+1]) visit(x+1, y+1);
}

int main() {
  int w, h, tmp, cnt;
  while (cin >> w >> h, w | h) {
    for (int i = 0; i < 50; i++) {
      for (int j = 0; j < 50; j++) {
        island[i][j] = false;
        visited[i][j] = false;
      }
    }
    cnt = 0;

    for (int i = 0; i < h; i++) {
      for (int j = 0; j < w; j++) {
        if (cin >> tmp, tmp)
          island[i][j] = true;
      }
    }
    for (int i = 0; i < h; i++) {
      for (int j = 0; j < w; j++) {
        if (island[i][j] && !visited[i][j]) {
          visit(j, i);
          cnt++;
        }
      }
    }
    cout << cnt << el;
  }
}