Go: // AOJ 1160: How Many Islands? http://judge.u-aizu.ac.jp/onlinejudge/description.jsp?id=1160

package main

import (
	"bufio"
	"container/list"
	"fmt"
	"os"
	"strconv"
)

var SEA = 0
var LAND = 1

func nextInt(sc *bufio.Scanner) int {
	sc.Scan()
	v, _ := strconv.Atoi(sc.Text())
	return v
}

func discoverIslands(M, V [][]int, H, W int) int {
	// 島を探す
	queue := list.New()
	islands := 0
	for i := 1; i <= H; i++ {
		for j := 1; j <= W; j++ {
			// 訪問済みの土地ならスキップ
			if V[i][j] == 1 {
				continue
			}

			// 訪問中の土地が海なら訪問済みにしてスキップ
			if M[i][j] == SEA {
				V[i][j] = 1
				continue
			}

			// 未訪問の陸地が見つかったので
			// 島の数を1つ増やし、この陸地から辿れる陸地を訪問していく
			islands++
			queue.PushBack([]int{i, j})

			for queue.Len() > 0 {
				cur := queue.Remove(queue.Front()).([]int)
				h, w := cur[0], cur[1]
				if V[h][w] == 1 {
					continue
				}
				V[h][w] = 1

				// 全方向に未訪問の陸地がないか探索
				for v := -1; v <= 1; v++ {
					for l := -1; l <= 1; l++ {
						dh := h + v
						dw := w + l
						if M[dh][dw] == LAND && V[dh][dw] == 0 {
							queue.PushBack([]int{dh, dw})
						}
					}
				}
				/*
					if M[h-1][w] == LAND {
						queue.PushBack([]int{h - 1, w})
					}
					if M[h-1][w+1] == LAND {
						queue.PushBack([]int{h - 1, w + 1})
					}
					if M[h][w+1] == LAND {
						queue.PushBack([]int{h, w + 1})
					}
					if M[h+1][w+1] == LAND {
						queue.PushBack([]int{h + 1, w + 1})
					}
					if M[h+1][w] == LAND {
						queue.PushBack([]int{h + 1, w})
					}
					if M[h+1][w-1] == LAND {
						queue.PushBack([]int{h + 1, w - 1})
					}
					if M[h][w-1] == LAND {
						queue.PushBack([]int{h, w - 1})
					}
					if M[h-1][w-1] == LAND {
						queue.PushBack([]int{h - 1, w - 1})
					}
				*/
			}
		}
	}
	return islands
}

func main() {
	sc := bufio.NewScanner(os.Stdin)
	sc.Split(bufio.ScanWords)

	for {
		W, H := nextInt(sc), nextInt(sc)

		if W == 0 && H == 0 {
			break
		}
		// Initialize map and visted list
		M := make([][]int, H+2)
		V := make([][]int, H+2)
		for i := 0; i < H+2; i++ {
			M[i] = make([]int, W+2)
			V[i] = make([]int, W+2)
		}
		for i := 1; i <= H; i++ {
			for j := 1; j <= W; j++ {
				M[i][j] = nextInt(sc)
			}
		}

		islands := discoverIslands(M, V, H, W)

		fmt.Println(islands)
	}
}

C++: #ifndef _GLIBCXX_NO_ASSERT
#include <cassert>
#endif
#include <cctype>
#include <cerrno>
#include <cfloat>
#include <ciso646>
#include <climits>
#include <clocale>
#include <cmath>
#include <csetjmp>
#include <csignal>
#include <cstdarg>
#include <cstddef>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <ctime>

#if __cplusplus >= 201103L
#include <ccomplex>
#include <cfenv>
#include <cinttypes>
#include <cstdbool>
#include <cstdint>
#include <ctgmath>
#include <cwchar>
#include <cwctype>
#endif

// C++
#include <algorithm>
#include <bitset>
#include <complex>
#include <deque>
#include <exception>
#include <fstream>
#include <functional>
#include <iomanip>
#include <ios>
#include <iosfwd>
#include <iostream>
#include <istream>
#include <iterator>
#include <limits>
#include <list>
#include <locale>
#include <map>
#include <memory>
#include <new>
#include <numeric>
#include <ostream>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <stdexcept>
#include <streambuf>
#include <string>
#include <typeinfo>
#include <utility>
#include <valarray>
#include <vector>

#if __cplusplus >= 201103L
#include <array>
#include <atomic>
#include <chrono>
#include <condition_variable>
#include <forward_list>
#include <future>
#include <initializer_list>
#include <mutex>
#include <random>
#include <ratio>
#include <regex>
#include <scoped_allocator>
#include <system_error>
#include <thread>
#include <tuple>
#include <typeindex>
#include <type_traits>
#include <unordered_map>
#include <unordered_set>
#endif




#define int long long
#define rng(i,l,r) for(size_t i=(l);i<(r);++i)
#define rep(i,n) rng(i,0,n)
#define gnr(i,l,r) for(size_t i=(r)-1;i>=(l);i--)
#define per(i,b) gnr(i,0,b)
#define ALL(obj) (obj).begin(), (obj).end()   //1,2,3,...
#define rALL(obj) (obj).rbegin(), (obj).rend()//...,3,2,1

using namespace std;


//constant
const int MOD = 1e9+7;
const int INF = 1e18;
const int dx4[4] = {1, 0, -1, 0};
const int dy4[4] = {0, 1, 0, -1};
const int dx8[8] = {1,1,0,-1,-1,-1,0,1};
const int dy8[8] = {0,1,1,1,0,-1,-1,-1};
const double pi = acos(-1);


template<class T> inline bool chmin(T& a, T b) { if (a > b) { a = b; return true; } return false; }
template<class T> inline bool chmax(T& a, T b) { if (a < b) { a = b; return true; } return false; }

int gcd(int a,int b){return b?gcd(b,a%b):a;}
//*max_element(ALL(vector))
//count(ALL(vector),num) -> n
//memset(obj,value,sizeof(obj));
int H,W;


void dfs(int h,int w,int map[60][60],int seen[60][60]){
    
    if(seen[h][w]) return;
    
    seen[h][w] = true;
    
    for (int i = 0; i < 8; i++) {
        int nh = h+dy8[i];
        int nw = w+dx8[i];
        bool ishok = (0<=nh && nh<H);
        bool iswok = (0<=nw && nw<W);
        bool island= (map[nh][nw]);
        
        if(seen[nh][nw]) continue;
        if(!(ishok&&iswok&&island)) continue;
        
        dfs(nh,nw,map,seen);
        
    }
}

void solve(){
    
    // remove the bottom 3 lines when you submit this code.
    std::ifstream in("sample.txt");
    std::cin.rdbuf(in.rdbuf());

    cin.tie(0);
    ios::sync_with_stdio(false);
    
    
    while(cin >> W >> H){
        if(W==0&&H==0) break;
        int map[60][60]; memset(map,0,sizeof(map));
        int seen[60][60];memset(seen,false,sizeof(seen));
        for (int h = 0; h < H; h++) {for (int w = 0; w < W; w++) cin >> map[h][w];}
        
        int ans = 0;
        for (int h = 0; h < H; h++) {
            for (int w = 0; w < W; w++) {
                if(!map[h][w] || seen[h][w]) continue;
                dfs(h,w,map,seen);
                ans++;
            }
        }
        
        cout << ans << endl;
        
        
    }
    
}





signed main() {
    solve();
    return 0;
}

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

vector<pair<int, int>> bfs(vector<pair<int, int>> not_yet, vector<vector<int>> maze)
{
  pair<int, int> check = not_yet.at(0);
  queue<pair<int, int>> a;
  a.push(check);
  maze.at(check.first).at(check.second) = 0;
  while (!a.empty())
  {
    pair<int, int> now = a.front();
    a.pop();
    not_yet.erase(find(not_yet.begin(), not_yet.end(), now));
    vector<int> move = {1, 0, -1};
    for (int i = 0; i < 3; ++i)
    {
      for (int j = 0; j < 3; ++j)
      {
        if (i == 1 && j == 1)
        {
          continue;
        }
        if (maze.at(now.first + move.at(i)).at(now.second + move.at(j)) == 1)
        {
          a.push({now.first + move.at(i), now.second + move.at(j)});
          maze.at(now.first + move.at(i)).at(now.second + move.at(j)) = 0;
        }
      }
    }
  }
  return not_yet;
}

int main()
{
  while (true)
  {
    int w, h;
    cin >> w >> h;
    if (w == h && w == 0)
    {
      break;
    }
    vector<vector<int>> maze(h + 2, vector<int>(w + 2, -1));
    vector<pair<int, int>> not_yet(0);// h w
    for (int i = 1; i < h + 1; ++i)
    {
      for (int j = 1; j < w + 1; ++j)
      {
        int point;
        cin >> point;
        maze.at(i).at(j) = point;
        if (point == 1)
        {
          not_yet.push_back({i, j});
        }
      }
    }

    int ans = 0;
    while (not_yet.size() != 0)
    {
      not_yet = bfs(not_yet, maze);
      ans += 1;
    }
    cout << ans << endl;
  }
}