Go: package main

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

func getScanner(fp *os.File) *bufio.Scanner {
	scanner := bufio.NewScanner(fp)
	scanner.Split(bufio.ScanWords)
	scanner.Buffer(make([]byte, 1000005), 1000005)
	return scanner
}

func getNextString(scanner *bufio.Scanner) string {
	scanner.Scan()
	return scanner.Text()
}

func getNextInt(scanner *bufio.Scanner) int {
	i, _ := strconv.Atoi(getNextString(scanner))
	return i
}

func getNextInt64(scanner *bufio.Scanner) int64 {
	i, _ := strconv.ParseInt(getNextString(scanner), 10, 64)
	return i
}

func getNextUint64(scanner *bufio.Scanner) uint64 {
	i, _ := strconv.ParseUint(getNextString(scanner), 10, 64)
	return i
}

func getNextFloat64(scanner *bufio.Scanner) float64 {
	i, _ := strconv.ParseFloat(getNextString(scanner), 64)
	return i
}

func main() {
	fp := os.Stdin
	wfp := os.Stdout
	cnt := 0

	if os.Getenv("MASPY") == "ますピ" {
		fp, _ = os.Open(os.Getenv("BEET_THE_HARMONY_OF_PERFECT"))
		cnt = 0
	}
	if os.Getenv("MASPYPY") == "ますピッピ" {
		wfp, _ = os.Create(os.Getenv("NGTKANA_IS_GENIUS10"))
	}

	scanner := getScanner(fp)
	writer := bufio.NewWriter(wfp)
	for solve(scanner, writer) {

	}
	for i := 0; i < cnt; i++ {
		fmt.Fprintln(writer, "-----------------------------------")
		solve(scanner, writer)
	}
	writer.Flush()
}

func solve(scanner *bufio.Scanner, writer *bufio.Writer) bool {
	sol := solver{}
	sol.w = getNextInt(scanner)
	sol.h = getNextInt(scanner)
	if sol.w == 0 && sol.h == 0 {
		return false
	}
	sol.land = makeInts(sol.h, sol.w)
	for y := 0; y < sol.h; y++ {
		for x := 0; x < sol.w; x++ {
			sol.land[y][x] = getNextInt(scanner)
		}
	}
	ans := 0
	for y := 0; y < sol.h; y++ {
		for x := 0; x < sol.w; x++ {
			if sol.land[y][x] == 0 {
				continue
			}
			ans++
			sol.dfs(y, x)
		}
	}
	fmt.Fprintln(writer, ans)
	return true
}

type solver struct {
	w, h int
	land [][]int
}

func (sol *solver) dfs(y, x int) {
	if y < 0 {
		return
	}
	if y >= sol.h {
		return
	}
	if x < 0 {
		return
	}
	if x >= sol.w {
		return
	}
	if sol.land[y][x] == 0 {
		return
	}
	sol.land[y][x] = 0
	dy := [8]int{-1, -1, -1, 0, 0, 1, 1, 1}
	dx := [8]int{-1, 0, 1, -1, 1, -1, 0, 1}
	for i := 0; i < 8; i++ {
		sol.dfs(y+dy[i], x+dx[i])
	}
}
func makeInts(h, w int) [][]int {
	index := make([][]int, h, h)
	data := make([]int, h*w, h*w)
	for i := 0; i < h; i++ {
		index[i] = data[i*w : (i+1)*w]
	}
	return index
}

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

int x,y;
int ti[51][51];
const int dx[]={1,0,-1,0,-1,1,-1,1}, dy[]={0,-1,0,1,-1,1,1,-1};

bool valid(int i,int j)
{
	return(i>=0&&i<y&&j>=0&&j<x);
}

int bfs(int m,int n)
{
	if(ti[m][n]==0)
      return 0;
    queue< pair<int,int> > Q;
	pair<int,int> a,b;
	a.first=m;
	a.second=n;
	Q.push(a);
	while(!Q.empty())
	{
		a=Q.front();
		Q.pop();
		for(int i=0;i<8;i++)
		{
			b.first=a.first+dx[i];
			b.second=a.second+dy[i];
			if(valid(b.first, b.second) && ti[b.first][b.second])
			{
				ti[b.first][b.second]=0;
				Q.push(b);
			}
		}
	}
	return 1;
}

int main()
{
	while(cin >> x >> y)
	{
		if(!x&&!y)
		  break;
		for(int i=0;i<y;i++)
		   for(int j=0;j<x;j++)
		   {
		   	    cin >> ti[i][j];
		   }
        int sum=0;
        for(int i=0;i<y;i++)
		   for(int j=0;j<x;j++)
              if(bfs(i,j))
              {
                  sum++;
              }
        cout << sum << endl;
	}
}

C++: #define _USE_MATH_DEFINES
#define INF 100000000

#include <iostream>
#include <sstream>
#include <cmath>
#include <cstdlib>
#include <algorithm>
#include <queue>
#include <stack>
#include <limits>
#include <map>
#include <string>
#include <cstring>
#include <set>
#include <deque>
#include <bitset>
#include <list>
#include <cctype>
#include <utility>

using namespace std;

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

static const double EPS = 1e-8;

int stage[50][50];

void dfs(int x,int y,int w,int h){
	stage[y][x] = 0;
	for(int tx=-1;tx<=1;tx++){
		for(int ty=-1;ty<=1;ty++){
			int dx = x + tx;
			int dy = y + ty;
			if(dx < 0 || dx >=w || dy < 0 || dy >= h) continue;

			if(stage[dy][dx]==1){
				dfs(x+tx,y+ty,w,h);
			}
		}
	}
}

int main(){
	int w,h;
	while(~scanf("%d %d",&w,&h)){
		if(w==h && h==0) break;
		for(int y=0;y<h;y++){
			for(int x=0;x<w;x++){
				scanf("%d",(*(stage+y)+x));
			}
		}

		int res=0;
		for(int y=0;y<h;y++){
			for(int x=0;x<w;x++){
				if(stage[y][x]==1){
					dfs(x,y,w,h);
					res++;
				}
			}
		}

		printf("%d\n",res);
	}
}

C++: #pragma GCC optimize ("O3")
#include <iostream>
#include <iomanip>
#include <istream>
#include <ostream>
#include <sstream>
#include <iterator>
#include <vector>
#include <algorithm>
#include <queue>
#include <deque>
#include <list>
#include <stack>
#include <map>
#include <unordered_map>
#include <set>
#include <bitset>
#include <utility>
#include <cmath>
#include <cstdio>
#include <cstring>
#include <string>
#include <ctime>
#include <cctype>
#include <cstdlib>
#define IINF 10e8
#define INF 1<<30
#define MOD 1000000007
#define mod 998244353
#define REP(i, a, n) for (ll i = a; i < (ll)(n); i++)
#define REPE(i, a, n) for (ll i = a; i <= (ll)(n); i++)
#define Endl endl
#define fi first
#define se second
#define pb push_back
#define mp make_pair
#define eb emplace_back
#define mmax(x,y)(x>y?x:y)
#define mmin(x,y)(x<y?x:y)
#define chmax(x,y) x=mmax(x,y)
#define chmin(x,y) x=mmin(x,y)
#define all(x) (x).begin(),(x).end()
#define siz(x) (ll)(x).size()
#define PI acos(-1.0)
using namespace std;
typedef long long int ll;
typedef unsigned long long int ull;
typedef long double ld;
typedef pair<int,int>Pin;
typedef pair<ll,ll>Pll;
template<class T> using V=vector<T>;
long long GCD(long long a, long long b) {return b?GCD(b,a%b):a;}
long long LCM(long long a, long long b) {return a/GCD(a,b)*b;}
int dx[4]={-1,0,1,0};
int dy[4]={0,-1,0,1};
int ddx[8]={-1,0,1,0,1,1,-1,-1};
int ddy[8]={0,-1,0,1,1,-1,1,-1};
ll cmp(pair<ll,ll>a,pair<ll,ll> b){
        if(a.se!=b.se)
        return a.se<b.se;
        else
        return a.fi<b.fi;
}
//----------------------------------------------------------------------
int h,w;
int c[55][55];
bool color[55][55];
int ans=0;

//----------------------------------------------------------------------
int main(){
    cin.tie(0);
    ios::sync_with_stdio(false);
    //-------------------------------  
    //ll begin_time=clock();
    //-------------------------------
    cin>>w>>h;
while(w&&h){
    memset(color,0,sizeof(color));
    ans=0;
    for(int i=0;i<h;i++){
        for(int j=0;j<w;j++){
            cin>>c[i][j];
        }
    }

    for(int i=0;i<h;i++){
        for(int j=0;j<w;j++){
            //cout<<i<<" "<<j<<" "<<color[i][j]<<" "<<c[i][j]<<Endl;
            if(color[i][j])continue;
            if(c[i][j]==0)continue;
            queue<Pin>q;
            ans++;
            q.push(mp(i,j));
            while(!q.empty()){
                Pin tmp=q.front();q.pop();
                int x=tmp.fi;int y=tmp.se;
                for(int i=0;i<8;i++){
                    int nx=x+ddx[i];
                    int ny=y+ddy[i];
                    //cout<<nx<<" "<<ny<<endl;
                    if(nx>=0&&ny>=0&&nx<h&&ny<w){
                        if(color[nx][ny])continue;
                        if(c[nx][ny]==0)continue;
                        color[nx][ny]=1;
                        q.push(mp(nx,ny));
                    }
                }
            }
        }
    }
    cout<<ans<<Endl;
    cin>>w>>h;
}
    //-------------------------------  
    //ll end_time=clock();cout<<"time="<<end_time-begin_time<<"ms"<<endl;
    //-------------------------------
    return 0;
}
//----------------------------------------------------------------------