Go: ///
// File:  1160.go
// Author: ymiyamoto
//
// Created on Sun Mar  4 20:45:57 2018
//
package main

import (
	"fmt"
)

var w, h int
var graph [][]string

func dfs(visited [][]bool, x, y int) {
	visited[x][y] = true

	for i := -1; i <= 1; i++ {
		for j := -1; j <= 1; j++ {
			dx := x + i
			dy := y + j
			if 0 <= dx && dx < h && 0 <= dy && dy < w && graph[dx][dy] == "1" && !visited[dx][dy] {
				dfs(visited, dx, dy)
			}
		}
	}
}

func main() {
	for {
		fmt.Scan(&w, &h)
		if w == 0 && h == 0 {
			break
		}

		graph = make([][]string, h)
		for i := range graph {
			graph[i] = make([]string, w)
		}

		for i := 0; i < h; i++ {
			for j := 0; j < w; j++ {
				var s string
				fmt.Scan(&s)
				graph[i][j] = s
			}
		}

		visited := make([][]bool, h)
		for i := range visited {
			visited[i] = make([]bool, w)
		}

		count := 0
		for i := range graph {
			for j := range graph[i] {
				if graph[i][j] == "1" && !visited[i][j] {
					dfs(visited, i, j)
					count++
				}
			}
		}

		fmt.Println(count)
	}
}

Python: #!/usr/bin/env python
# -*- coding: utf-8 -*-

import sys
sys.setrecursionlimit(10**6)

def dfs(s,Map,H,W,c):
    dxdy = [(1,0),(-1,0),(0,1),(0,-1),(1,1),(-1,-1),(-1,1),(1,-1)]
    for i in range(len(dxdy)):
        Map[s[0]][s[1]] = c
        nextPos = (s[0]+dxdy[i][0],s[1]+dxdy[i][1])
        if nextPos[0] >= 0 and nextPos[1] >= 0 and nextPos[0] < H and nextPos[1] < W:
            if Map[nextPos[0]][nextPos[1]] == 1:
                dfs(nextPos,Map,H,W,c)

while True:
    W,H = map(int,input().split(" "))
    if W == 0 and H == 0:
        break
    Map = [list(map(int,input().split())) for i in range(0,H)]
    c = 1
    for i in range(H):
        for j in range(W):
            if Map[i][j] == 1:
                c += 1
                dfs((i,j),Map,H,W,c)
    print(c-1)

Python: import sys
sys.setrecursionlimit(10**7)
def dfs(i, j):
    if(c[i][j] != "1"):
        return 1
    else:
        c[i][j] = "2"

        if(0 < i):                      dfs(i - 1, j)
        if(0 < j):                      dfs(i, j - 1)
        if(i < h - 1):                  dfs(i + 1, j)
        if(j < w - 1):                  dfs(i, j + 1)
        if(0 < i and 0 < j):            dfs(i - 1, j - 1)
        if(0 < i and j < w - 1):        dfs(i - 1, j + 1)
        if(i < h - 1 and 0 < j):        dfs(i + 1, j - 1)
        if(i < h - 1 and j < w - 1):    dfs(i + 1, j + 1)
        return 1

ans=[]
while(True):
    islands=0
    w, h = map(int, input().split())
    if(w == 0 and h == 0):
        break
    c = []
    for i in range(h):
        c.append(input().split())
    for i in range(h):
        for j in range(w):
            if(c[i][j] == "1"):
                islands += dfs(i, j)
    ans.append(islands)

for i in range(len(ans)):
    print(ans[i])

Python: import sys

def landsearch(i,j):
    result=[]
    if i>0:
        if j>0:
            for x in range(-1,2):
                for y in range(-1,2):
                    try:
                        if (not (x==0 and y==0)) and atlas[i+x][j+y][0]==1:
                            result.append([i+x,j+y])
                    except:
                        pass
        else:
            for x in range(-1,2):
                for y in range(2):
                    try:
                        if (not (x==0 and y==0)) and atlas[i+x][j+y][0]==1:
                            result.append([i+x,j+y])
                    except:
                        pass
    elif j>0:
        for x in range(2):
            for y in range(-1,2):
                try:
                    if (not (x==0 and y==0)) and atlas[i+x][j+y][0]==1:
                        result.append([i+x,j+y])
                except:
                    pass
    else:
        for x in range(2):
            for y in range(2):
                try:
                    if (not (x==0 and y==0)) and atlas[i+x][j+y][0]==1:
                        result.append([i+x,j+y])
                except:
                    pass
    return result           


while True:
    w,h=[int(i) for i in input().split(" ")]
    if w==0 and h==0:
        sys.exit()


    atlas=[]
    for i in range(h):
        atlas.append([[int(j),0] for j in input().split(" ")])


    how_many_island=0
    for i in range(h):
        for j in range(w):
            if atlas[i][j][0]==1 and atlas[i][j][1]==0:
                how_many_island+=1
                dis_list=[[i,j]]
                while len(dis_list)!=0:
                    dis_list_cash=[]
                    for k in dis_list:
                        land=landsearch(k[0],k[1])
                        for l in land:
                            if atlas[l[0]][l[1]][1]==0:
                                atlas[l[0]][l[1]][1]=1
                                dis_list_cash.append([l[0],l[1]])
                    dis_list=dis_list_cash

    print(how_many_island)

Python: def solver():
    w, h = map(int, input().split())
    if w == 0 and h == 0:
        return None
    islands = set()
    counter = 2
    c = [[0] + [int(x) for x in input().split()] + [0]  for _ in range(h)]
    c = [[0 for _ in range(w+2)]] + c + [[0 for _ in range(w+2)]]
    for i in range(1, h+1):
        for j in range(1, w+1):
            if c[i][j] > 0:
                neighbour = {c[i-1][j-1], c[i-1][j], c[i-1][j+1], c[i][j-1]}
                if max(neighbour) > 1:
                    c[i][j] = max(neighbour)
                    if len(neighbour - {0}) > 1:
                        islands -= {max(neighbour)}
                else:
                    c[i][j] = counter
                    islands |= {counter}
                    counter += 1
    for row in c:
        print(row)
    print(islands)
    return len(islands)

outputs = []
while(True):
    ans = solver()
    if ans == None:
        break
    outputs.append(ans)
print('\n'.join(map(str, outputs)))

Python: move = [[0, 1], [0, -1], [-1, 0], [1, 0], [1, 1], [1, -1], [-1, -1], [-1, 1]]
from collections import deque
def main(w, h):
    c = [list(map(int, input().split())) for i in range(h)]
    ans = 0
    for y in range(h):
        for x in range(w):
            if c[y][x]:
                ans += 1
                q = deque()
                q.append((x, y))
                while q:
                    xx, yy = q.popleft()
                    for mx, my in move:
                        xm = xx + mx
                        ym = yy + my
                        if 0 <= xm < w and 0 <= ym < h:
                            if c[ym][xm]:
                                c[ym][xm] = 0
                                q.append((xm,ym))
    print(ans)


while 1:
    w, h = map(int, input().split())
    if w == h == 0:
        break
    main(w,h)

Python: import sys

# 再帰の深さの上限変更
sys.setrecursionlimit(10**4)

def DFS(v,visit):
  visit[v] = True
  for u in adj[v]:
    if visit[u] == False:
      DFS(u,visit)

while(1):
  W, H = map(int, input().split())
  if (W,H) == (0,0):# 入力終了
    sys.exit()
  
  C = []
  for _ in range(H):
    tmp = list(map(int, input().split()))
    C.append(tmp)

  # 縦横斜め8方向が島か探索し、隣接リストを作成する
  dx = [1,-1,0,0,1,-1,-1,1]
  dy = [0,0,1,-1,1,1,-1,-1]
  
  # 隣接リスト
  adj = [[] for i in range(H*W)]
  
  for i in range(H):
    for j in range(W):
      if C[i][j] == 1:# 陸である
        # 周りが陸か探索する
        tmp = []
        for k in range(8):
          x,y = j+dx[k], i+dy[k]
          if 0 <= y < H and 0 <= x < W:
            if C[y][x] == 1:
              tmp.append(W*y + x)
        adj[W*i+j] = tmp
  # print(adj)
        
  # 深さ優先探索を行う
  # 訪問チェック
  visit = [False for i in range(H*W)]
  
  ans = 0
  for i in range(H):
    for j in range(W):
      if C[i][j] == 1 and visit[W*i+j] == False:
        DFS(W*i+j,visit)
        ans += 1
  print(ans)