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>
using namespace std;

int ans,w,h,I[55][55];//??????

void dfs(int x,int y){
	I[x][y]=0;
	for(int i=-1;i<=1;i++){
		for(int j=-1;j<=1;j++){
			if(I[x+i][y+j]==1)dfs(x+i,y+j);
		}
	}
}
int main(){
	while(1){
		cin>>w;
		cin>>h;
		if(w==0&&h==0)break;
		ans=0;
		for(int i=0;i<55;i++){
			for(int j=0;j<55;j++){
				I[i][j]=0;
			}
		}
		for(int i=1;i<=h;i++){
			for(int j=1;j<=w;j++){
				cin>>I[i][j];
			}
		}
		for(int i=1;i<=h;i++){
			for(int j=1;j<=w;j++){
				if(I[i][j]==1){
					dfs(i,j);
					ans++;
				}
			}
		}
		cout<<ans<<endl;
	}
	return 0;
}

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

using namespace std;

int w, h;
int cnt;
vector<vector<int>> vvi;

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

int main()
{
    while (cin >> w >> h, w|h) {
        // input
        vvi.clear();
        cnt = 0;
        for (int j = 0; j < h; j++) {
            vector<int> vi;
            for (int i = 0; i < w; i++) {
                int c;
                cin >> c;
                vi.push_back(c);
            }
            vvi.push_back(vi);
        }

        // dfs
        for (int y = 0; y < h; y++) {
            for (int x = 0; x < w; x++) {
                if (vvi[y][x]) {
                    dfs(x, y);
                    cnt++;
                }
            }
        }

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

C++: // http://judge.u-aizu.ac.jp/onlinejudge/description.jsp?id=1160&lang=jp
// ????????¶????????£???????????????????????????????????°???????????£?????????
#include <bits/stdc++.h>
using namespace std;
typedef pair<int, int> P;
int w, h, cnt;
int c[50][50], m[50][50];
int dx[8] = {-1, 0, 1, -1, 1, -1, 0, 1};
int dy[8] = {-1, -1, -1, 0, 0, 1, 1, 1};
int main() {
    while(scanf("%d%d",&w,&h)) {
        if (w==0&&h==0) break;
        memset(m, -1, sizeof(m)); cnt = 0;
        for (int i=0; i<h; i++) for (int j=0; j<w; j++) scanf("%d",&c[i][j]);
        for (int i=0; i<h; i++) for (int j=0; j<w; j++) {
            if (m[i][j] == -1 && c[i][j] == 1) {
                cnt++;
                queue<P> q;
                q.push(P(i,j));
                while(!q.empty()) {
                    P p = q.front(); q.pop();
                    m[p.first][p.second] = 0;
                    for (int k=0; k<8; k++) {
                        int ny = p.first + dy[k];
                        int nx = p.second + dx[k];
                        if (nx >= 0 && nx < w && ny >= 0 && ny < h && m[ny][nx] == -1 && c[ny][nx] == 1) {
                            q.push(P(ny,nx));
                        }
                    }
                }
            }
        }
        printf("%d\n",cnt);
    }
}

C++: #include<iostream>

using namespace std;

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

void rec(int x,int y){
  grid[y][x]=false;
  for(int i=-1;i<2;i++){
    for(int j=-1;j<2;j++){
      int ny=y+i,nx=x+j;
      if(0<=nx && nx<w && 0<=ny && ny<h && grid[ny][nx])
	rec(nx,ny);
    }
  }
}

int main(void){

  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])rec(j,i),ans++;
  
    cout << ans << endl;
  }
  return 0;
}

C++: #include<iostream>

using namespace std;

int h, w, map[51][51];

const int vx[] = {1,0,-1,0}, vy[] = {0,1,0,-1};

void island(int _w, int _h) {
	map[_w][_h] = 2;
	for (int i = 0;i < 4;i++) {
		if (_w + vy[i] >= 1 && _w + vy[i] <= w&&_h + vx[i] >= 1 && _h + vx[i] <= h&&map[_w+vy[i]][_h+vx[i]] == 1) {
			island(_w + vy[i], _h + vx[i]);
		}
	}
}

int main() {
	while (cin >> w >> h, w + h) {
		for (int i = 1;i <= h;i++) {
			for (int j = 1;j <= w;j++) {
				cin >> map[i][j];
			}
		}
		int count = 0;
		for (int i = 1;i <= w;i++) {
			for (int j = 1;j <= h;j++) {
				if (map[i][j] == 1) {
					count++, island(i, j);
				}
			}
		}
		cout << count << endl;
		for (int i = 1;i <= w;i++)for (int j = 1;j <= h;j++)map[i][j] = -1;
	}
	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(i + dx, j + 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;
}

C++: #include <iostream>
#include <queue>
#include <utility>
using namespace std;
typedef pair<int, int> P;

const int dx[4] = {1, 0, -1, 0};
const int dy[4] = {0, 1, 0, -1};

int main()
{
    while(true){
        int w, h;
        cin >> w >> h;
        if(w == 0 && h == 0) break;
        int c[53][53];
        for(int j = 0; j < w + 2; j++){
            c[0][j] = c[h + 1][j] = 0;
        }
        for(int i = 1; i <= h; i++){
            c[i][0] = c[i][w + 1] = 0;
            for(int j = 1; j <= w; j++) cin >> c[i][j];
        }
        queue<P> que;
        int ans = 0;
        for(int i = 1; i <= h; i++){
            for(int j = 1; j <= w; j++){
                if(c[i][j] == 1){
                    c[i][j] = 0;
                    que.push(P(i, j));
                    while(que.size()){
                        int x = que.front().first;
                        int y = que.front().second;
                        que.pop();
                        for(int k = x - 1; k <= x + 1; k++){
                            for(int l = y - 1; l <= y + 1; l++){
                                if(c[k][l] == 1){
                                    c[k][l] = 0;
                                    que.push(P(k, l));
                                }
                            }
                        }
                    }
                    ans++;
                }
            }
        }
        cout << ans << endl;
    }
}