Go: // AOJ 1160: How Many Islands? http://judge.u-aizu.ac.jp/onlinejudge/description.jsp?id=1160

package main

import (
	"bufio"
	"container/list"
	"fmt"
	"os"
	"strconv"
)

var SEA = 0
var LAND = 1

func nextInt(sc *bufio.Scanner) int {
	sc.Scan()
	v, _ := strconv.Atoi(sc.Text())
	return v
}

func discoverIslands(M, V [][]int, H, W int) int {
	// 島を探す
	queue := list.New()
	islands := 0
	for i := 1; i <= H; i++ {
		for j := 1; j <= W; j++ {
			// 訪問済みの土地ならスキップ
			if V[i][j] == 1 {
				continue
			}

			// 訪問中の土地が海なら訪問済みにしてスキップ
			if M[i][j] == SEA {
				V[i][j] = 1
				continue
			}

			// 未訪問の陸地が見つかったので
			// 島の数を1つ増やし、この陸地から辿れる陸地を訪問していく
			islands++
			queue.PushBack([]int{i, j})

			for queue.Len() > 0 {
				cur := queue.Remove(queue.Front()).([]int)
				h, w := cur[0], cur[1]
				if V[h][w] == 1 {
					continue
				}
				V[h][w] = 1

				// 全方向に未訪問の陸地がないか探索
				for v := -1; v <= 1; v++ {
					for l := -1; l <= 1; l++ {
						dh := h + v
						dw := w + l
						if M[dh][dw] == LAND && V[dh][dw] == 0 {
							queue.PushBack([]int{dh, dw})
						}
					}
				}
				/*
					if M[h-1][w] == LAND {
						queue.PushBack([]int{h - 1, w})
					}
					if M[h-1][w+1] == LAND {
						queue.PushBack([]int{h - 1, w + 1})
					}
					if M[h][w+1] == LAND {
						queue.PushBack([]int{h, w + 1})
					}
					if M[h+1][w+1] == LAND {
						queue.PushBack([]int{h + 1, w + 1})
					}
					if M[h+1][w] == LAND {
						queue.PushBack([]int{h + 1, w})
					}
					if M[h+1][w-1] == LAND {
						queue.PushBack([]int{h + 1, w - 1})
					}
					if M[h][w-1] == LAND {
						queue.PushBack([]int{h, w - 1})
					}
					if M[h-1][w-1] == LAND {
						queue.PushBack([]int{h - 1, w - 1})
					}
				*/
			}
		}
	}
	return islands
}

func main() {
	sc := bufio.NewScanner(os.Stdin)
	sc.Split(bufio.ScanWords)

	for {
		W, H := nextInt(sc), nextInt(sc)

		if W == 0 && H == 0 {
			break
		}
		// Initialize map and visted list
		M := make([][]int, H+2)
		V := make([][]int, H+2)
		for i := 0; i < H+2; i++ {
			M[i] = make([]int, W+2)
			V[i] = make([]int, W+2)
		}
		for i := 1; i <= H; i++ {
			for j := 1; j <= W; j++ {
				M[i][j] = nextInt(sc)
			}
		}

		islands := discoverIslands(M, V, H, W)

		fmt.Println(islands)
	}
}

C++: #include <bits/stdc++.h>
#define rep(i,a,b) for(int i=int(a);i<int(b);++i)
#define SIZE 200005
#define INF 1000000005LL
#define MOD 1000000007

using namespace std;
typedef long long int ll;
typedef pair <int,int> P;

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

void dfs(int i,int j);
void dfs_(int i,int j){
  if(i>=0 && j>=0) if(c[i][j]==1) dfs(i,j);
}

void dfs(int i,int j){
  if(d[i][j]==1)return;
  d[i][j] = 1;
  rep(l,0,3) rep(r,0,3) dfs_(i-1+l,j-1+r);
}


int imain(){
  a=0;
  rep(i,0,52) rep(j,0,52) c[i][j]=d[i][j]=0;
  rep(i,0,h) rep(j,0,w) {
    cin >> c[i][j];
  }

  rep(i,0,h) rep(j,0,w) if(c[i][j]==1 && d[i][j]==0) {
    dfs(i,j);
    a++;
  }
  cout << a << endl;

  return 0;
}

int main(){
  for(;;){
    cin >> w >> h;
    if(w==0 && h==0)break;
    imain();
  }
  return 0;
}

C++: #include <bits/stdc++.h>
using namespace std;
vector<string>v;
vector<int>ansl;
string s;
int ary[100005] = {0};
int mp[51][51];
int cnt = 0;
int d_x[] = {0,1,0,-1};
int d_y[] = {1,0,-1,0};
int W,H;

void dfs(int x,int y){
  mp[y][x] = 0;
  for(int dx = -1;dx<2;dx++){
    for(int dy = -1;dy<2;dy++){
    int nx = x+dx,ny = y+dy;
    if((nx>=0)&&(ny>=0)&&(ny<H)&&(nx<W)&&(mp[ny][nx] == 1)){
      dfs(nx,ny);
    }
  }
}
}
int main(){
  cin >>W>>H;

  while((W!=0)&&(H!=0)){
    cnt = 0;
    for(int i = 0;i<51;i++){
      for(int j = 0;j<51;j++){
        mp[i][j] = 0;
      }
    }
    for(int i = 0;i<H;i++){
      for(int j = 0;j<W;j++){
        cin >>mp[i][j];
      }
    }
    for (int y = 0; y < H; y++){
      for (int x = 0; x < W; x++) {
        if (mp[y][x] == 1) {
          dfs(x, y);
          cnt++;
        }
      }
    }
    cout <<cnt<<endl;
    cin >>W>>H;
  }
  return 0;
}

C++: //default
#define _GLIBCXX_DEBUG
#include<bits/stdc++.h>
using namespace std;
#define int long long
#define double long double
typedef pair<int, int> P_i;
typedef pair<double,double> P_d;
#define rep(i,n) for(int i=0;i<(n);++i)
#define alrep(i,j,n) for(int i=0;i<(n);++i)for(int j=i+1;j<(n);++j)
#define Yes cout << "Yes" << endl;
#define No cout << "No" << endl;
#define answer cout << ans << endl;
template<class T> inline bool chmin(T& a, T b) { if (a > b) { a = b; return true; } return false; }
template<class T> inline bool chmax(T& a, T b) { if (a < b) { a = b; return true; } return false; }
const int MOD=1e9+7;
const long long INF = 1LL << 60;

bool seen[55][55];
int h,w,ans=0;
char field[55][55];
int dx[8]={1,1,0,-1,-1,-1,0,1};
int dy[8]={0,1,1,1,0,-1,-1,-1};

void dfs(int x,int y) {
	seen[x][y] = true; // v を訪問済にする
	// v から行ける各頂点 next_v について
	rep(i,8){
		int nx=x+dx[i];
		int ny=y+dy[i];
		if(nx<0 || nx>=w || ny<0 || ny>=h) continue;
		if(seen[nx][ny]==true) continue;
		if(field[nx][ny]=='0') continue;
		dfs(nx,ny);
	}
}

signed main(){
	while(cin >> w >> h && (w!=0 || h!=0)){
		ans=0;
		rep(y,h)rep(x,w){
			cin >> field[x][y];
		}
		memset(seen,false,sizeof(seen));
		rep(y,h)rep(x,w){
			if(seen[x][y]==true) continue;
			if(field[x][y]=='0') continue;
			ans++;
			dfs(x,y);
		}
		cout << ans << endl;
	}

	return 0;
}

C++: 
#pragma GCC optimize("O3")
#include <bits/stdc++.h>
#define ll long long
#define rep2(i,a,b) for(ll i=a;i<=b;++i)
#define rep(i,n) for(ll i=0;i<n;i++)
#define rep3(i,a,b) for(ll i=a;i>=b;i--)
#define pii pair<int,int>
#define pll pair<ll,ll>
#define pq priority_queue
#define pb push_back
#define eb emplace_back
#define veci vector<int>
#define vecll vector<ll>
#define vecpii vector<pii>
#define vec2(a,b) vector<vec>(a,vec(b))
#define vec2ll(a,b) vector<vec>(a,vecll(b))
#define vec3(a,b,c) vector<vector<vec>>(a,vec2(b,c))
#define vec3ll(a,b,c) vector<vector<vecll>>(a,vec2ll(b,c))
#define fi first
#define se second
#define all(c) begin(c),end(c)
#define ios ios_base::sync_with_stdio(0),cin.tie(0),cout.tie(0);
#define lb(c,x) distance(c.begin(),lower_bound(all(c),x))
#define ub(c,x) distance(c.begin(),upper_bound(all(c),x))


using namespace std;
int in() {int x;cin>>x;return x;}
ll lin() {ll x;cin>>x;return x;}
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;}
template<class T> inline void print(pair<T,T> p){cout<<"("<<p.first<<","<<p.second<<") ";}
//template<class T> inline void print(vector<pair<T,T>> v){for(auto e:v)print(e); cout<<endl;}
//template<class T> inline void print(T v){for(auto e:v)cout<<e<<" ";cout<<endl;}
template<typename T>
istream& operator >> (istream& is, vector<T>& vec){
    for(T& x:vec) is >> x;
    return is;
}
const ll INF=1e9+7;

ll c[100][100];
ll w,h;

void dfs(ll y,ll x){
    c[y][x]=0;
    for(ll p=-1;p<=1;p++){
        for(ll q=-1;q<=1;q++){
            if(p==0 && q==0) continue;
            ll ny=y+p;
            ll nx=x+q;
            if(0<=ny && ny<=h-1 && 0<=nx && nx<=w-1 && c[ny][nx]==1){
                dfs(ny,nx);
            }
        }
    }
}

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