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 <fstream>
#include <vector>
#include <deque>
#include <assert.h>
#include <queue>
#include <stack>
#include <set>
#include <map>
#include <stdio.h>
#include <string.h>
#include <utility>
#include <math.h>
#include <bitset>
#include <iomanip>
#include <complex>
#include <ios>
#include <iomanip>
using namespace std;
#define rng(i,a,b) for(int i=int(a);i<int(b);i++)
#define rep(i,b) rng(i,0,b)
#define gnr(i,a,b) for(int i=int(b)-1;i>=int(a);i--)
#define per(i,b) gnr(i,0,b)
#define ALL(x) (x).begin(),(x).end()
#define RALL(x) (x).rbegin(),(x).rend()
#define IDX(vec, element_iter) distance((vec).begin(), element_iter)
#define print(x) cout<<(x)<<'\n'
#define print_vec(v) rep(i,v.size()){if(i!=0)cout<<" ";cout<<v[i];}cout<<'\n'
#define pe(x) cout<<(x)<<" "
#define DEBUG(x) cout<<#x<<": "<<x<<'\n'
#define DEBUG_VEC(v) cout<<#v<<":";rep(i,v.size())cout<<" "<<v[i];cout<<'\n'
#define pb push_back
#define mp make_pair
#define MOD 1000000007 //10^9+7
#define fi first
#define sc second
using ll=long long;
using pi=pair<int,int>;
using pll=pair<ll,ll>;
using vi=vector<int>;
using vll=vector<ll>;
using vstr=vector<string>;


int w,h,c[50][50];
int dx[] = {-1,0,1,-1,1,-1,0,1};
int dy[] = {1,1,1,0,0,-1,-1,-1};

bool inRange(int y,int x){
    if(0<=x && x<w && 0<=y && y<h) return true;
    else return false;
}

void dfs(int x,int y){
    c[x][y] = 0;
    rep(i,8){
        int x_next=x+dx[i],y_next=y+dy[i];
        if(inRange(x_next,y_next) && c[x_next][y_next]==1){
            dfs(x_next,y_next);
        }
    }
}

void solve() {
    while(1){
        cin >> w >> h;
        if(w==0 && h==0) break;
        rep(row,h){
            rep(col,w){
                cin >> c[row][col];
            }
        }

        int ans = 0;
        rep(row,h){
            rep(col,w){
                if(c[row][col]==1){
                    ans++;
                    dfs(row,col);
                }
            }
        }

        print(ans);
    }
}
 
int main() {
    cin.tie(nullptr);
    ios_base::sync_with_stdio(false);
	cout<<fixed<<setprecision(20);
    //int q; cin >> q;
	//while (q--)
		solve();
    return 0;
}

C++: #include<iostream>
using namespace std;
const int MAXW = 51;
const int MAXH = 51;
int map[MAXW][MAXH];
int w,h;
int dx[] = {-1,0,1,-1,1,-1,0,1};
int dy[] = {1,1,1,0,0,-1,-1,-1};
int dfs(int x,int y){
  if(map[x][y] == 0)return 0;
  map[x][y] = 0;
  for(int i = 0;i < 9;i++){
    int nx = x+dx[i];
    int ny = y+dy[i];
    if(nx >= 0 && nx < w & ny >= 0 && ny < h)
      dfs(nx,ny);
  }
  return 1;
}

int main(){
  while(true){
    cin >> w >> h;
    if(w == 0 && h == 0)break;
    for(int i = 0;i < h;i++){
      for(int j = 0;j < w;j++){
        cin >> map[j][i];
      }
    }
    int count = 0;
    for(int i = 0;i < h;i++){
      for(int j = 0;j < w;j++){
        count += dfs(j,i);
      }
    }
    cout << count << endl;
  }
}

C++: #include<iostream>

using namespace std;

const int MaxI = 51;
int C[MaxI][MaxI];
int Dh[] = {-1,-1,-1,0,0,1,1,1};
int Dw[] = {-1,0,1,-1,1,-1,0,1};
int H, W;

void dfs(int i, int j);

int main() {

  while(cin>>W>>H && !(W==0 && H==0)) {
    for(int i=0; i<H; i++) {
      for(int j=0; j<W; j++) {
	cin>>C[i][j];
      }
    }

    int ans = 0;
    for(int i=0; i<H; i++) {
      for(int j=0; j<W; j++) {
	if(C[i][j] == 1) {
	  dfs(i,j);
	  ans++;
	}
      }
    }
    cout<<ans<<endl;
  }
}

void dfs(int i, int j) {
  C[i][j] = 0;
  for(int a=0; a<8; a++) {
    if(0<=i+Dh[a] && i+Dh[a]<H && 0<=j+Dw[a] && j+Dw[a]<W && C[i+Dh[a]][j+Dw[a]] == 1) {
      dfs(i+Dh[a], j+Dw[a]);
    }
  }
}

C++: #include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef pair<int, int> P;
#define rep(i, n) for (int i = 0; i < (int)(n); i++)
#define MOD (int) (1e9+7)
#define INF (int) 2e9

bool seen[50*50];
vector<int> edges[50*50];

void dfs(int s, int p){
    seen[s] = true;
    for (auto v: edges[s]){
        if(v==p) continue;
        if(!seen[v]) dfs(v, s);
    }
}


int main(){
    int w, h;
    int di[8] = {-1, -1, -1, 0, 0, 1, 1, 1};
    int dj[8] = {-1, 0, 1, -1, 1, -1, 0, 1};
    
    while(true){
        cin >> w >> h;
        if(w==0 && h==0) break;
        int c[h][w];
        rep(i, h){
            rep(j, w){
                cin >> c[i][j];
                edges[w*i+j] = {};
            }
        }

        // 隣接リスト構築
        rep(i, h){
            rep(j, w){
                if(c[i][j]==1){
                    rep(k, 8){
                        int i_ = i+di[k];
                        int j_ = j+dj[k];
                        if(i_>=0 && i_<h && j_>=0 && j_<w && c[i_][j_]==1){
                            int idx = w * i_ + j_;
                            edges[w*i+j].emplace_back(idx);
                            // edges[idx].emplace_back(w*i+j);
                        }
                    }
                }
            }
        }

        rep(i, h){
            rep(j, w){
                seen[w*i+j] = false;
            }
        }

        // ここからdfs実行
        int cnt = 0;
        rep(i, h){
            rep(j, w){
                int num = w*i+j;
                if(c[i][j]==1 && !seen[num]){
                    dfs(num, -1);
                    cnt++;
                }
            }
        }
        cout << cnt << endl;
    }

    // printf("%d\n", N);
    return 0;
}