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
//------------------------------------------
#include <vector>
#include <list>
#include <map>
#include <set>
#include <deque>
#include <stack>
#include <bitset>
#include <algorithm>
#include <functional>
#include <numeric>
#include <utility>
#include <sstream>
#include <iostream>
#include <iomanip>
#include <cstdio>
#include <cmath>
#include <cstdlib>
#include <cctype>
#include <string>
#include <cstring>
#include <ctime>
#include <climits>
#include <queue>

using namespace std;

//typedef
//------------------------------------------
typedef vector<int> VI;
typedef vector<VI> VVI;
typedef vector<string> VS;
typedef pair<int, int> PII;
typedef long long LL;

//container util
//------------------------------------------
#define ALL(a)  (a).begin(),(a).end()
#define RALL(a) (a).rbegin(), (a).rend()
#define PB push_back
#define MP make_pair
#define SZ(a) int((a).size())
#define EACH(i,c) for(typeof((c).begin()) i=(c).begin(); i!=(c).end(); ++i)
#define EXIST(s,e) ((s).find(e)!=(s).end())
#define SORT(c) sort((c).begin(),(c).end())

//repetition
//------------------------------------------
#define FOR(i,a,b) for(int i=(a);i<(b);++i)
#define REP(i,n)  FOR(i,0,n)

//constant
//--------------------------------------------
const double EPS = 1e-10;
const double PI  = acos(-1.0);

bool tab[60][60];
int dx[] = {-1,0,1,-1,1,-1,0,1};
int dy[] = {-1,-1,-1,0,0,1,1,1};
void dfs(int x, int y){
  if(!tab[y][x]) return;
  tab[y][x] = false;
  for(int i=0;i<8;++i)
	dfs(x+dx[i], y+dy[i]);
}

int main(){
  cin.tie(0);
  ios_base::sync_with_stdio(false);

  while(true){
	int W, H; cin >> W >> H;
	if(W == 0) break;
	int ans = 0;
	fill((bool*)tab, (bool*)tab+60*60, false);
	REP(y,H) REP(x,W){
	  int c; cin >> c;
	  tab[y+1][x+1] = c == 1;
	}
	for(int y=1;y<=H;++y)
	  for(int x=1;x<=W;++x)
		if(tab[y][x]){
		  ++ans;
		  dfs(x,y);
		}
	cout << ans << endl;
  }
  
  return 0;
}

C++: #include <bits/stdc++.h>
using namespace std;
vector<vector<int> > c;
vector<vector<bool> > done;
int w,h;
int dy[] = {-1,-1,-1,0,0,1,1,1};
int dx[] = {1,0,-1,1,-1,1,0,-1};

void dfs(int y, int x)
{
		done[y][x] = true;
		for(int i=0;i<8;i++)
		{
				int ny = y + dy[i];
				int nx = x + dx[i];
				if(0<=ny && ny<h && 0<=nx && nx<w && c[ny][nx]==1 && !done[ny][nx])
				{
						dfs(ny,nx);
				}
		}
}

signed
main()
{
	while(cin >> w >> h and !(w==0 & h==0))
	{
			c = vector<vector<int> >(h,vector<int>(w));
			done = vector<vector<bool> >(h,vector<bool>(w, false));
			for(int y=0;y<h;y++)
					for(int x=0;x<w;x++)
							cin >> c[y][x];

			int cnt = 0;
			for(int y=0;y<h;y++)
			{
					for(int x=0;x<w;x++)
					{
							if(c[y][x] && !done[y][x]){
									cnt++;
									dfs(y,x);
							}
					}
			}
			cout << cnt << endl;
	}
}

C++: #include <iostream>
#include <algorithm>
#include <cstring>
using namespace std;
int dx[] = {-1,-1,-1,0,0,1,1,1};
int dy[] = {0,1,-1,1,-1,0,1,-1};
bool visited[100][100];
int w,h;
int f[100][100];
void dfs(int x,int y){
  visited[y][x] = true;
  for(int i=0;i<8;++i){
    int mx=x+dx[i];
    int my=y+dy[i];
    if(mx>=w||my>=h||mx<0||my<0) continue;
    if(visited[my][mx] || !f[my][mx]) continue;
    dfs(mx,my);
  }
}

int main(){
  while(cin>>w>>h,w||h){
    memset(visited,0,sizeof(visited));
    for(int i=0;i<h;++i)
      for(int j=0;j<w;++j) cin >> f[i][j];
    int cnt = 0;
    for(int i=0;i<h;++i)
      for(int j=0;j<w;++j){
	if(f[i][j]&&!visited[i][j]){
	  ++cnt;
	  dfs(j,i);
	}
      }
    cout << cnt << endl;
  }
  return 0;
}

C++: #include <iostream>
#include <vector>
using namespace std;
int w, h;
//?§???????
const int mx[] = {-1, 0, 1, -1, 1, -1, 0, 1 };
const int my[] = { 1, 1, 1, 0, 0, -1, -1, -1};
vector<int> vx(begin(mx), end(mx));
vector<int> vy(begin(my), end(my));
vector<vector<int>> area;
int islands = 0;

//????±????????????¨????????¢?´¢
void dfs(int i,int j){
	for (int n = 0; n < vx.size(); n++){
		int x = j + vx[n];
		int y = i + vy[n];
		if (x >= 0 && x < w &&
			y >= 0 && y < h &&
			area[y][x] == 1){
			area[y][x] = 2;
			dfs(y,x);
		}
	}
}

int main(){
	while (1){
		cin >> w >> h;
		if (w == 0 && h == 0) break;
		//?????????
		islands = 0;
		area.clear();
		area = vector<vector<int>>(h, vector<int>(w));
		//??\???
		for (int i = 0; i < h; i++){
			for (int j = 0; j < w; j++){
				cin >> area[i][j];
			}
		}
		//??¨??¢?´¢
		for (int i = 0; i < h; i++){
			for (int j = 0; j < w; j++){
				if (area[i][j] == 1){
					islands++;
					area[i][j] = 2;
					dfs(i, j);
				}
			}
		}
		cout << islands << endl;
	}
	return 0;
}

C++: #include<iostream>
using namespace std;
#include<vector>

int wm,hm;
vector< vector<int> > wh(51, vector<int>(51,0));

int xp[]={-1,0,1,-1,1,-1,0,1};
int yp[]={-1,-1,-1,0,0,1,1,1};

void dfs(int y,int x){
  for(int i=0;i<8;i++){
    int nx=x+xp[i];
    int ny=y+yp[i];
    if(nx>-1&&nx<wm&&ny>-1&&ny<hm&&wh[ny][nx]==1){
      //cout<<nx<<" "<<ny<<" "<<wh[nx][ny]<<endl;
      wh[ny][nx]=0;
      dfs(ny,nx);
    }
  }
}

int main(){
  while(1){
    //int wm,hm;
    int aa,bb;
    cin>>aa>>bb;
    wm=aa;
    hm=bb;
    if(wm==0&&hm==0)break;
    //vector< vector<int> > wh(wm, vector<int>(hm));
    for(int i=0;i<hm;i++){
      for(int j=0;j<wm;j++){
        int a;
        cin>>a;
        wh[i][j]=a;
      }
    }
    int ans=0;
    for(int i=0;i<hm;i++){
      for(int j=0;j<wm;j++){
        if(wh[i][j]==1){
          //cout<<"*"<<i<<" "<<j<<endl;
          wh[i][j]=0;
          dfs(i,j);
          ans++;
        }
      }
    }
    cout<<ans<<endl;
  }
  return 0;
}