Go: package main

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

var (
	readString func() string
	readBytes  func() []byte
	stdout     *bufio.Writer
)

func init() {
	readString, readBytes = newReadString(os.Stdin)
	stdout = bufio.NewWriter(os.Stdout)
}

func newReadString(ior io.Reader) (func() string, func() []byte) {
	r := bufio.NewScanner(ior)
	r.Buffer(make([]byte, 1024), math.MaxInt32)
	r.Split(bufio.ScanWords)

	f1 := func() string {
		if !r.Scan() {
			panic("Scan failed")
		}
		return r.Text()
	}
	f2 := func() []byte {
		return []byte(f1())
	}
	return f1, f2
}

func readInt() int {
	return int(readInt64())
}

func readInt64() int64 {
	i, err := strconv.ParseInt(readString(), 10, 64)
	if err != nil {
		panic(err.Error())
	}
	return i
}

func readFloat64() float64 {
	f, err := strconv.ParseFloat(readString(), 64)
	if err != nil {
		panic(err.Error())
	}
	return f
}

func printf(f string, args ...interface{}) (int, error) {
	return fmt.Fprintf(stdout, f, args...)
}

func println(args ...interface{}) (int, error) {
	return fmt.Fprintln(stdout, args...)
}

func eprintln(args ...interface{}) (int, error) {
	return fmt.Fprintln(os.Stderr, args...)
}

func eprintf(f string, args ...interface{}) (int, error) {
	return fmt.Fprintf(os.Stderr, f, args...)
}

func min(a, b int) int {
	if a < b {
		return a
	}
	return b
}

func min64(a, b int64) int64 {
	if a < b {
		return a
	}
	return b
}

func max(a, b int) int {
	if a < b {
		return b
	}
	return a
}

func max64(a, b int64) int64 {
	if a < b {
		return b
	}
	return a
}

func abs(a int) int {
	if a < 0 {
		return -a
	}
	return a
}

func abs64(a int64) int64 {
	if a < 0 {
		return -a
	}
	return a
}

func gcd(a, b int) int {
	if b == 0 {
		return a
	}
	return gcd(b, a%b)
}

func gcd64(a, b int64) int64 {
	if b == 0 {
		return a
	}
	return gcd64(b, a%b)
}

// readString() string
// readInt() int
// readInt64() int64
// readFloat64() float64

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

func main() {
	defer stdout.Flush()

	for {
		ans, ok := solve()
		if !ok {
			break
		}
		println(ans)
	}

}

func solve() (int, bool) {
	w := readInt()
	h := readInt()
	if w == 0 && h == 0 {
		return 0, false
	}

	c := make([][]int, h+2)
	c[0] = make([]int, w+2)
	c[h+1] = make([]int, w+2)
	for i := 1; i-1 < h; i++ {
		c[i] = make([]int, w+2)
		for j := 1; j-1 < w; j++ {
			c[i][j] = readInt()
		}
	}

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

func dfs(c [][]int, y, x int) {
	c[y][x] = 0
	dx := []int{-1, 0, 1, -1, 1, -1, 0, 1}
	dy := []int{-1, -1, -1, 0, 0, 1, 1, 1}
	for i := range dx {
		nx := x + dx[i]
		ny := y + dy[i]
		if 0 < c[ny][nx] {
			dfs(c, ny, nx)
		}
	}
}

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

C++: #include <bits/stdc++.h>
using namespace std;
#include <bits/stdc++.h>
using namespace std;
struct Graph{
  using ver_t=int32_t; using edge_t=pair<ver_t,ver_t>;
  vector<vector<ver_t>> field;
  const edge_t dr[8]={{1,1},{1,0},{1,-1},{0,1},{0,-1},{-1,1},{-1,0},{-1,-1}};
  void dfs(edge_t frm){
    // preorder
    field[frm.first][frm.second]=0;
    for(const auto& v:dr){
      auto x=frm.first+v.first; auto y=frm.second+v.second;
      if(x>=0&&x<field.size()&&y>=0&&y<field.front().size()&&field[x][y]==1){
        dfs(make_pair(x,y));
      }
    }
    // postorder
  }
  // void bfs(ver_t frm){
  //   seen[frm]=0;
  //   deque<ver_t> que(1,frm); // dfs,bfs
  //   while(!que.empty()){
  //     auto u=que.front(); que.pop_front(); // bfs
  //     for(const auto& v:edge[u]){
  //       if(seen[v]==NO){
  //         seen[v]=seen[u]+1;
  //         que.push_back(v); // dfs,bfs
  //       }
  //     }
  //   }
  // }
};
int32_t main(){
  Graph g;
  // size_t n; cin>>n;
  // for(size_t i=0; i<n; ++i){
  size_t w,h; cin>>w>>h;
  while(w!=0||h!=0){
    g.field.resize(h);
    for(auto&& x:g.field){
      x.resize(w);
      for(auto&& y:x) cin>>y;
    }
    size_t ret=0;
    for(size_t i=0; i<h; ++i){
      for(size_t j=0; j<w; ++j){
        if(g.field[i][j]==1){ ++ret; g.dfs(make_pair(i,j)); }
      }
    }
    cout<<ret<<'\n';
    cin>>w>>h;
  }
}

C++: #include <iostream>
using namespace std;
int w,h;
int in[100][100];
bool seen[100][100];
int ans =0;
bool flag = false;

void solve(int i,int j){
    cout << i << " " << j << endl;
    if(i < 0 || j < 0 || i >= h || j >= w){
        return;
    }
    if(seen[i][j]){
        return;
    }
    if(in[i][j] == 0){
        return;
    }
    seen[i][j] = true;
    solve(i-1,j-1);
    solve(i-1,j);
    solve(i-1,j+1);
    solve(i,j-1);
    solve(i,j+1);
    solve(i+1,j-1);
    solve(i+1,j);
    solve(i+1,j+1);

}
int main(){
    cin >> w >> h;
    for(int i=0;i<h;i++){
        for(int j=0;j<w;j++){
            cin >> in[i][j];
        }
    }
    for(int i=0;i<h;i++){
        for(int j=0;j<w;j++){
            if(seen[i][j] == true || in[i][j] == 0){
            }
            else{
                solve(i,j);
                ans++;
            }
            
        }
    }
    cout << ans << endl;
    return 0;
}

C++: #include <iostream>
#include <vector>
#include <algorithm>
#include <stack>
#include <string>
#include <map>
#include <functional>
#include <cmath>
#include <cstdio>
using namespace std;

#define PI 4*atan(1)

int w,h;
int c[50][50];

void dfs(int i, int j){
  c[i][j] = 0;
  for(int dx = -1; dx <= 1; dx++){
    for(int dy = -1; dy <= 1; dy++){
      int nx = i + dx, ny = j + dy;
      if(0 <= nx && nx < h && 0 <= ny && ny < w && c[nx][ny] == 1){
        dfs(nx, ny);
      }
    }
  }
}

void solve(){
  int cnt = 0;
  for(int i = 0; i < h; i++){
    for(int j = 0; j < w; j++){
      if(c[i][j] == 1){
        dfs(i, j);
        cnt++;
      }
    }
  }
  cout << cnt << endl;
}

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

      }
    }
    solve();
  }
}

C++: #include <bits/stdc++.h>
using namespace std;
int w, h;
vector<vector<int>> a(55, vector<int>(55, 0));
int ct;

void dfs(int y, int x) {
  a[y][x] = 0;
  for(int i = -1; i <= 1; ++i) {
    for(int j = -1; j <= 1; ++j) {
      int ny = y + i, nx = x + j;
      if(0 <= ny && ny < h && 0 <= nx && nx < w && a[ny][nx] == 1) {
        dfs(ny, nx);
      }
    }
  }
}

int main() {
  for(;;) {
    scanf("%d %d", &w, &h);
    if(w == 0 && h == 0) break;
    for(int i = 0; i < h; ++i) {
      for(int j = 0; j < w; ++j) {
        scanf("%d", &a[i][j]);
      }
    }

    for(int i = 0; i < h; ++i) {
      for(int j = 0; j < w; ++j) {
        if(a[i][j] == 1) {
          ++ct;
          dfs(i, j);
        }
      }
    }
    printf("%d\n", ct);
    ct = 0;
    a.clear();
  }
  return 0;
}