Go: package main

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

const (
	initialBufSize = 100000
	maxBufSize     = 1000000
)

var sc *bufio.Scanner

func initScanner(r io.Reader) *bufio.Scanner {
	buf := make([]byte, initialBufSize)

	sc := bufio.NewScanner(r)
	sc.Buffer(buf, maxBufSize)
	sc.Split(bufio.ScanWords) // bufio.ScanLines
	return sc
}

func main() {
	sc = initScanner(os.Stdin)
	resolve()
}

func resolve() {
	results := []int{}
	for true {
		w, h := scanInt(sc), scanInt(sc)
		if w == 0 && h == 0 {
			break
		}
		grid := make([][]int, h)
		for i := 0; i < h; i++ {
			grid[i] = make([]int, w)
			for j := 0; j < w; j++ {
				grid[i][j] = scanInt(sc)
			}
		}
		num := 0
		for i := range loop0(h) {
			for j := range loop0(w) {
				start := pos{y: i, x: j}
				if grid[start.y][start.x] == 0 {
					continue
				}
				num++
				_dfs(grid, w, h, start)
			}
		}
		results = append(results, num)
	}
	for _, v := range results {
		fmt.Println(v)
	}
	return
}

func _dfs(grid [][]int, w, h int, start pos) {
	grid[start.y][start.x] = 0
	for _, df := range []pos{
		pos{1, 0}, pos{-1, 0},
		pos{0, -1}, pos{0, 1},
		pos{1, 1}, pos{-1, -1},
		pos{-1, 1}, pos{1, -1},
	} {
		np := addPos(start, df)
		if np.y < 0 || h <= np.y || np.x < 0 || w <= np.x {
			continue
		}
		if grid[np.y][np.x] == 0 {
			continue
		}
		_dfs(grid, w, h, np)
	}
}

type pos struct{ x, y int }

func addPos(p, d pos) pos {
	return pos{
		x: p.x + d.x,
		y: p.y + d.y,
	}
}

func loop0(n int) chan int {
	return loop(0, n-1, 1)
}

func loop1(n int) chan int {
	return loop(1, n, 1)
}

func loop(s, e, d int) chan int {
	ch := make(chan int)
	go func() {
		for i := s; i <= e; i += d {
			ch <- i
		}
		close(ch)
	}()
	return ch
}

// snip-scan-funcs
func scanInt(sc *bufio.Scanner) int {
	sc.Scan()
	i, _ := strconv.Atoi(sc.Text())
	return int(i)
}
func scanString(sc *bufio.Scanner) string {
	sc.Scan()
	return sc.Text()
}

C++: #include <iostream>
#include <vector>
using namespace std;
#define MAX 50

int c[MAX][MAX];

void dfs(int w, int h, int x, int y);
int main(int argc, char const *argv[]){
    int w, h, ans;

    while (cin>>w>>h, w || h){
        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 < w; i++){
            for (int j = 0; j < h; j++){
                if (c[i][j] == 1){
                    dfs(w, h, i, j);
                    ans++;

                    for (int i = 0; i < w; i++){
                        for (int j = 0; j < h; j++){
                            cout<<c[i][j]<<" ";
                        }
                        cout<<'\n';
                    }
                    cout<<'\n';
                }
            }
        }
        cout<<ans<<endl;
    }

    return 0;
}

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

    for (int i = 0; i < 8; i++){
        int nx = x + dx[i];
        int ny = y + dy[i];

        if (0 <= nx && nx < w && 0 <= ny && ny < h && c[nx][ny] == 1){
            dfs(w, h, nx, ny);
        }
    }
    return;
}

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

int mp[50][50]={0};
int dx[8]={-1,-1,-1,0,0,1,1,1};
int dy[8]={-1,0,1,-1,1,-1,0,1};
 int W,H;
 void dfs(int w,int h);
int main(){
 
    while(true){
        
    int cnt=0;

    cin >>W >>H;
    if(W==0 && H==0) break;
    for (int i=0;i<H;i++){
        for(int j=0;j<W;j++){
            cin >> mp[i][j];
        }
    }
    
//    memcpy(mp1,mp,sizeof(mp));
    
    for (int i=0;i<H;i++){
        for(int j=0;j<W;j++){
            if (mp[i][j]==1){
                cnt++;
                dfs(j,i);
            }
        }
    }
   cout <<cnt<<endl;
    } 

}

void dfs(int w,int h){
    mp[h][w]=0;
    int nx,ny;
    for (int i=0;i<8;i++){
        nx=w+dx[i];ny=h+dy[i];
        if(0<=nx && nx<W && 0<=ny && ny<H && mp[ny][nx]==1){
            dfs(nx,ny);
        }

    }

}

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

int w,h;
int c[50][50];
vector<vector<int> > visited;
int dx[]={1,1,0,-1,-1,-1,0,1};
int dy[]={0,-1,-1,-1,0,1,1,1};

bool dfs(int x,int y){
    if(visited[x][y]) return false;
    visited[x][y]=1;

    for(int d=0;d<8;d++){
        int i=x+dx[d];
        int j=y+dy[d];
        if(0<=i&&i<h&&0<=j&&j<w){
            if(c[i][j]){
                dfs(i,j);
            }  
        }
    }
    return true;
}

int main(){
    visited.assign(50,vector<int>(50,0));
    cin>>w>>h;
    for(int i=0;i<h;i++){
        for(int j=0;j<w;j++){
            cin>>c[i][j];
        }
    }
    int cnt=0;
    for(int i=0;i<h;i++){
        for(int j=0;j<w;j++){
            if(c[i][j]){
                cnt+=dfs(i,j);
            }
        }
    }

    cout<<cnt<<endl;
    return 0;
}

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

int h,w;
bool grid[100][100];

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

  for(int i=0;i<8;i++){
    int ty = y+dy[i], tx = x+dx[i];
    if(ty<0 || tx<0 || h<=ty || w<=tx)continue;
    if(grid[ty][tx])dfs(ty,tx);
  }
}

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

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

    cout << ans << endl;
  }
}

C++: #include <iostream>

using namespace std;

int w, h;
int dx[] = {0, 1, 0, -1, 1, 1, -1, -1};
int dy[] = {1, 0, -1, 0, 1, -1, 1, -1};
int map_[50][50];

int sink(int x, int y) {
    if (map_[x][y] == 0) {
        return 0;
    }
    map_[x][y] = 0;
    for (int i=0; i<8; i++) {
        int x_new = x+dx[i];
        int y_new = y+dy[i];
        if (0 <= x_new && x_new < w && 0 <= y_new && y_new < h) {
            sink(x_new, y_new);
        }
    }
    return 1;
}

int main() {
    while (true) {
        int count = 0;
        cin >> w >> h;
        if (w == 0) {
            break;
        }
        for (int i=0; i<h; i++) {
            for (int j=0; j<w; j++) {
                cin >> map_[j][i];
            }
        }
        for (int i=0; i<h; i++) {
            for (int j=0; j<w; j++) {
                count += sink(j, i);
            }
        }
        cout << count << endl;
    }
    return 0;
}

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

int w,h;
int dx[8] = {0,1,1,1,0,-1,-1,-1};
int dy[8] = {1,1,0,-1,-1,-1,0,1};
vector<vector<int>> mp;

void dfs(int y, int x){
  mp[y][x] = 0;
  for (int i = 0; i < 8; i++)
  {
    if(x+dx[i]>=w || x+dx[i]<0 || y+dy[i]>=h || y+dy[i]<0) continue;
    if(mp[y+dy[i]][x+dx[i]]) dfs(y+dy[i], x+dx[i]);
  }
}


int main(){
  vector<int> anss;
  while(true){
    cin >> w >> h;
    if(w+h==0) break;
    mp = vector<vector<int>>(h,vector<int>(w));
    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]){
          ans++;
          dfs(i,j);
        }
      }
    }
    anss.push_back(ans);
  }
  for(int ans:anss) cout << ans << endl;
}

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

int w, h, ans, tmp;
vector<vector<int>> c(60, vector<int>(60,0));

void ume(int x, int y){
	if(c[x][y]==0) return;
	c[x][y]=0;
	ume(x-1,y);
	ume(x+1,y);
	ume(x-1,y-1);
	ume(x,y-1);
	ume(x+1,y-1);
	ume(x-1,y+1);
	ume(x,y+1);
	ume(x+1,y+1);
	return;
}

bool hantei(int x, int y){
	if(c[x][y]==1){
		ume(x,y);
		return 1;
	}
	else return 0;
}

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