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<iostream>
#include<cstring>
#include<string>
#include<algorithm>
#include<cmath>
#include<vector>
using namespace std;
typedef long long ll;
#define rep(i,s,n) for(int i = s;i<n;i++)
#define repe(i,s,n) for(int i = s;i<=n;i++)
static const ll maxLL = (ll)1 << 62;

//dfs(bool型)
int dy[]={-1,0,1,0,1,-1,-1,1};
int dx[]={0,1,0,-1,1,1,-1,-1};
bool visited[1000][1000];
int t[1000][1000];
char copy_t[1000][1000];
int h,w;
int si,sj;

void dfs(int y,int x){
  if(y<=0||x<=0)return;
  if(y>h||x>w)return;
  if(t[y][x]==0)return;
  if(visited[y][x])return;
  visited[y][x]=true;
  for(int i=0;i<8;i++){
    int ny=y+dy[i];
    int nx=x+dx[i];
    dfs(ny,nx);
  }
  return;
}

int main(){
    while(0){
    int cnt=0;
    cin>>w>>h;
    if(w==\0 && h==\0)break;
    repe(i,1,h){
        for(int j=1;j<=w;j++){
            cin>>t[i][j];
        }
    }
    repe(i,1,h){
        repe(j,1,w){
             if(t[i][j]==1 && visited[i][j]==false)cnt++;
        dfs(i,j);
        }
    }
    cout<<cnt<<endl;
}
return 0;
}

C++: #include <iostream>
#include <map>
#include <algorithm>
#include <vector>
#include <string>
#include <utility>
#include <queue>
#define INF 1e9+7
#define rep(i,n) for(int i=0;i<n;i++)
#define NO cout<<"NO"<<endl;
#define YES cout << "YES"<<endl;
#define No cout << "No"<<endl;
#define Yes cout << "Yes"<<endl;
#define all(a) a.begin(),a.end()
#define P pair<int,int>
using namespace std;
typedef long long int ll;
typedef unsigned long long int ull;

//(int)'a'は97 (int)'A'は65 (int)'1'は49
//おまじない

int main(){
    
    while(true){
        int h,w;
        int m[50][50];
        bool used[50][50]={0};
        int dx[]={-1,0,1};
        int dy[]={-1,0,1};
        int ans=0;
        
        cin >> w>>h;
        if(h==0&&w==0)break;
        
        rep(i,h){
            rep(j,w){
                cin >> m[i][j];
            }
        }
        
        rep(i,h){
            rep(j,w){
                if(m[i][j]==1&&!used[i][j]){
                    ans++;
                    queue<P> que;
                    que.push(P(i,j));
                    while(!que.empty()){
                        P p=que.front();que.pop();
                        int x = p.second,y=p.first;
                        rep(a,3){
                            rep(b,3){
                                int nx=x+dx[a],ny=y+dy[b];
                                if(nx>=0&&nx<w&&ny>=0&&ny<h&&m[ny][nx]==1&&!used[ny][nx]){
                                    que.push(P(ny,nx));
                                    used[ny][nx]=true;
                                }
                            }
                        }
                    }
                }
            }
        }
        cout << ans<<endl;
    }
    
    return 0;
}

C++: #include <bits/stdc++.h>
using namespace std;
#define rep(i, n) for (int i = 0; i < (int)(n); i++)
#define FOR(i, s, e) for (int i = (int)(s); i <= (int)(e); i++)
#define printYesNo(is_ok) puts(is_ok ? "Yes" : "No");
#define SORT(v) sort(v.begin(), v.end());
#define RSORT(v) sort(v.rbegin(), v.rend());
#define REVERSE(v) reverse(v.begin(), v.end());


int ans = 0;
vector<vector<int> > island_map;
void dfs(vector<vector<int> > field, pair<int, int> init_pos)
{
  int dirx[] = {1, 1, 1, 0, 0, -1, -1, -1};
  int diry[] = {1, 0, -1, 1, -1, 1, 0, -1};

  int h = field.size();
  int w = field[0].size();
  stack<pair<int, int> > pos_stack;
  pos_stack.push(init_pos);

  while (pos_stack.size())
  {
    pair<int, int> pos = pos_stack.top();
    pos_stack.pop();
    int pos_x = pos.first;
    int pos_y = pos.second;
    rep(i, 8)
    {
      int next_x = pos_x + dirx[i];
      int next_y = pos_y + diry[i];

      if (0 <= next_x && next_x < h && 0 <= next_y && next_y < w && island_map[next_x][next_y] == 0 && field[next_x][next_y] == 1)
      {
        island_map[next_x][next_y] = ans;
        pos_stack.push(make_pair(next_x, next_y));
      }
    }
  }
}

int main()
{
  while(true)
  {
    int w, h;
    cin >> w >> h;
    if(w == 0 && h == 0)
      return 0;

    ans = 0;    
    vector<vector<int> > field(h, vector<int>(w, 0));
    island_map = vector<vector<int> >(h, vector<int>(w, 0));
    rep(i, h) rep(j, w)
    {
      cin >> field[i][j];
    }

    rep(i, h) rep(j, w)
    {
      if(island_map[i][j] == 0 && field[i][j] == 1)
      {
        ans++;
        dfs(field, make_pair(i, j));
      }
    }
    cout << ans << endl;
  }
  return 0;
}

C++: #include<bits/stdc++.h>
using namespace std;
using ll=long long;
using ld=long double;

#define rep(i,N) for(int i=0;i<(int)(N);++i)
#define rrep(i,N) for(int i=(int)(N)-1;i>=0;--i)
#define debug(x) cout<<#x<<"="<<x<<endl;
constexpr ll MOD=1000000007;
constexpr ll INF=1LL<<60;
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;}
template<typename T> void fail(T v){cout << v << endl;exit(0);}
//template end
int W,H;
bool inside(int h,int w){return 0<=h&&h<H&&0<=w&&w<W;}
int gh[]={1,-1,0,0,1,1,-1,-1},gw[]={0,0,1,-1,-1,1,-1,1},use[51][51],G[51][51];
void dfs(int h,int w){
    rep(k,8){
        int i=gh[k]+h,j=gw[k]+w;
        if(!inside(i,j)||use[i][j]||G[i][j]==0)continue;
        use[i][j]=1;
        dfs(i,j);
    }
}
void solve(){
    while((cin>>W>>H,W|H)){
        rep(i,51)rep(j,51)use[i][j]=0;
        rep(i,H)rep(j,W)cin>>G[i][j];
        int res=0;
        rep(i,H)rep(j,W)if(!use[i][j]&&G[i][j]){dfs(i,j);res++;}
        cout<<res<<endl;
    }
}

int main(){
    cin.tie(0);
    ios::sync_with_stdio(false);
    cout << fixed << setprecision(20);
    solve();
    return 0;
}