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 <bits/stdc++.h>
using namespace std;
#define rep(i,n) for(int i=0;i<n;i++)

int n,m;
int v[100][100];
bool used[100][100];
int dir[8][2]={{1,0},{1,1},{0,1},{-1,1},{-1,0},{-1,-1},{0,-1},{1,-1}};

void dfs(int i, int j){
    if((v[i][j]==0) || (used[i][j]==true)){
        return;
    }
    used[i][j]=true;
    rep(k,8){
        if(i+dir[k][0]<=n-1 && 0<=i+dir[k][0]){
            if(j+dir[k][1]<=m-1 && 0<=j+dir[k][1]){
                dfs(i+dir[k][0],j+dir[k][1]);
            }
        }
    }
    return;
}

int main(){
    vector<int> ans;

    while(true){
        cin >> m >> n;
        if(n==0 && m==0)break;
        rep(i,n){
            rep(j,m){
                cin >> v[i][j];
            }
        }

        int res=0;
        rep(i,n){
            rep(j,m){
                used[i][j]=false;
            }
        }
        rep(i,n){
            rep(j,m){
                if(v[i][j]==1 && used[i][j]==false){
                    dfs(i,j);
                    res++;
                }
            }
        }
        ans.push_back(res);
    }

    rep(i,ans.size()){
        cout << ans[i] << endl;
    }
    return 0;
}

C++: #include<iostream>
using namespace std;
bool ban[52][52];
	void dfs(int x,int y){
		ban[y][x]=0;
		for(int i=-1;i<=1;i++){
			for(int j=-1;j<=1;j++){
				if(ban[y+i][x+j]==true){
					dfs(x+j,y+i);
				}
			}
		}
	}
int main(){
	int w,h;
	while(cin>>w>>h,h+w!=0){
		for(int i=0;i<h;i++)ban[i][0]=0,ban[i][h+1]=0;//番兵
		for(int i=0;i<w;i++)ban[0][i]=0,ban[0][w+1]=0;//
		for(int i=1;i<=h;i++){
			for(int j=1;j<=w;j++)cin>>ban[i][j];
		}
	int cnt=0;
	for(int i=1;i<=h;i++){
		for(int j=1;j<=w;j++){
			if(ban[i][j]==true){
				dfs(j,i);
				cnt++;
			}
		}	
	}
		cout<<cnt<<endl;
	}
	return 0;
}

C++: #include <bits/stdc++.h>
#define int long long
using namespace std;
int d[51][51];
int used[51][51];
typedef pair<int,int> P;

signed main(){
  int W,H;
  std::vector<int> fans;
  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>>d[i][j];
    for(int i=0;i<51;i++)for(int j=0;j<51;j++)used[i][j]=0;
    int ans=0;
    for(int i=0;i<H;i++){
      for(int j=0;j<W;j++){
        if(used[i][j]||d[i][j]==0)continue;
        used[i][j]=1;ans++;
        queue<P> que;que.push(P(i,j));
        while(que.size()){
          P p=que.front();que.pop();
          for(int k=max(0LL,p.first-1);k<=min(H-1,p.first+1);k++){
            for(int l=max(0LL,p.second-1);l<=min(W-1,p.second+1);l++){
              if(used[k][l]==0&&d[k][l]){
                used[k][l]++;que.push(P(k,l));
              }
            }
          }
        }
      }
    }
    fans.push_back(ans);
  }
  for(int i=0;i<fans.size();i++)cout<<fans[i]<<endl;
}

C++: #include <iostream>
#include <math.h>
#include <algorithm>
#include <vector>
using std::cin;
using std::cout;
using std::endl;
using std::string;
using std::vector;
				
				
void del_island(int i,int j,vector<vector<int>> &v){	
	v[i][j] = 0;
	if(v[i-1][j] == 1){
		del_island(i-1,j,v);
	}
	if(v[i+1][j] == 1){
		del_island(i+1,j,v);
	}
	if(v[i][j-1] == 1){
		del_island(i,j-1,v);
	}
	if(v[i][j+1] == 1){
		del_island(i,j+1,v);
	}
	if(v[i-1][j-1] == 1){
		del_island(i-1,j-1,v);
	}
	if(v[i-1][j+1] == 1){
		del_island(i-1,j+1,v);
	}
	if(v[i+1][j-1] == 1){
		del_island(i+1,j-1,v);
	}
	if(v[i+1][j+1] == 1){
		del_island(i+1,j+1,v);
	}

}


int count_island(int w, int h,vector<vector<int>> v){
	int count = 0;
	for(int i = 1 ; i <= h ;i++){
		for(int j = 1 ;j <= w ;j++){
			//cout << v[i][j] ;
			if(v[i][j] == 1){
				count++;
				//cout << "hello" <<endl;
				del_island(i,j,v);
			}
		}
	}
	return count;
}
	
int main(){ 
	int w,h;
	vector<vector<int>> v(60,vector<int>(60,0));
	int count;

	while(1){
		cin >> w >> h;
		if(w == 0 && h == 0){
			break;
		}
		count = 0; 
		for(int i = 1; i <= h ; i++){
			for(int j = 0 ; j <= w ; j++){
				v[i][j] = 0;
			}
		}

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

		count = count_island(w,h,v);


		cout << count << endl;
	}
}

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

int N,M;
char filed[100][101];

void dfs(int x,int y){
  filed[x][y]='.';
  
  for(int dx=-1;dx<=1;dx++){
    for(int dy=-1;dy<=1;dy++){
      int nx=x+dx,ny=y+dy;
      if(0<=nx&&nx<N&&0<=ny&&ny<M&&filed[nx][ny]=='W')dfs(nx,ny);
    }
  }
}

void solve(){
  int res=0;
  for(int i=0;i<N;i++){
    for(int j=0;j<M;j++){
      if(filed[i][j]=='W'){
	dfs(i,j);
	res++;
      }
    }
  }
  cout<<res<<endl;
}
int main(){
  cin>>N>>M;
  for(int i=0;i<N;i++){
    for(int j=0;j<M;j++){
      cin>>filed[i][j];
    }
  }
  
  solve();
  return 0;
}