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>
using namespace std;
int n, w;
//vector<vector<int>> c(50,vector<int>(50));
//vector<vector<bool>> visit(50,vector<bool>(50));
int c[50][50] = {};
bool visit[50][50] = {};
void dfs(int x, int y){
    
    if(x < 0 || y < 0 || x >= w || y >= n) return;
    if(visit[x][y] == true) return;

    visit[x][y] = true;
    
    dfs(x+1, y);
    dfs(x-1, y);
    dfs(x, y+1);
    dfs(x, y-1);
    dfs(x+1, y+1);
    dfs(x+1, y-1);
    dfs(x-1, y+1);
    dfs(x-1, y-1);
}


int main(void){

    cin >> n >> w;
    
    while(n != 0 && w != 0){
    
    
        for(int i = 0; i < w; i++){
            for(int j = 0; j < n; j++){
                cin >> c[i][j];
                if(c[i][j] == 0) visit[i][j] = true;
            }
        }
        
        int ans = 0;
        
        for(int i = 0; i < w; i++){
            for(int j = 0; j < n; j++){
                if(!visit[i][j]){
                dfs(i, j);
                ans++;
                }
            }
        }
        
        cout << ans << endl;
        
        for(int i = 0; i < 50; i++){
            for(int j = 0; j < 50; j++){
                visit[i][j] = false;
            }
        }
        
        cin >> n >> w;
    }
}

C++: #define _GLIBCXX_DEBUG

#include <stdio.h>
#include <math.h>
#include <string.h>
#include <stdlib.h>
#include <algorithm>

#include <numeric>
#include <iostream>
#include <string>
#include <vector>
#include <queue>
#include <deque>
#include <stack>
#include <set>
#include <map>
#include <cstdio>
#include <cstdlib>
#include <cassert>
#include <cstring>
#include <cmath>
#include <complex>

#define REP(i,a,b) for(int i=a; i<b; i++)
#define rep(i,n) REP(i,0,n)
#define FOR(i,c) for(typeof((c).begin())i=(c).begin();i!=(c).end(); ++i)

#define EXIST(e,s)((s).find(e)!=(s).end())
#define RESET(a) memset((a),0,sizeof(a))
#define SET(a) memset((a),-1,sizeof(a))

#define ll long long

using namespace std;

int w, h;
int num;
int field[50][50];

int d[8][2] = {{-1,-1},{0,-1},{1,-1},{-1,0},{1,0},{-1,1},{0,1},{1,1}};

void search(int x, int y){
  if(field[y][x] == 1){
    field[y][x] = 0;
    for(int i=0; i<8; i++){
      if(0<=x+d[i][0] && x+d[i][0]<w && 0<=y+d[i][1] && y+d[i][1]<h){
	search(x+d[i][0], y+d[i][1]);
      }
    }
  }
}

int main(void){
  while(cin >> w >> h, w || h){
    num = 0;
    for(int i=0; i<h; i++){
      for(int j=0; j<w; j++){
	cin >> field[i][j];
      }
    }

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

C++: // clang-format off
#include <bits/stdc++.h>
#define int long long
#define main signed main()
// #define main int main()
#define loop(i, a, n) for (int i = (a); i < (n); i++)
#define rep(i, n) loop(i, 0, n)
#define all(v) (v).begin(), (v).end()
#define rall(v) (v).rbegin(), (v).rend()
#define prec(n) fixed << setprecision(n)
#define stlice(from, to) substr(from, (to) - (from) + 1)
#define pb push_back
#define mp make_pair
#define mt make_tuple
#define fi first
#define se second
using namespace std;
using pii = pair<int, int>;
using vi = vector<int>;
using vd = vector<double>;
using vc = vector<char>;
using vb = vector<bool>;
using vs = vector<string>;
using vpii = vector<pii>;
using vvi = vector<vi>;
using vvb = vector<vb>;
using vvpii = vector<vpii>;
template<typename A> using fn = function<A>;
constexpr int INF = sizeof(int) == sizeof(long long) ? 1000000000000000000LL : 1000000000;
constexpr int MOD = 1000000007;
constexpr double PI = acos(-1);
template<typename A, typename B> bool cmin(A &a, const B &b) { return a > b ? (a = b, true) : false; }
template<typename A, typename B> bool cmax(A &a, const B &b) { return a < b ? (a = b, true) : false; }
constexpr bool odd(const int &n) { return n & 1; }
constexpr bool even(const int &n) { return !odd(n); }
template<typename V> constexpr typename V::value_type sum(const V &v) { return accumulate(all(v), 0); }
void solve();
main { solve(); return 0; }
// clang-format on

void solve() {
  int w, h;
  while (cin >> w >> h, w) {
    vvi v(h, vi(w));
    rep(i, h) rep(j, w) cin >> v[i][j];
    int c = 0;
    fn<void(int, int, int)> f = [&](int i, int j, int a) {
      if (i < 0 || j < 0 || i >= h || j >= w) return;
      if (v[i][j]) {
        v[i][j] = 0;
        c += a;
        int di[] = {-1, -1, 0, 1, 1, 1, 0, -1}, dj[] = {0, 1, 1, 1, 0, -1, -1, -1};
        rep(k, 8) f(i + di[k], j + dj[k], 0);
      }
    };
    rep(i, h) rep(j, w) f(i, j, 1);
    cout << c << endl;
  }
}

C++: #include <bits/stdc++.h>
using namespace std;
using ll=long long;
typedef pair<int,int> P;
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; }
int w,h;
int nx,ny;
void dfs(vector<vector<int>> &G,int a,int b){
    G.at(a).at(b)=0;
    
    for(int k=-1;k<=1;k++){
        for(int s=-1;s<=1;l++){
            nx=a+k;
            ny=b+s;
        if(0<=nx&& nx<h && 0<=ny&& ny<w && G.at(nx).at(ny)==1) dfs(G,nx,ny);
        }
    }
    return ;
}



int main(){
    int w,h;
    cin>>w>>h;
    vector<vector<int>>G(h,vector<int>(w));
    for(int i=0;i<h;i++){
        for(int j=0;j<w;j++){
            cin>>G.at(i).at(j);
        }
    }
    int count=0;
    for(int i=0;i<h;i++){
        for(int j=0;j<w;j++){
            if(G.at(i).at(j)==1){
                dfs(G,i,j);
                count++;
            }
        }
    }
cout<<count<<endl;
}

C++: #include <bits/stdc++.h>
#define rep(i, n) for (int i = 0; i < (n); ++i)
using namespace std;
using ll = long long;
using P = pair<int, int>;

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

void dfs(int h, int w, vector<vector<int>> &G, vector<vector<bool>> &S)
{
    // printf("h = %d, w = %d\n", h, w);
    S[h][w] = true;
    rep(i, 8)
    {
        int nh = h + dy[i];
        int nw = w + dx[i];
        if (nh < 0 || nh >= H || nw < 0 || nw >= W)
            continue;
        if (G[nh][nw] == 0 || S[nh][nw])
            continue;
        dfs(nh, nw, G, S);
    }
}

int main()
{
    while (cin >> W >> H)
    {
        if (W == 0 && H == 0)
            break;
        vector<vector<int>> G(H, vector<int>(W));
        vector<vector<bool>> S(H, vector<bool>(W));
        rep(h, H)
        {
            rep(w, W)
            {
                cin >> G[h][w];
            }
        }
        int ans = 0;
        rep(h, H)
        {
            rep(w, W)
            {
                if (G[h][w] == 1 && !S[h][w])
                {
                    dfs(h, w, G, S);
                    ans++;
                }
            }
        }
        cout << ans << endl;
    }
    return 0;
}