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;

typedef long long llint;
typedef long double ld;
typedef pair<int,int> P;

#define inf 1e18
#define mod 1000000007
#define sort(v) sort(v.begin(),v.end())
#define reverse(v) reverse(v.begin(),v.end())
#define rep(i,a,n) for(int i=a;i<n;i++)


priority_queue<llint,vector<llint>,greater<llint> > que;
priority_queue<llint> Que;
template<class T> inline bool chmin(T& a, T b) { if (a > b) { a = b; return 1; } return 0; }
template<class T> inline bool chmax(T& a, T b) { if (a < b) { a = b; return 1; } return 0; }


#define W 51
#define H 51

int c[H][W];


void dfs(int y,int x,int h,int w){
    c[y][x]=0;
    rep(dy,-1,2){
        rep(dx,-1,2){
            int nx=x+dx;
            int ny=y+dy;
            if(nx<0 || ny<0 || nx>=w || ny>=h)continue;
            if(c[ny][nx]==1)dfs(ny,nx,h,w);
        }
    }
}

void solve(){
    int h,w;
    while(cin >> w >> h){
        if(w==0 && h==0)break;
        rep(i,0,51)rep(j,0,51)c[i][j]=0;
        rep(i,0,h){
            rep(j,0,w){
                cin >> c[i][j];
            }
        }
        int ans=0;
        rep(i,0,h){
            rep(j,0,w){
                if(c[i][j]==1){
                    dfs(i,j,h,w);
                    ans++;
                }
            }
        }
        cout << ans << endl;
    }


    
}

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

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

int w;
int h;
char field[1145][1419];
bool used[1145][1419];

void dfs(int y, int x) {
  if (y < 0 || h <= y) return;
  if (x < 0 || w <= x) return;
  if (field[y][x] == '0') return;
  if (used[y][x]) return;
  used[y][x] = true;

  for (int dy=-1; dy<=1; dy++) {
    for (int dx=-1; dx<=1; dx++) {
      if (dy == 0 && dx == 0) continue;
      int newy = y + dy;
      int newx = x + dx;
      dfs(newy, newx);
    }
  }
}

int main() {
  while (1) {
    if (scanf("%d%d", &w, &h) != 2) return 0;
    if (w == 0 && h == 0) return 0;

    for (int i=0; i<h; i++) {
      for (int j=0; j<w; j++) {
        scanf(" %c", &field[i][j]);
        used[i][j] = false;
      }
    }

    int ans = 0;
    for (int i=0; i<h; i++) {
      for (int j=0; j<w; j++) {
        if (!used[i][j] && field[i][j] == '1') {
          dfs(i, j);
          ans++;
        }
      }
    }
    printf("%d\n", ans);
  }
}

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

#define rep(i,n) for(i = 0;i < n;++i)
#define all(v) v.begin(), v.end()
typedef long long ll;

vector<vector<ll>> tizu(50,vector<ll>(50));
vector<vector<bool>> visit(50,vector<bool>(50, false));
ll h,w;

void dfs(pair<ll,ll> v)
{
    ll a = v.first;
    ll b = v.second;
    if(!tizu.at(a).at(b)){
        return;
    }
    visit.at(a).at(b) = true;
    ll tate[9] = {-1,0,1,-1,0,1,-1,0,1};
    ll yoko[9] = {-1,-1,-1,0,0,0,1,1,1};
    ll i,j;
    for(i = 0;i < 9;++i){
        ll p = a + tate[i];
        ll q = b + yoko[i];
        if(p < 0 || p >= h){
            continue;
        }else if (q < 0 || q >= w){
            continue;
        }else if(!visit.at(p).at(q) && tizu.at(p).at(q)){
            dfs(make_pair(p,q));
        }
    }
    return;
}

int main()
{
    ll i,j;
    while(true){
        cin >> w >> h;
        if(w == 0 && h == 0){
            return 0;
        }
        for(i = 0;i < h;++i){
            rep(j,w){
                cin >> tizu.at(i).at(j);
            }
        }
        for(i = 0;i < h;++i){
            rep(j,w){
                visit.at(i).at(j) = false;
            }
        }
        ll ans = 0;
        for(i = 0;i < h;++i){
            rep(j,w){
                if(visit.at(i).at(j) || !tizu.at(i).at(j)){
                    continue;
                }else{
                    dfs(make_pair(i,j));
                    ++ans;
                }
            }
        }
        cout << ans << endl;
    }

    return 0;
}

C++: #include <bits/stdc++.h>
using namespace std;
#define FOR(i,a,b) for(int i=(a);i<(b);++i)
#define rep(i,n)   FOR(i,0,n)
#define pb emplace_back
typedef long long ll;
typedef pair<int,int> pint;

int c[51][51];
bool used[51][51];
const int dx[]={-1,-1,-1,0,0,1,1,1},dy[]={1,0,-1,1,-1,1,0,-1};
int main(){
    int w,h;
    while(cin>>w>>h,w|h){
        int cnt=0;
        memset(used,0,sizeof(used));
        rep(i,h)rep(j,w) cin>>c[i][j];
        rep(i,h)rep(j,w)if(!used[i][j]&&c[i][j]==1){
            ++cnt;
        queue<pair<pint,int> > q;
        q.push({{j,i},0});
        used[i][j]=true;
        while(!q.empty()){
            pair<pint,int> pi=q.front();q.pop();
            rep(k,8){
                int tx=pi.first.first+dx[k],ty=pi.first.second+dy[k];
                if(tx>=0&&ty>=0&&tx<w&&ty<h&&c[ty][tx]==1&&!used[ty][tx]){
                    q.push({{tx,ty},pi.second+1});
                    used[ty][tx]=true;
                }
            }
        }
        }
        cout<<cnt<<endl;
    }
    return 0;
}