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 <bits/stdc++.h>
#define pq priority_queue
using namespace std; typedef pair<int, int> P; typedef pair<int, P> P2;
typedef pair<int, P2> P3; typedef long long ll; typedef long double ld;
constexpr long long gcd(long long a, long long b){return b ? gcd(b, a % b) : a;}
constexpr long long lcm(long long a, long long b){return a / gcd(a, b) * b;}
constexpr int INF = 1e9, MOD = INF + 7, around[] = {0, 1, 1, -1, -1, 0, -1, 1, 0, 0};
constexpr int mod_pow(long long x, long long n, const int mod){long long ret=1;while(n){if(n&1)(ret*=x)%=mod;(x*=x)%=mod;n>>=1;}return ret;}
template<int n> struct Prime{bool arr[n+1];constexpr bool operator[](int k){return arr[k];}constexpr Prime():arr(){for(int i=2;i<n;i++){arr[i]=true;for(int j=2;j*j<=i;j++){if(!(i%j))arr[i]=false;}}}};
template<int n> struct Factorial{long long arr[n+1],ary[n+1];constexpr Factorial():arr(),ary(){arr[0]=1;ary[0]=1;for(int i=0;i<n;i++){arr[i+1]=arr[i]*(i+1)%MOD;ary[i+1]=mod_pow(arr[i+1],MOD-2,MOD);}}};
constexpr Factorial<10> fact; constexpr Prime<10> prime;
constexpr int comb(int a, int b){long long pos = fact.arr[a], pot = fact.ary[a - b], por = fact.ary[b];return pos * pot % MOD * por % MOD;}
template<int n> struct Bernoulli{long long arr[n+1];constexpr Bernoulli():arr(){arr[0]=1;for(int i=1;i<=n;i++){long long sum=0;for(int j=0;j<i;j++){(sum+=comb(i+1,j)*arr[j]%MOD)%=MOD;}arr[i]=(MOD-mod_pow(i+1,MOD-2,MOD))%MOD*sum%MOD;}}};
constexpr int vx[] = {1, 0, -1, 0}, vy[] = {0, 1, 0, -1};
constexpr int sqrtN = 512, logN = 32;
constexpr ld PI = abs(acos(-1));
constexpr ll LINF=1e18;

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

int main(){
	while(cin >> w >> h,w){
		for(int i = 0; i < h; i++){
			for(int j = 0; j < w; j++){
				cin >> a[i][j];
			}
		}
		
		int c = 0;
		for(int i = 0; i < h; i++){
			for(int j = 0; j < w; j++){
				if(a[i][j] != 1) continue; c++;
				
				queue<P> qu; qu.push({i, j}); a[i][j] = 2;
				while(!qu.empty()){
					auto p = qu.front(); qu.pop();
					
					for(int k = 0; k < 8;){
						int nx = p.first + around[k], ny = p.second + around[++k];
						if(nx<0||ny<0||nx>=h||ny>=w||a[nx][ny] != 1) continue;
						a[nx][ny] = 2;
						qu.push({nx, ny});
					}
				}
			}
		}
		
		cout << c << endl;
	}

	return 0;
}

C++: #include <bits/stdc++.h>
#define rep(i,n) for(int i=0;i<n;i++)
using namespace std;
typedef long long ll;
typedef pair<int,int> pp;
const int INF = 1e9;
const int MOD = 1000000007;
int w = 51,h = 51;
vector<vector<int>> maze(h,vector<int>(w,0));
vector<int> dx = {0,0,1,-1,1,1,-1,-1};
vector<int> dy = {1,-1,0,0,1,-1,1,-1};

void dfs(int y,int x){
        maze.at(y).at(x) = 2;
        rep(i,8){
                int new_y,new_x;
                new_y = y + dy[i];
                new_x = x + dx[i];
                if(0 <=new_y && new_y < h && 0<= new_x && new_x < w){
                        if(maze.at(new_y).at(new_x) == 1){
                                dfs(new_y,new_x);
                        }
                }
        }
}

int main(){
        while(1){
                cin >> w >> h;
                if(w == 0 && h == 0) break;
            rep(i,h)rep(j,w) cin >> maze.at(i).at(j);
        
            int cnt = 0;
            rep(i,h)rep(j,w){
                        if(maze.at(i).at(j) == 1){
                                dfs(i,j);
                        cnt ++;
                }
            }
        
                cout << cnt << endl;
        }
        return 0;
}

C++: #include<iostream>

int field[52][52];

void clear();

bool walk(int x, int y);

int main(){

	int w,h,ans;
	
	while(true){

		std::cin >> w >> h;
		if(w==0 && h==0)
			break;

		clear();
		for(int i=1; i<=h; i++)
			for(int j=1; j<=w; j++)
				std::cin >> field[i][j];

		ans = 0;
		for(int i=1; i<=h; i++)
			for(int j=1; j<=w; j++)
				if(walk(j, i))
					ans++;

		std::cout << ans << std::endl;

	}

	return 0;
}

void clear(){
	for(int i=0; i<52; i++)
		for(int j=0; j<52; j++)
			field[i][j] = 0;
}

bool walk(int x, int y){
	if(field[y][x] == 0)
		return false;

	field[y][x] = 0;
	
	for(int i=-1; i<=1; i++)
		for(int j=-1; j<=1; j++)
			walk(x+i, y+j);

	return true;
}

C++: #include<bits/stdc++.h>
#include<vector>
using namespace std;
vector< vector<int> > c(50, vector<int> (50));
void dfs(int x, int y, int w, int h){
  c[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(0 <= nx && nx < w && 0 <= ny && ny < h && c[ny][nx] == 1)
        dfs(nx, ny, w, h);
    }
  }
}

int main(void){

while(true){
  int w, h;   cin >> w >> h;
  if(w == 0 && h == 0)    break;
  for(int i = 0 ; i < h ; i++){
    for(int j = 0 ; j < w ; j++){
      cin >> c[i][j];
    }
  }

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

  cout << ans << endl;
}

  return 0;
}

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

int field[50][50];
int w, h;
vector<int> di_x = {1,0,-1,0,1,-1,1,-1};
vector<int> di_y = {0,1,0,-1,1,1,-1,-1};

void dfs(int x, int y) {
   field[x][y] = 0;
   rep(i,8) {
      int n_x = x + di_x[i];
      int n_y = y + di_y[i];
      if (n_x < 0 || h <= n_x || n_y < 0 || w <= n_y) continue;
      else if (field[n_x][n_y] == 0) continue;
      else dfs(n_x,n_y);
   }
}

int main () {
   while (true) {
      cin >> w >> h;
      if (w == 0 && h == 0) break;
      int ct = 0;
      rep(i,h)rep(j,w) cin >> field[i][j];
      rep(i,h)rep(j,w) {
         if (field[i][j] == 1) {
            dfs(i,j);
            ct++;
         }
      }
      cout << ct << endl;
      rep(i,h)rep(j,w) field[i][j] = 0;
   }
}