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 <algorithm>
#include <vector>
using namespace std;
//int cnt = 0;

void depth_search(int ,int,vector< vector<int> > &);
void show(vector<vector <int> > &umi){
  return ;
  cout << "---------------------------" << endl;
  for(int i = 0;i < umi[0].size();i++){
    for(int j = 0;j < umi.size();j++){
      cout << umi[j][i];
    }
    cout << endl;
  }
    cout << "---------------------------" << endl;
  
}
int main(){
  int w,h;
  while(true){
    cin >> w;
    cin >> h;
    if(w == 0 && h == 0)return 0;
    vector< vector<int> > umi(w,vector<int>(h));
    //    cout << "aaaaaaaaaa" << endl;
    for(int i = 0;i < h;i++){
      for(int j = 0;j < w;j++){
	cin >> umi[j][i];

      }
    } 

    int cnt = 0;
    for(int i = 0;i < w;i++){
      for(int j = 0;j < h;j++){
	if( umi[i][j] == 1){
	  cnt++;
	  umi[i][j] = 3;
	  depth_search(i,j,umi);
	  show(umi);

	}
      }
    }
    cout << cnt << endl;
  }
  return 0;
}
void depth_search(int x,int y,vector< vector<int> > &umi){
  int dx[3] = {0,1,-1};
  int dy[3] = {0,1,-1};
  for(int i = 0;i < 3;i++){
    for(int j = 0;j < 3;j++){
      if(!(x+dx[i] == -1
	 ||x+dx[i] == umi.size()
	 ||y+dy[j] == -1
	 ||y+dy[j] == umi[0].size())
	 && (umi[x+dx[i]][y+dy[j]] == 1)
	 && !(dx[i] == 0 && dy[j] == 0)){
	umi[x+dx[i]][y+dy[j]] = 3;
	depth_search(x+dx[i],y+dy[j],umi);
	
      }
    }
  }
}

C++: #include <iostream>
#include <vector>
#include <string>
#include <queue>

#define rep(i, n) for(int i = 0; i < (n); i++)
#define MP make_pair
#define X first
#define Y second

using namespace std;

typedef vector<int> vi;
typedef long long ll;
typedef pair<int, int> P;

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

int main(){
	//freopen("test.txt", "r", stdin);
	int w, h;
	while(cin >> w >> h, w|h){
		vector<vi> v(h, vi(w, 0));
		rep(i, h){
			rep(j, w){
				cin >> v[i][j];
			}
		}

		int ans = 0;
		rep(i, h){
			rep(j, w){
				if(v[i][j] != 1)continue;
				ans++;
				queue<P> q;
				q.push(MP(j,i));
				while(!q.empty()){
					P p = q.front();
					q.pop();
					if(v[p.Y][p.X] != 1) continue;
					v[p.Y][p.X] = 0;
					rep(k, 8){
						int x = p.X+dx[k];
						int y = p.Y+dy[k];
						if(x < 0 || x >= w || y < 0 || y >= h) continue;
						q.push(MP(x, y));
					}
				}
			}
		}
		cout << ans << endl;
	}


	return 0;
}

C++: #include <bits/stdc++.h>
#define rep(i, n) for (int i = 0; i < (n); i++)
using namespace std;
//using ll = long long;

int h, w;
vector< vector<int> > g;
//vector<vector<bool>> v;

void dfs(int y, int x) {
  g[y][x] = 0;
  for (int i = -1; i <= 1; i++) {
    for (int j = -1; j <= 1; j++) {
      int ny = y + i;
      int nx = x + j;
      if (ny < 0 || ny >= h || nx < 0 || nx >= w) continue;
      if (g[ny][nx] == 0) continue;
      dfs(ny, nx);
    }
  }
}

int main() {
  while (cin >> w >> h) {
    if (h == 0 || w == 0) break;
    g.assign(h, vector<int>(w, 0));
    rep(i, h) rep(j, w) {
      cin >> g[i][j];
    }
    //v.assign(h, vector<bool>(w, false));
    int cnt = 0;
    rep(i, h) rep(j, w) {
      if (g[i][j] == 0) continue;
      dfs(i, j);
      cnt++;
    }
    cout << cnt << endl;
  }
  return 0;
}

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

using namespace std;

const int ddx[8] = {1,1,1,0,0,-1,-1,-1};
const int ddy[8] = {1,0,-1,1,-1,1,0,-1};

bool visited[60][60] ={false};
int v[60][60] = {0};
int h,w;

int dfs(int i ,int j){
    if(visited[i][j]) return 0;
    
    visited[i][j] = true;

    for(int x = 0; x < 8; x++){
        for(int y = 0; y < 8;y++){
            int posx = j + ddx[x];
            int posy = i + ddy[y];
            if(posx >= 0 && posx < w && posy >= 0 && posy < h){
                if(!visited[posy][posx] && v[posy][posx]){
                    dfs(posy, posx);
                }
            }
        }
    }
    return 1;
}

int main()
{
    while(cin >> w >> h, w and h){
        int ans = 0;

        for(int i = 0; i < 60 ;i++){
            for(int j = 0;j < 60; j++){
                visited[i][j] = false;
            }
        }

        for(int i = 0; i < h ;i++){
            for(int j = 0;j < w; j++){
                cin >> v[i][j];
            }
        }

        for(int i = 0; i < h ;i++){
            for(int j = 0;j < w; j++){
                if(v[i][i] || !visited[i][j]) ans += dfs(i,j);
            }
        }
        cout << ans << endl;
    }
    return 0;
}

C++: #include <bits/stdc++.h>
#include <algorithm>
#include <stdlib.h>
using namespace std;
int W, H; 
char C[60][60] = {}; 
void search(int x, int y){
	C[x][y] = '0'; 
	for(int dx = -1; dx <= 1; dx++){
		for(int dy = -1; dy <= 1; dy++){
			int nx = x + dx; int ny = y + dy; 
			if(nx >= 0 && nx < H && ny >= 0 && ny < W && C[nx][ny] == '1') search(nx, ny); 
		}
	}
	return; 
}
int main() {
	for(int K; ; K++){
	cin >> W >> H; 
	if(W == 0){break;}
	for(int i = 0; i < H; i++){
		for(int j = 0; j < W; j++){
			cin >> C[i][j]; 
		}
	}
	int count = 0; 
	for(int i = 0; i < H; i++){
		for(int j = 0; j < W; j++){
			if(C[i][j] == '1'){search(i, j); count ++; }
		}
	}
	for(int i = 0; i < H; i++){
		for(int j = 0; j < W; j++){
			C[i][j] = '0'; 
		}
	}
	cout << count << endl; 
	}
}