### constant

DRCS = [[1, 0], [-1, 0], [0, 1], [0, -1]]

### subroutines

def reachable?(y0, x0, y1, x1)
  dy = (y1 <=> y0)
  dx = (x1 <=> x0)

  return false if dy != 0 && dx != 0

  if dy == 0
    x0 += dx
    while x0 != x1
      return false if ! $pass[y0][x0]
      x0 += dx
    end
  else
    y0 += dy
    while y0 != y1
      return false if ! $pass[y0][x0]
      y0 += dy
    end
  end

  true
end

def count_rt(k, y, x)
  if k == 0
    return (reachable?(y, x, $sy, $sx) ? 1 : 0)
  end

  count = 0

  for dxy in DRCS
    dy, dx = dxy
    y0 = y + dy
    x0 = x + dx

    while y0 >= 0 && y0 < $n && x0 >= 0 && x0 < $m && $pass[y0][x0]
      if $sects[y0][x0] == 1
        $pass[y0][x0] = false
        count += count_rt(k - 1, y0, x0)
        $pass[y0][x0] = true
      end

      y0 += dy
      x0 += dx
    end
  end

  count
end

### main

loop do
  $m, $n = gets.strip.split(' ').map{|s| s.to_i}
  break if ($m | $n) == 0

  $sects = $n.times.map{[]}
  $sy = $sx = -1
  home = 0

  for y in (0...$n)
    hl = gets.strip.split(' ').map{|s| s.to_i}
    for x in (0...$m)
      $sects[y][x] = hl[x]

      case hl[x]
      when 1
        home += 1
      when 2
        $sy = y
        $sx = x
      end
    end
  end

  $pass = $n.times.map{$m.times.map{true}}

  puts count_rt(home, $sy, $sx)
end