output_description: For each dataset, output the number of the islands in a line. No extra
  characters should occur in the output.
input_description: The input consists of a series of datasets, each being in the following
  format.
  
  > _w h_  
  >  _c_ 1,1 _c_ 1,2 ... _c_ 1,_w_  
  >  _c_ 2,1 _c_ 2,2 ... _c_ 2,_w_  
  >  ...  
  >  _c_ _h_ ,1 _c_ _h_ ,2 ... _c_ _h_ ,_w_  
  >
  
  _w_ and _h_ are positive integers no more than 50 that represent the width and
  the height of the given map, respectively. In other words, the map consists of
  _w_ × _h_ squares of the same size. _w_ and _h_ are separated by a single
  space.
  
  _c_ _i_ , _j_ is either 0 or 1 and delimited by a single space. If _c_ _i_ ,
  _j_ = 0, the square that is the _i_ -th from the left and _j_ -th from the top
  on the map represents a sea area. Otherwise, that is, if c _i_ , _j_ = 1, it
  represents a land area.
  
  The end of the input is indicated by a line containing two zeros separated by
  a single space.
attempt: #!/usr/bin/env python3
  # -*- coding: utf-8 -*-
  
  import array
  import itertools
  import sys
  
  
  class RecursiveDepthFirstSearch(object):
  
      def __init__(self, width, height, stage):
          self.width = width
          self.height = height
          self.stage = stage
  
      def search(self, x0, y0):
          if x0 < 0 or self.width <= x0 or y0 < 0 or self.height <= y0:
              pass
          elif self.stage[x0][y0] == 0:
              pass
          else:
              self.stage[x0][y0] = 0
              for dx, dy in itertools.product(range(-1, 2), range(-1, 2)):
                  if dx != 0 or dy != 0:
                      self.search(x0 + dx, y0 + dy)
  
  
  if __name__ == "__main__":
      sys.setrecursionlimit(10000)
      while True:
          w, h = map(int, input().split())
          if w == 0 and h == 0:
              break
          else:
              stage = [
                  array.array("B", x)
                  for x in zip(*[list(map(int, input().split())) for y in range(h)])
              ]
              dfs = RecursiveDepthFirstSearch(w, h, stage)
              answer = 0
              for x, y in itertools.product(range(w), range(h)):
                  if dfs.stage[x][y] == 1:
                      dfs.search(x, y)
                      answer += 1
              print(answer)
  
problem_description: B: How Many Islands?
  
  You are given a marine area map that is a mesh of squares, each representing
  either a land or sea area. Figure B-1 is an example of a map.
  
  ![](https://judgeapi.u-aizu.ac.jp/resources/images/IMAGE1_2009B2)  
  Figure B-1: A marine area map
  
  You can walk from a square land area to another if they are horizontally,
  vertically, or diagonally adjacent to each other on the map. Two areas are on
  the same island if and only if you can walk from one to the other possibly
  through other land areas. The marine area on the map is surrounded by the sea
  and therefore you cannot go outside of the area on foot.
  
  You are requested to write a program that reads the map and counts the number
  of islands on it. For instance, the map in Figure B-1 includes three islands.
samples: [{"input": "1\n    0\n    2 2\n    0 1\n    1 0\n    3 2\n    1 1 1\n    1 1 1\n    5 4\n    1 0 1 0 0\n    1 0 0 0 0\n    1 0 1 0 1\n    1 0 0 1 0\n    5 4\n    1 1 1 0 1\n    1 0 1 0 1\n    1 0 1 0 1\n    1 0 1 1 1\n    5 5\n    1 0 1 0 1\n    0 0 0 0 0\n    1 0 1 0 1\n    0 0 0 0 0\n    1 0 1 0 1\n    0 0", "output": "1\n    1\n    3\n    1\n    9"}]