Go: package main

import (
	"bufio"
	"fmt"
	"io"
	"os"
	"strconv"
)

const (
	initialBufSize = 100000
	maxBufSize     = 1000000
)

var sc *bufio.Scanner

func initScanner(r io.Reader) *bufio.Scanner {
	buf := make([]byte, initialBufSize)

	sc := bufio.NewScanner(r)
	sc.Buffer(buf, maxBufSize)
	sc.Split(bufio.ScanWords) // bufio.ScanLines
	return sc
}

func main() {
	sc = initScanner(os.Stdin)
	resolve()
}

func resolve() {
	results := []int{}
	for true {
		w, h := scanInt(sc), scanInt(sc)
		if w == 0 && h == 0 {
			break
		}
		grid := make([][]int, h)
		for i := 0; i < h; i++ {
			grid[i] = make([]int, w)
			for j := 0; j < w; j++ {
				grid[i][j] = scanInt(sc)
			}
		}
		num := 0
		for i := range loop0(h) {
			for j := range loop0(w) {
				start := pos{y: i, x: j}
				if grid[start.y][start.x] == 0 {
					continue
				}
				num++
				_dfs(grid, w, h, start)
			}
		}
		results = append(results, num)
	}
	for _, v := range results {
		fmt.Println(v)
	}
	return
}

func _dfs(grid [][]int, w, h int, start pos) {
	grid[start.y][start.x] = 0
	for _, df := range []pos{
		pos{1, 0}, pos{-1, 0},
		pos{0, -1}, pos{0, 1},
		pos{1, 1}, pos{-1, -1},
		pos{-1, 1}, pos{1, -1},
	} {
		np := addPos(start, df)
		if np.y < 0 || h <= np.y || np.x < 0 || w <= np.x {
			continue
		}
		if grid[np.y][np.x] == 0 {
			continue
		}
		_dfs(grid, w, h, np)
	}
}

type pos struct{ x, y int }

func addPos(p, d pos) pos {
	return pos{
		x: p.x + d.x,
		y: p.y + d.y,
	}
}

func loop0(n int) chan int {
	return loop(0, n-1, 1)
}

func loop1(n int) chan int {
	return loop(1, n, 1)
}

func loop(s, e, d int) chan int {
	ch := make(chan int)
	go func() {
		for i := s; i <= e; i += d {
			ch <- i
		}
		close(ch)
	}()
	return ch
}

// snip-scan-funcs
func scanInt(sc *bufio.Scanner) int {
	sc.Scan()
	i, _ := strconv.Atoi(sc.Text())
	return int(i)
}
func scanString(sc *bufio.Scanner) string {
	sc.Scan()
	return sc.Text()
}

C++: #include <iostream>
using namespace std;
void kazoeruyo(int p,int P,int box[50][50]){
  box[p][P]=0;
    if(box[p][P+1]==1) {
      box[p][P+1]=0;
      P=P+1; 
      kazoeruyo(p,P,box);
    }
    if(box[p][P-1]==1){
      box[p][P-1]=0;
      P=P-1;
      kazoeruyo(p,P,box);
    }
    if(box[p+1][P]==1){ 
      box[p+1][P]=0;
      p=p+1;
      kazoeruyo(p,P,box);
    }
    if(box[p-1][P]==1){
      box[p-1][P]=0;
      p=p-1;
      kazoeruyo(p,P,box);
    }
    if(box[p-1][P-1]==1){
      box[p-1][P-1]=0;
      p=p-1;
      P=P-1;
      kazoeruyo(p,P,box);
    }
    if(box[p-1][P+1]==1){
      box[p-1][P+1]=0;
      p=p-1;
      P=P+1;
      kazoeruyo(p,P,box);
    }
    if(box[p+1][P+1]==1){
      box[p+1][P+1]=0;
      p=p+1;
      P=P+1;
      kazoeruyo(p,P,box);
    }
    if(box[p+1][P-1]==1){
      box[p+1][P-1]=0;
      p=p+1;
      P=P-1;
      kazoeruyo(p,P,box);
    }	
}
int main(){
  int w,h,i,I,p,P,o,t,T,y,Y;
  int box[10][10];
  while(1){
    cin>>w>>h;
    if(w==0&&h==0) break;
    for(i=0;i<h;i++){
      for(I=0;I<w;I++){
	cin>>box[i][I];
      }
    }
    o=0;
    for(i=0;i<h;i++){
      for(I=0;I<w;I++){
	if(box[i][I]==1) {
	  p=i;
	  P=I;
	  kazoeruyo(p,P,box);
	  box[p][P]=1;
	}
      }
    }
    for(i=0;i<h;i++){
      for(I=0;I<w;I++){
	if(box[i][I]==1) o++;
      }
    }
    cout<<o<<endl;
    o=0;
  }
  return 0;
}

C++: #include<bits/stdc++.h>
using namespace std;

int w,h;
int maps[50][50];

void dfs(int y, int x)
{
  int dx[]={1,-1,0,0};
  int dy[]={0,0,1,-1};

  maps[y][x]=0;

  for(int i=0;i < 4;i++){
    for(int j=0;j < 4;j++){
      int xx = x + dx[j];
      int yy = y + dy[i];
      if(maps[yy][xx] == 1) dfs(yy,xx);
    }
  }
}
int main(){
  while(cin >> w >> h && w)
    {
      for(int i=0;i < h;i++){
	for(int j=0;j < w;j++){
	  cin >> maps[i][j];
	}
      }
      int island=0;

      for(int i=0;i < h;i++){
	for(int j=0;j < w;j++){
	  if(maps[i][j] == 1){
	    island++;
	    dfs(i,j);
	  }
	}
      }
      cout << island << endl;
    }
  return 0;
}

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

using namespace std;

pair<int,int> finish(vector<vector<int>> a,int h,int w){
  for(int i=0;i<h;i++){
    for(int j=0;j<w;j++){
      if(a[i][j]==1){return make_pair(i,j);}
    }
  }
  return make_pair(h,w);
}

//沈めてく
void idlands(int H,int W){
  vector<vector<int>> d(H,vector<int>(W,0));
  queue< pair<int,int> > x;
  for(int i=0;i<H;i++){
    for(int j=0;j<W;j++){
      cin >> d[i][j];
    }
  }
  int l=1;
  int c=0;
  while(finish(d,H,W)!=make_pair(H,W)){
    x.push(finish(d,H,W));
    c+=1;l=x.size();d[x.front().first][x.front().second]=0;
    while(l!=0){
      for(int i=0;i<l;i++){
        int h,w;h=x.front().first;w=x.front().second;
        if(0<h and d[h-1][w]==1){
          x.push(make_pair(h-1,w));d[h-1][w]=0;}
        if(h<H-1 and d[h+1][w]==1){
          x.push(make_pair(h+1,w));d[h+1][w]=0;}
        if(0<w and d[h][w-1]==1){
          x.push(make_pair(h,w-1));d[h][w-1]=0;}
        if(w<W-1 and d[h][w+1]==1){
          x.push(make_pair(h,w+1));d[h][w+1]=0;}
        if(0<h and w<W-1 and d[h-1][w+1]==1){
          x.push(make_pair(h-1,w+1));d[h-1][w+1]=0;}
        if(h<H-1 and 0<w and d[h+1][w-1]==1){
          x.push(make_pair(h+1,w-1));d[h+1][w-1]=0;}
        if(0<h and 0<w and d[h-1][w-1]==1){
          x.push(make_pair(h-1,w-1));d[h-1][w-1]=0;}
        if(h<H-1 and w<W-1 and d[h+1][w+1]==1){
          x.push(make_pair(h+1,w+1));d[h+1][w+1]=0;}
        x.pop();
      }
      l=x.size();
    }
  }
  cout << c << endl;
}

int main(){
  int W,H;
  while(cin >> W >> H and W!=0){
    idlands(H,W);
  }
}

C++: #include<bits/stdc++.h>
using namespace std;
#define fs first
#define sc second
#define pb push_back
#define mp make_pair
#define eb emplace_back
#define ALL(A) A.begin(),A.end()
#define RALL(A) A.rbegin(),A.rend()
typedef long long ll;
typedef pair<ll,ll> P;
const ll mod=1000000007;
const ll LINF=1LL<<60;
const int INF=1<<30;

vector<vector<int>> c(51,vector<int>(51,0));
int w,h;

void search(int x,int y){
	c[x][y] = 0;
	for(int i = -1; i <= 1; i++) {
		for(int j = -1; j <= 1; j++) {
			if (x + i >= 0 && x + i < h && y + j >= 0 && y + j < w && c[x + i][y + j] == 1){
				search(x + i,y + j);
			}
		}
	}
}

int main(){
	while (1) {
		cin>>w>>h;
		if (w == 0 && h == 0){
			break;
		}
		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++) {
	// 		cout << c[i][j];
	// 	}
	// 	cout << endl;
	// }
		for(int i = 0; i < h; i++) {
			for(int j = 0; j < w; j++) {
				if (c[i][j] == 1){
					search(i,j);
					ans++;
				}
			}
		}
		cout << ans << endl;
	}
	return 0;
}

C++: 
#include <cstdio>
#include <iostream>
#include <algorithm>
#include <cstdlib>
#include <cmath>
#include <vector>
#include <ctime>
#include <string>
#include <map>
#include <stack>
#include <queue>

using namespace std;

int w, h;
const int MAX = 50;
int c[MAX][MAX];


void check(int x, int y){
	c[x][y] = 0;
    for(int dx = -1; dx <= 1; dx++){
        for(int dy = -1; dy <= 1; dy++){
            if(x + dx >= 0 && x + dx <= h  && y + dy >= 0 && y + dy <= w && c[x + dx][y + dy] == 1){
            	check(x + dx, y + dy);
            } 
        }
    }
	
}

int main(void){

	for(int i = 0; i < MAX ; i++){
		for(int j = 0; j < MAX; j++){
			c[i][j] = 0;
		}
	}


	while((cin >> w) != 0 && (cin >> h) != 0){
		int ans = 0;
		for(int i = 0; i < h ; i++){
			for(int j = 0; j < w; j++){
				cin >> c[i][j];
			}
		}

		for(int i = 0; i < h ; i++){
			for(int j = 0; j < w; j++){
				if(c[i][j] == 1){
					check(i, j);
					ans++;
				}
			}
		}
		cout << ans << endl;
	}
	
    return 0;
}