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++: #define _CRT_SECURE_NO_WARNINGS
#include<bits/stdc++.h>
using namespace std;
#ifndef FAST_IO
 #define IOS ios_base::sync_with_stdio(false);cin.tie(nullptr);
#endif

#define int long long
#define FILE_IO freopen("input.txt","r",stdin)/#define int long long
#define f first
#define s second
#define endl '\n'
#define all(c) c.begin(),c.end()
#define bp __builtin_popcount
#define debug(x) cerr << #x << " = " << x << endl;

const int MOD = 1e9 + 7;
const int INF = 1e18;

mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());
template <typename T>void print(T t){for (auto &it : t) cout << it << " ";}
inline int powerexp(int a, int b, int m = MOD){ int res = 1;while(b){if(b&1)res = res * a % m;a = a * a % m,b >>= 1;}return res % m;}
inline int modinv(int a){ return powerexp(a,MOD-2,MOD);}
inline int lcm(int a, int b){ int g = __gcd(a,b);return a/g*b;}
inline int add(int x, int y){ x += y; if (x >= MOD) x -=  MOD; return x;}
inline int mul(int x, int y){ return (x * 1ll * y) % MOD;}
inline int sub(int x, int y){ x -= y; if (x<0) x += MOD; return x ;}

//-------------------< start here >-------------------------// 

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

void solve(){
    int w, h;
    while (cin >> w >> h, w && h){
		int g[h][w];
		bool visited[h][w];
		for (int i = 0; i < h; i++){
		  for (int j = 0; j < w; j++){
			  cin >> g[i][j]; 
			}	
		}
	  memset(visited, false, sizeof visited);
	  int ret = 0;
	  for (int i = 0; i < h; i++){
		for (int j = 0; j < w; j++){
			if (g[i][j] == 1 && !visited[i][j]){
			  ret++;
			  queue< pair<int, int> > q;
			  q.push({i, j});
			  while (!q.empty()){
				 auto p = q.front();
				 q.pop();
				 int ny = p.f, nx = p.s;
				 for (int k = 0; k < 8; k++){
					 int X = nx + dx[k];
					 int Y = ny + dy[k];
					 if (X >= 0 && Y >= 0 && X < w && Y < h){
					  if (g[Y][X] == 1 && !visited[Y][X]){
						   visited[Y][X] = true;
						   q.push({Y, X}); 
						 }	 
					  } 
				   }  
				}	
			 }
		  } 
		}
	 cout << ret << endl;	
	}
}

//-----------------------------
//comment from here for TOPCODER 
int32_t main(){
   IOS;
   solve();
   //find();
   #ifdef LOCAL_DEFINE
   cerr << "time elapsed " << (double)clock()/CLOCKS_PER_SEC << endl;
   #endif
}

C++: //
// Created by elegant-belltree on 15/06/03.
//

#include <iostream>
#include <set>

using namespace std;

int main(){
    int w,h;
    int c[50][50];

    while (cin>>w>>h && (w>0 || h>0)) {
        int label=2;
        set<int> labels;
        int n = -1;
        int count;

        do{
            n++;
            count=0;
            for (int i = 0; i < h; ++i) {
                for (int j = 0; j < w; ++j) {
                    if(n==0) {
                        cin >> c[j][i];
                    }
                    if (0<c[j][i]) {
                        for (int k = -1; (n==0 && k < 1) || (0<n && k<2); ++k) {
                            for (int l = -1; (0<n || k!=0 || l!=0) && l < 2 && j + l < w; ++l) {
                                if (-1 < i + k && -1 < j + l && 0 < c[j + l][i + k] && (c[j][i] == 1 || c[j + l][i + k] < c[j][i])) {
                                    if(0<n){
                                        labels.erase(c[j][i]);
                                        count++;
                                    }
                                    c[j][i] = c[j + l][i + k];
                                }
                            }
                        }
                        if (c[j][i] == 1) {
                            c[j][i] = label++;
                            labels.insert(c[j][i]);
                        }
                    }
                }
            }
        }while(n==0 || 0<count);
        /*for (int m = 0; m < h; ++m) {
            for (int i = 0; i < w; ++i) {
                cout<<c[i][m]<<" ";
            }
            cout<<endl;
        }*/
        cout<<labels.size()<<endl;
    }
    return 0;
}

C++: #include<bits/stdc++.h>
using namespace std;
#define OUT(x) cout<<x<<endl
#define DEBUG(x) cout<<#x<<": "<<x
const int BA = 1000;
const int INFTY = (1<<21);
int c[51][51];
int memo[51][51];
int w,h;
int del(int x,int y){
  if(memo[x][y] != -1)return memo[x][y];
  else if(x<0 || y<0 || x>=w || y>= h)return 0;
  else if(c[x][y]){
    c[x][y] = 0;
    return memo[x][y] = del(x+1,y)+del(x,y+1)+del(x+1,y+1)+
      del(x-1,y-1)+del(x-1,y)+del(x,y-1)+del(x+1,y-1)+del(x-1,y+1);
    
  }
  else return 0;
}

int solve(int x,int y){
  if(x==0 && y == h)return 0;
  else if(x==w){
    return solve(0,y+1);
  }
  else if(c[x][y]==1){
    del(x,y);
    return solve(x+1,y)+1;
  }
  else return solve(x+1,y);
}

int main(){
  while(cin>>w>>h&&w&&h){
    memset(memo,-1,sizeof(memo));
    for(int i=0;i<h;i++){
      for(int j=0;j<w;j++){
	cin >> c[i][j];
      }
    }
    
    cout << solve(0,0) << endl;
  }
}