Go: package main

import (
	"fmt"
)

func main() {
	var h, w int
	for {
		fmt.Scan(&w, &h)
		if w == 0 && h == 0 {
			return
		}
		matrix := make([][]int, h)
		Sean := make([][]bool, h)
		for i := 0; i < h; i++ {
			matrix[i] = make([]int, w)
			Sean[i] = make([]bool, w)
			for j := 0; j < w; j++ {
				fmt.Scan(&matrix[i][j])
			}
		}
		var count int
		for i := 0; i < h; i++ {
			for j := 0; j < w; j++ {
				if matrix[i][j] == 1 && Sean[i][j] == false {
					search(i, j, h, w, matrix, Sean)
					count++
				}
			}
		}
		fmt.Println(count)
	}
}
func search(x int, y int, h int, w int, matrix [][]int, Sean [][]bool) {
	if x < 0 || y < 0 || x >= h || y >= w || matrix[x][y] == 0 {
		return
	}
	if Sean[x][y] == true {
		return
	}

	Sean[x][y] = true

	search(x+1, y, h, w, matrix, Sean)
	search(x-1, y, h, w, matrix, Sean)
	search(x, y+1, h, w, matrix, Sean)
	search(x, y-1, h, w, matrix, Sean)
	search(x+1, y-1, h, w, matrix, Sean)
	search(x-1, y+1, h, w, matrix, Sean)
	search(x+1, y+1, h, w, matrix, Sean)
	search(x-1, y-1, h, w, matrix, Sean)
}

C++: #include <iostream>
#include <stdio.h>
#include <algorithm>
#include <math.h>
#include <vector>
#include <string>
#include <sstream>
#include <numeric>
#include <set>
#include <map>

#define rep(i, j) for(int i = 0; i < j; i++)
#define all(i) i.begin(), i.end()
#define ll long long
#define bl bool
#define nn printf("\n");
#define mod 1000000007

using namespace std;

int w, h, cnt = 0;

void srch(int x, int y, vector<vector<int>> &inp, vector < vector <bool>> &hit)
{
	if (x < 0 || w <= x || y < 0 || h <= y || inp[y][x] == 0) { return; }
	if (hit[y][x]) return;
	hit[y][x] = true;
	cnt += 1;
	srch(x + 1, y, inp, hit);
	srch(x - 1, y, inp, hit);
	srch(x, y + 1, inp, hit);
	srch(x, y - 1, inp, hit);
	srch(x + 1, y + 1, inp, hit);
	srch(x + 1, y - 1, inp, hit);
	srch(x - 1, y + 1, inp, hit);
	srch(x - 1, y - 1, inp, hit);
}
int main()
{
	cin.tie(0);
	ios::sync_with_stdio(false);
	while (cin >> w >> h && w != 0 && h != 0)
	{
		vector<vector<int>> inp(h, vector<int>(w, 0));
		vector<vector<bool>> hit(h, vector<bool>(w, false));
		int ans = 0;
		rep(y, h) rep(x, w){ cin >> inp[y][x]; }
		rep(y, h) rep(x, w)
		{
			srch(x, y, inp, hit);
			if (cnt > 0) { ans += 1; }
			cnt = 0;
		}
		printf("%d\n", ans);
	}

	return 0;
}

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

#define REP(i,n) for(int i=0;i<(n);i++)
#define ALL(v) (v).begin(),(v).end()
#define pb push_back
typedef long long ll;
typedef pair<int,int> P;

template<typename A,typename B> inline void chmin(A &a,B b){if(a>b) a=b;}
template<typename A,typename B> inline void chmax(A &a,B b){if(a<b) a=b;}

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

//-----------------------------------------------------------------------

int w,h;
int M[55][55];

void dfs(int y,int x)
{
    M[y][x]=0;
    REP(i,8){
        int ny=y+dy[i],nx=x+dx[i];
        if(0<=ny && ny<h && 0<=nx && nx<w && M[ny][nx]) dfs(ny,nx);
    }
}   

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

    while(cin>>w>>h,w|h)
    {
        REP(i,h) REP(j,w) cin>>M[i][j];

        int cnt=0;
        REP(i,h) REP(j,w){
            if(M[i][j]){
                cnt++;
                dfs(i,j);
            }
        }
        cout<<cnt<<endl;
    }
}

C++: #include<iostream>
#include<cmath>
#include<vector>
#include<string>
typedef unsigned long long ull;
#define rep(i,a) for(int i=0;i<a;i++)
#define loop(i,a,b) for(int i=a;i<b;i++)
using namespace std;
const double eps = 1e-10;
const double pi = acos(-1);
const double inf = (int)1e8;

char maze[50][50];
int H,W;

int dfs(int x,int y){
  if(x<0 || H<=x || y<0 || W<=y)return 0;
  int cnt = 0;
  maze[x][y] = '0';
  int dx[] = { 1, 1, 1,-1,-1,-1, 0, 0};
  int dy[] = {-1, 0, 1,-1, 0, 1, 1,-1};
  rep(i,8){
    // if(i==j&&i==0)continue;
    if(maze[x+dx[i]][y+dy[i]]=='1')dfs(x+dx[i],y+dy[i]);
  }
  return cnt;
}
void solve(){
  int h,w;
  while(cin>>W>>H,H+W){
    rep(i,H)rep(j,W)cin>>maze[i][j];
    int res=0;
    rep(i,H)rep(j,W){
      //rep(k,H){rep(l,W)cout<<maze[k][l];cout<<endl;}
      //cout<<endl;
      if(maze[i][j]=='1'){
	dfs(i,j);
	res++;
      }
    }
    cout<<res<<endl;
  }
}

int main(void){
  solve();
  return 0;
}

C++: #include <bits/stdc++.h>
using namespace std;
#define rep(i, n) for(int i = 0; i < n; i++)
#define rep2(i, x, n) for(int i = x; i <= n; i++)
#define rep3(i, x, n) for(int i = x; i >= n; i--)
#define elif else if
#define sp(x) fixed << setprecision(x)
#define pb push_back
#define eb emplace_back
#define all(x) x.begin(), x.end()
#define sz(x) (int)x.size()
using ll = long long;
using ld = long double;
using pii = pair<int, int>;
using pil = pair<int, ll>;
using pli = pair<ll, int>;
using pll = pair<ll, ll>;
const ll MOD = 1e9+7;
//const ll MOD = 998244353;
const int inf = (1<<30)-1;
const ll INF = (1LL<<60)-1;
const ld EPS = 1e-10;
template<typename T> bool chmax(T &x, const T &y) {return (x < y)? (x = y, true) : false;};
template<typename T> bool chmin(T &x, const T &y) {return (x > y)? (x = y, true) : false;};

int H, W;
int c[50][50];

int dfs(int x, int y){
    c[x][y] = 0;
    rep2(i, -1, 1){
        rep2(j, -1, 1){
            int nx = x+i, ny = y+j;
            if(nx < 0 || nx >= H || ny < 0 || ny >= W) continue;
            if(c[nx][ny] == 0) continue;
            dfs(nx, ny);
        }
    }
    return 1;
}

int main(){
    while(true){
        cin >> W >> H;
        if(W == 0 && H == 0) break;
        rep(i, H){
            rep(j, W) cin >> c[i][j];
        }
        int ans = 0;
        rep(i, H){
            rep(j, W){
                if(c[i][j]) ans += dfs(i, j);
            }
        }
        cout << ans << endl;
    }
}

C++: #include<iostream>
using namespace std;
int ans;
int map[100][100];
void dfs(int x,int y,int w,int h);
int dx[8] = {1,1,0,-1,-1,-1,0,1},dy[8] = {0,1,1,1,0,-1,-1,-1};

int main(){
  int w,h;
  while(cin>>w>>h){
    ans=0;
    if(w == 0 && h == 0)break;
    for(int i = 0;i<h;i++)
      for(int j = 0;j<w;j++) 
	cin>>map[i][j];
    for(int i = 0;i<h;i++)
      for(int j = 0;j<w;j++){
	if(map[i][j] == 1){
	  ans++;
	  dfs(i,j,w,h);
	  // for(int a=0;a<h;a++){
	  //   for(int b=0;b<w;b++) cout<<map[a][b];
	  // cout<<endl;
	  // }
	  // cout<<endl;
	}
      }
    cout<<ans<<endl;
  }
  return 0;
}

void dfs(int x,int y,int w,int h){
  //cout<<"LK+LKJ"<<endl;
  int nx,ny;
  for(int i = 0;i<8;i++){
    nx = x+dx[i]; ny = y+dy[i];
    if(nx>=0 && nx<w && ny>=0 && ny<h && map[ny][nx] == 1){
      map[ny][nx] = 0;
      dfs(nx,ny,w,h);
    }
  }
}

C++: #include <stdio.h>
#include <cmath>
#include <algorithm>
#include <stack>

using namespace std;

int map[50][50],width,height;
int dx[8] = {-1,0,1,-1,1,-1,0,1},dy[8] = {-1,-1,-1,0,0,1,1,1};

bool rangeCheck(int row,int col){
	if(row >= 0 && row <= height-1 && col >= 0 && col <= width-1)return true;
	else{
		return false;
	}
}

void erase(int row,int col){
	map[row][col] = 0;

	for(int i = 0; i < 8; i++){
		if(rangeCheck(row+dy[i],col+dx[i]) == true && map[row+dy[i]][col+dx[i]] == 1)erase(row+dy[i],col+dx[i]);
	}
}


int main(){

	int count;

	while(true){
		scanf("%d %d",&width,&height);
		if(width == 0 && height == 0)break;

		for(int i = 0; i < height; i++){
			for(int k = 0; k < width; k++){
				scanf("%d",&map[i][k]);
			}
		}

		count = 0;

		for(int row = 0; row < height; row++){
			for(int col = 0; col < width; col++){
				if(map[row][col] == 1){
					count++;
					erase(row,col);
				}
			}
		}


		printf("%d\n",count);

	}


	return 0;
}