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;
using ll = long long;
#define FOR(i,a,b) for(int i=a;i<=b;i++)
#define FORD(i,a,b) for(int i=a;i>=b;i--)
#define ALL(a)  (a).begin(),(a).end()
#define SZ(a) int((a).size())
#define SORT(c) sort((c).begin(),(c).end())
#define MEMSET(v, h) memset((v), h, sizeof(v))
#define sqr(x) ((x) * (x))
#define bit(n) (1LL<<(n))
#define pcnt(x) __builtin_popcountll(x)
#define PB push_back
#define MP make_pair
#define endl "\n"
using Edge = pair<int,int>;
using Graph = vector<vector<Edge>>;
template<class T> inline bool chmax(T& a, T b) { if (a < b) { a = b; return 1; } return 0; }
template<class T> inline bool chmin(T& a, T b) { if (a > b) { a = b; return 1; } return 0; }
// ll gcd(ll a,ll b){return b?gcd(b,a%b):a;}
// ll lcm(ll a,ll b){return a/gcd(a,b)*b;}

bool visit[60][60];
bool land[60][60];
const int dx[8] = {1, 0, -1, 0, 1, -1, -1, 1};
const int dy[8] = {0, 1, 0, -1, 1, 1, -1, -1};

int w,h; 

bool dfs(int a,int b) {
  if(!visit[a][b] && land[a][b]){
    visit[a][b] = true;
    FOR(i,0,7){
      if(a+dx[i]>=0 && a+dx[i]<=h-1 && b+dy[i]>=0 && b+dy[i]<=w-1 && !visit[a+dx[i]][b+dy[i]] && land[a+dx[i]][b+dy[i]]){
        dfs(a+dx[i],b+dy[i]);
      };
    }
    return true;
  }
  return false;
}

int main() {
  while(1){
    cin >> w >> h;
    if(w==0 && h==0) break;

    FOR(i,0,h-1){
      FOR(j,0,w-1){
        int c; cin >> c;
        visit[i][j] = false;
        land[i][j] = c;
      }
    }

    int ans = 0;

    FOR(i,0,h-1){
      FOR(j,0,w-1){
        int track = ans;
        ans += dfs(i,j);
        // if(track!=ans) cout << i << " " << j << endl;
      }
    }
    cout << ans << endl;
  }

  


}

C++: #include <iostream>
#include <deque>
#include <cstring>
#include <algorithm>

using namespace std;

int dx[8] = {-1,-1,-1,0,0,1,1,1};
int dy[8] = {-1,0,1,-1,1,-1,0,1};

int main(){
    int n,m;
    while(cin >> m >> n){
        if((n|m) == 0) break;
        bool field[n+2][m+2];
        bool visited[n+2][m+2];
        deque<pair<int,int> > q;
        int tmp;
        int ret = 0;

        memset(field, false, sizeof(field));
        memset(visited, false, sizeof(visited));
        for(int i=1;i<=n;i++){
            for(int j=1;j<=m;j++){
                cin >> tmp;
                field[i][j] = tmp?true:false;
            }
        }
        for(int i=1;i<=n;i++){
            for(int j=1;j<=m;j++){
                if(visited[i][j] == false && field[i][j] == true){
                    q.push_back(make_pair(i,j));
                    ret++;
                }
                while(!q.empty()){
                    int x = q.front().first;
                    int y = q.front().second;
                    q.pop_front();
                    if(visited[x][y] == true){
                        continue;
                    }else{
                        visited[x][y] = true;
                    }
                    for(int k=0;k<8;k++){
                        if(field[x+dx[k]][y+dy[k]] == true &&
                           visited[x+dx[k]][y+dy[k]] == false){
                            q.push_back(make_pair(x+dx[k], y+dy[k]));
                        }
                    }
                }
            }
        }
        cout << ret << endl;
    }
}

C++: #include <cstdio>
#include <algorithm>
#include <iostream>
#include <vector>

using namespace std;
int dx[8]={1,1,1 ,0, 0,-1,-1,-1};
int dy[8]={0,1,-1,1,-1, 1, 0,-1};
int c[55][55];
int w,h;
int res;
void paint(int i,int j,int k){
  if(i<0||j<0)return;
  else if(i>=h||j>=w) return;
  else if(c[i][j]!=1) return;
  c[i][j]=k;
  for(int l=0;l<8;l++) paint(i+dx[l],j+dy[l],k);
  return;
}
void count(int i,int j){
  res++;
  paint(i,j,res);
  return;
}
int main(void){
  int i,j;
  while(scanf("%d %d",&w,&h),w|h){
    res=1;
    for(i=0;i<h;i++){
      for(j=0;j<w;j++){
        scanf("%d",&c[i][j]);
      }
    }
    for(i=0;i<h;i++){
      for(j=0;j<w;j++){
        if(c[i][j]==1){
          count(i,j);
        }
      }
    }
    printf("%d\n",res-1);
  }
  return 0;
}

C++: #include<iostream>
#include<stack>
#define loop(i,a,b) for(int i=a;i<b;i++)
#define rep(i,a) loop(i,0,a)
using namespace std;

class Node{
public:
	int x,y;
	int node;
	Node(int p,int q){
		x=p;
		y=q;
	}
};

int main(){
	int w,h;
	while(1){
		cin>>w>>h;
		if(w==0 && h==0)break;
		stack<Node> st;
		int dt[h][w];
		bool flg[h][w];
		rep(i,h){
			rep(j,w){
				cin>>dt[i][j];
				flg[i][j]=false;
			}
		}
		int ho[8]={1,1,0,-1,-1,-1,0,1};
		int ve[8]={0,1,1,1,0,-1,-1,-1};
		int ans=0;
		rep(i,h){
			rep(j,w){
				if(!flg[i][j] &&  dt[i][j]==1){
					st.push(Node(j,i));
					while(!st.empty()){
						Node now=st.top();
						flg[now.y][now.x]=true;
						st.pop();
						rep(k,8){
							int nx=now.x+ho[k];
							int ny=now.y+ve[k];
							if(nx>=0 && ny>=0 && nx<w && ny<h && flg[ny][nx]==false){
								if(dt[ny][nx]==dt[now.y][now.x]){
									Node next(nx,ny);
									st.push(next);
								}
							}
						}
					}
					ans++;
				}
			}
		}
		cout<<ans<<endl;
	}
	return 0;
}