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>

using namespace std;
#define TOSTRING(x) #x
#define SZ(x) (int)(x).size()
#define REP(i,n) for(int i=0;i<(n);i++)
#define FOR(i,a,b) for(int i=(a);i<(b);i++)
#define ALL(s) (s).begin(), (s).end()
#define dis distance
#define so(V) sort(ALL(V))
#define rev(V) reverse(ALL(V))
#define uni(v) v.erase( unique(ALL(v)) , v.end());

typedef long long unsigned int llu;
typedef long long ll;
typedef vector<int> vi;
typedef vector<ll> vll;
typedef vector<bool> vb;
typedef vector<vector <int> > vvi;
typedef vector<vb> vvb;
const double EPS = 1e-9;
const int MOD = 1e9+7;
const int INF = (1 << 29);
const ll LINF = 1e18;
const double PI = acos(-1);


typedef pair<int,bool> P;

int w,h;

bool check(int y, int x){
    if(y < 0 || y >= h)return false;
    if(x < 0 || x >= w)return false;
    return true;
}

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

void dfs(int y, int x, vvb &V){
    V[y][x] = false;
    REP(i,8){
        if(check(y + dy[i],x + dx[i]) && V[y + dy[i]][x + dx[i]]){
            dfs(y + dy[i], x + dx[i], V);
        }
    }
}

int main(){
    cin.tie(0);
    ios::sync_with_stdio(false);
    cin >> w >> h;
    vi ansV;
    while(w != 0 && h != 0){
        int ans = 0;
        vvb V(h,vb(w,false));
        REP(i,h){
            REP(j,w){
                int a;
                cin >> a;
                if(a == 1)V[i][j] = true;
            }
        }
        REP(i,h){
            REP(j,w){
                if(V[i][j]){
                    dfs(i,j,V);
                    ans++;
                }
            }
        }
        ansV.push_back(ans);
        cin >> w >> h;
    }
    for(int a: ansV){
        cout << a << endl;
    }
    return 0;
}

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;
}