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<fstream>

using namespace std;

	ifstream fin;
	ofstream fout;

	//http://judge.u-aizu.ac.jp/onlinejudge/description.jsp?id=1160&lang=jp

	int gomi = 0;

	int a[52][52] = {};
	int check(int, int);

	int main(){

		fin.open("input.txt");
		fout.open("output.txt");

		//		int w = 0, h = 0;
		int width = 0, height = 0;
		int count = 0;


		while (1){
			for (int i = 0; i < 50; i++)
				for (int j = 0; j < 50; j++)
					a[i + 1][j + 1] = 0;

			cin >> width >> height;
			if (width == 0 && height == 0)break;

			for (int i = 0; i < height; i++){
				for (int j = 0; j < width; j++){
					//初期化
					gomi = a[i + 1][j + 1] = 0;

					cin >> a[i + 1][j + 1];
				}
			}

			for (int i = 0; i < height; i++){
				for (int j = 0; j < width; j++){
					if (a[i + 1][j + 1] == 1){
						check(i + 1, j + 1);
						a[i + 1][j + 1] = 1;
						count++;
					}
				}
			}
			cout << count << endl;
			fout << count << endl;

			//初期化
			count = 0;
			for (int i = 0; i < height; i++){
				for (int j = 0; j < width; j++){
					a[i + 1][j + 1] = 0;
				}
			}
		}
	}

	int check(int y, int x){
		for (int i = -1; i <= 1; i++){
			for (int j = -1; j <= 1; j++){
				if (i + j != 0 || i - j != 0){
					if (a[y + i][x + j] == 1){
						a[y + i][x + j] = 0;
						check(y + i, x + j);
					}
				}
			}
		}
		return 0;
	}

/*	int check(int y, int x){
		if (a[y + 1][x] == 1){
			a[y + 1][x] = 0;
			check(y + 1, x);
		}
		//-----------------------------
		if (a[y][x + 1] == 1){
			a[y][x + 1] = 0;
			check(y, x + 1);
		}
		//-----------------------------
		if (a[y - 1][x] == 1){
			a[y - 1][x] = 0;
			check(y - 1, x);
		}
		//-----------------------------
		if (a[y][x - 1] == 1){
			a[y][x - 1] = 0;
			check(y, x - 1);
		}
		//-----------------------------
		if (a[y + 1][x + 1] == 1){
			a[y + 1][x + 1] = 0;
			check(y + 1, x + 1);
		}
		//-----------------------------
		if (a[y + 1][x - 1] == 1){
			a[y + 1][x - 1] = 0;
			check(y + 1, x - 1);
		}
		//-----------------------------
		if (a[y - 1][x + 1] == 1){
			a[y - 1][x + 1] = 0;
			check(y - 1, x + 1);
		}
		//-----------------------------
		if (a[y - 1][x - 1] == 1){
			a[y - 1][x - 1] = 0;
			check(y - 1, x - 1);
		}
		//-----------------------------
		return 0;
	}*/

C++: #include <cstdio>
#include <utility>
#include <cmath>
#include <algorithm>
#include <iostream>
#include <sstream>
#include <iomanip>
#include <vector>
#include <string>
#define REP(i,n) for(int i=0;i<n;i++)
typedef long long ll;
using namespace std;
typedef vector<ll> vl;
typedef pair<ll,ll> pll;
typedef vector<pll> vpll;
typedef vector<string> vs;


int a[50][50]={};
int w,h;

void island(int x,int y)
{
	a[x][y]=0;
	for(int i=-1;i<2;i++)
	{
		for(int j=-1;j<2;j++)
		{
			if( y+j<0||y+j>h-1||x+i>w-1||x+i<0)
			{
				continue;
			}
			if(a[x+i][y+j]==1)
			{
				island(x+i,y+j);
			}
		}
	}
}

int main() 
{
	while(cin>>w>>h,!(w==0&&h==0))
	{
		REP(i,h){REP(j,w){cin>>a[j][i];}}
		int ans(0);
		REP(i,h){REP(j,w){if(a[j][i]==1){island(j,i);ans++;}}}
		cout << ans << endl;
	}
	return 0;
}

C++: #include <iostream>
#include <vector>
#include <queue>
using namespace std;
#define MAX_H 55
#define MAX_W 55

int W, H;
int c[MAX_H][MAX_W];

void dfs(int y, int x){

    c[y][x] = 0;
    for(int i=-1; i<=1; i++){
        for(int j=-1; j<=1; j++){

            int ny = y + i;
            int nx = x + j;
            if(ny >= 0 && ny < H && nx >= 0 && nx < W){
                if(c[ny][nx] == 1){
                    dfs(ny, nx);
                }
            }
        }
    }
}
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++){
                if(c[i][j] == 1){
                    dfs(i, j);
                    ans++;
                }
            }
        }
        cout << ans << endl;
    }
    return 0;
}

C++: #include <bits/stdc++.h>

#define ll long long

using namespace std;

int w,h;

int f[100][100]={};
int cf[100][100] = {};
int LUT[1000] ={};



int main(){

    while(1){
        scanf("%d %d",&w,&h);
        if(w == 0 && h==0)break;

        for(int i=0;i<h;i++){
            for(int j=0;j<w;j++){
                scanf("%d",&f[i][j]);
            }
        }
        int ans = 0;
        for(int i=0;i<h;i++){
            for(int j=0;j<w;j++){
                if(f[i][j] == 1 && cf[i][j]==0){
                    ans++;
                    LUT[ans]=ans;
                    cf[i][j] = ans;
                }
                
                for(int k=-1;k<2;k++){
                    if(i+k<0 || h <= i+k)continue;
                    for(int t=-1;t<2;t++){
                        if(j+t<0 || w <= j+t)continue;
                        if(f[i][j] == 1 && f[i+k][j+t]==1 ){
                            if(cf[i+k][j+t]==0){
                                cf[i+k][j+t]= cf[i][j];
                            }else if(cf[i+k][j+t] > cf[i][j]){
                                LUT[cf[i+k][j+t]] = cf[i][j];
                            }
                        }
                    }
                }
            }
        }
        for(int i=0;i<1000;i++){
            if(LUT[i] != 0 && LUT[i] != i){
                ans--;
            }
        }
        cout << ans << endl;
    }

}

C++: #include<stdio.h>
int main(){
  int w,h,a[51][51],i,j,i2,j2,c,k;
  while(1){
    //syokika
    for(i=0;i<51;i++){
      for(j=0;j<51;j++){
	a[i][j]=0;
      }
    }
    c=2;
  
    scanf("%d%d",&w,&h);
    if(w==0&&h==0)break;
    for(i=0;i<h;i++){
      for(j=0;j<w;j++){
	scanf("%d",&a[i][j]);
      }
    }
    for(i=0;i<h;i++){
      for(j=0;j<w;j++){
	if(a[i][j]==1){
	  a[i][j]=c;
	  k=1;
	  while(k!=3){
	    k=1;
	    //go
	    for(i2=0;i2<h;i2++){
	      for(j2=0;j2<w;j2++){
		//someru
		if(a[i2][j2]==1){
		  if(j2!=0){
		    if(a[i2][j2-1]==c){
		      a[i2][j2]=c; //left
		      k=2;
		    }
		  }
		  if(i2!=0){
		    if(a[i2-1][j2]==c){
		      a[i2][j2]=c; //up
		      k=2;
		    }		    

		    if(j2!=0){
		      if(a[i2-1][j2-1]==c){
			a[i2][j2]=c; //left up
			k=2;
		      }
		    }
		    if(a[i2-1][j2+1]==c){
		      a[i2][j2]=c; //right up
		      k=2;
		    }
		    
		  }
		}
		//some end
	     
	      }
	    }

	    //back
	    for(i2=h-1;i2>=0;i2--){
	      for(j2=w-1;j2>=0;j2--){
		//someru
		if(a[i2][j2]==1){
		  if(a[i2][j2+1]==c){
		    a[i2][j2]=c; //right
		    k=2;
		  }    
		  if(a[i2+1][j2]==c){
		    a[i2][j2]=c; //down
		    k=2;
		  }		
		  if(a[i2+1][j2+1]==c){
		    a[i2][j2]=c; //right do
		    k=2;
		  }
		  if(j2!=0){
		    if(a[i2+1][j2-1]==c){
		      a[i2][j2]=c; //left do
		      k=2;
		    }
		  }
		}
		
		
		//some end
		
	      }
	    }
	    if(k==1)k=3;
	    //sima end
	  }
	  c++;
	}
      }
    }
    printf("%d\n",c-2);
  }
  return 0;
}