Go: package main

import (
	"fmt"
)

func main() {
	var h, w int
	for {
		fmt.Scan(&w, &h)
		if w == 0 && h == 0 {
			return
		}
		matrix := make([][]int, h)
		Sean := make([][]bool, h)
		for i := 0; i < h; i++ {
			matrix[i] = make([]int, w)
			Sean[i] = make([]bool, w)
			for j := 0; j < w; j++ {
				fmt.Scan(&matrix[i][j])
			}
		}
		var count int
		for i := 0; i < h; i++ {
			for j := 0; j < w; j++ {
				if matrix[i][j] == 1 && Sean[i][j] == false {
					search(i, j, h, w, matrix, Sean)
					count++
				}
			}
		}
		fmt.Println(count)
	}
}
func search(x int, y int, h int, w int, matrix [][]int, Sean [][]bool) {
	if x < 0 || y < 0 || x >= h || y >= w || matrix[x][y] == 0 {
		return
	}
	if Sean[x][y] == true {
		return
	}

	Sean[x][y] = true

	search(x+1, y, h, w, matrix, Sean)
	search(x-1, y, h, w, matrix, Sean)
	search(x, y+1, h, w, matrix, Sean)
	search(x, y-1, h, w, matrix, Sean)
	search(x+1, y-1, h, w, matrix, Sean)
	search(x-1, y+1, h, w, matrix, Sean)
	search(x+1, y+1, h, w, matrix, Sean)
	search(x-1, y-1, h, w, matrix, Sean)
}

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

int flag[52][52];
int c[52][52];
std::vector<std::vector<int> > land;
std::vector<int> v(2);
int island;
int w, h;

bool search(int x, int y){
  flag[x][y] = 1;
  for(int dx = -1; dx < 2; dx++){
    for(int dy = -1; dy < 2; dy++){
      if(c[x + dx][y + dy] == 1 && flag[x + dx][y + dy] == 0){
        v[0] = x + dx;
        v[1] = y + dy;
        land.push_back(v);
        flag[x + dx][y + dy] = 1;
      }
    }
  }
  land.erase(land.begin());
  return land.empty();
}

int main(){
  while(true){
    std::cin >> w >> h;
    if(w == 0 && h == 0){
      break;
    }
    else{
      island = 0;
      for(int i = 1; i <= h; i++){
        for(int j = 1; j <= w; j++){
          std::cin >> c[j][i];
          flag[j][i] = 0;
        }
      }

      for(int i = 1; i <= w; i++){
        for(int j = 1; j <= h; j++){
          if(c[i][j] == 1 && flag[i][j] == 0){
            v[0] = i;
            v[1] = j;
            land.push_back(v);
            while(true){
              if(search(land[0][0], land[0][1])) break;
            }
            island++;
          }
        }
      }
      std::cout << island << std::endl;
    }
  }
  return 0;
}

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

int c[MAX][MAX];

void dfs(int w, int h, int x, int y);
int main(int argc, char const *argv[]){
    int w, h, ans;

    while (cin>>w>>h, w || h){
        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 < w; i++){
            for (int j = 0; j < h; j++){
                if (c[i][j] == 1){
                    dfs(w, h, i, j);
                    ans++;

                    for (int i = 0; i < w; i++){
                        for (int j = 0; j < h; j++){
                            cout<<c[i][j]<<" ";
                        }
                        cout<<'\n';
                    }
                    cout<<'\n';
                }
            }
        }
        cout<<ans<<endl;
    }

    return 0;
}

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

    for (int i = 0; i < 8; i++){
        int nx = x + dx[i];
        int ny = y + dy[i];

        if (0 <= nx && nx < w && 0 <= ny && ny < h && c[nx][ny] == 1){
            dfs(w, h, nx, ny);
        }
    }
    return;
}

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

int mp[50][50]={0};
int dx[8]={-1,-1,-1,0,0,1,1,1};
int dy[8]={-1,0,1,-1,1,-1,0,1};
 int W,H;
 void dfs(int w,int h);
int main(){
 
    while(true){
        
    int cnt=0;

    cin >>W >>H;
    if(W==0 && H==0) break;
    for (int i=0;i<H;i++){
        for(int j=0;j<W;j++){
            cin >> mp[i][j];
        }
    }
    
//    memcpy(mp1,mp,sizeof(mp));
    
    for (int i=0;i<H;i++){
        for(int j=0;j<W;j++){
            if (mp[i][j]==1){
                cnt++;
                dfs(j,i);
            }
        }
    }
   cout <<cnt<<endl;
    } 

}

void dfs(int w,int h){
    mp[h][w]=0;
    int nx,ny;
    for (int i=0;i<8;i++){
        nx=w+dx[i];ny=h+dy[i];
        if(0<=nx && nx<W && 0<=ny && ny<H && mp[ny][nx]==1){
            dfs(nx,ny);
        }

    }

}

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

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

bool dfs(int x,int y){
    if(visited[x][y]) return false;
    visited[x][y]=1;

    for(int d=0;d<8;d++){
        int i=x+dx[d];
        int j=y+dy[d];
        if(0<=i&&i<h&&0<=j&&j<w){
            if(c[i][j]){
                dfs(i,j);
            }  
        }
    }
    return true;
}

int main(){
    visited.assign(50,vector<int>(50,0));
    cin>>w>>h;
    for(int i=0;i<h;i++){
        for(int j=0;j<w;j++){
            cin>>c[i][j];
        }
    }
    int cnt=0;
    for(int i=0;i<h;i++){
        for(int j=0;j<w;j++){
            if(c[i][j]){
                cnt+=dfs(i,j);
            }
        }
    }

    cout<<cnt<<endl;
    return 0;
}

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

int h,w;
bool grid[100][100];

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

  for(int i=0;i<8;i++){
    int ty = y+dy[i], tx = x+dx[i];
    if(ty<0 || tx<0 || h<=ty || w<=tx)continue;
    if(grid[ty][tx])dfs(ty,tx);
  }
}

int main(){
  while(cin >> w >> h ,w||h){
    for(int i=0;i<h;i++)
      for(int j=0;j<w;j++)cin >> grid[i][j];

    int ans = 0;
    for(int i=0;i<h;i++)
      for(int j=0;j<w;j++)
	if(grid[i][j]){
	  dfs(i,j);
	  ans++;
	}

    cout << ans << endl;
  }
}

C++: #include <iostream>

using namespace std;

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

int sink(int x, int y) {
    if (map_[x][y] == 0) {
        return 0;
    }
    map_[x][y] = 0;
    for (int i=0; i<8; i++) {
        int x_new = x+dx[i];
        int y_new = y+dy[i];
        if (0 <= x_new && x_new < w && 0 <= y_new && y_new < h) {
            sink(x_new, y_new);
        }
    }
    return 1;
}

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

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

int w,h;
int dx[8] = {0,1,1,1,0,-1,-1,-1};
int dy[8] = {1,1,0,-1,-1,-1,0,1};
vector<vector<int>> mp;

void dfs(int y, int x){
  mp[y][x] = 0;
  for (int i = 0; i < 8; i++)
  {
    if(x+dx[i]>=w || x+dx[i]<0 || y+dy[i]>=h || y+dy[i]<0) continue;
    if(mp[y+dy[i]][x+dx[i]]) dfs(y+dy[i], x+dx[i]);
  }
}


int main(){
  vector<int> anss;
  while(true){
    cin >> w >> h;
    if(w+h==0) break;
    mp = vector<vector<int>>(h,vector<int>(w));
    for (int i = 0; i < h; i++)
    {
      for (int j = 0; j < w; j++)
      {
        cin >> mp[i][j];
      }    
    }

    int ans = 0;
    for (int i = 0; i < h; i++)
    {
      for (int j = 0; j < w; j++)
      {
        if(mp[i][j]){
          ans++;
          dfs(i,j);
        }
      }
    }
    anss.push_back(ans);
  }
  for(int ans:anss) cout << ans << endl;
}