import std.stdio;
import core.bitop;
import std.c.stdio;
import std.range;
import std.array;
import std.functional;
import std.algorithm;
import std.conv;
import std.container;
import std.math;
import std.numeric;
import std.string;
import std.c.string;
import std.regex;
import std.typecons;

const M = 10;
alias int[M][M] F;

struct P {
    int y, x;
}

int m, n;
int[int][23] cache;
int[M][M] id;
int vCount;
P[] pos;
F cur;

int[] next(int v) {
    int[] ret;
    static const dy = [0, 1, 0, -1],
                 dx = [1, 0, -1, 0];
    foreach (i; 0 .. 4) {
        int y = pos[v].y, x = pos[v].x;
        while (true) {
            y += dy[i];
            x += dx[i];
            if (y < 0 || y >= n) break;
            if (x < 0 || x >= m) break;
            if (cur[y][x] == 2) {
                ret ~= id[y][x];
                continue;
            }
            if (cur[y][x] == 1) {
                ret ~= id[y][x];
                break;
            }
        }
    }
    return ret;
}

int dfs(int v, int used) {
    int popCount = used.popcnt;
    if (popCount >= 3) {
        if (used in cache[v - 1]) return cache[v - 1][used];
    }
    int ans = 0;
    //writeln(pos[v], next(v).map!((i) => pos[i]));
    int[] z = next(v);
    foreach (i; z) {
        if (i == 0) {
            //writefln("%b", used);
            if (used == (1 << vCount - 1) - 1) ans++;
            continue;
        }
        if (used & (1 << i-1)) continue;
        int ny = pos[i].y,
            nx = pos[i].x;
        if (cur[ny][nx] == 1) {
            cur[ny][nx] = 3;
            ans += dfs(i, used | (1 << i-1));
            cur[ny][nx] = 1;
        } 
    }
    delete(z);
    if (popCount >= 3) {
        cache[v - 1][used] = ans;
    }
    return ans;
}

void main() {
    while (scanf("%d %d\n", &m, &n), m || n) {
        memset(cast(void*)id, -1, id.sizeof);
        vCount = 1;
        foreach (i; 0 .. n) {
            foreach (j; 0 .. m) {
                scanf("%d", &cur[i][j]);
                if (cur[i][j] == 1) {
                    id[i][j] = vCount++;
                }
                if (cur[i][j] == 2) {
                    id[i][j] = 0;
                }
            }
        }
        pos = new P[vCount];
        foreach (i; 0 .. n) {
            foreach (j; 0 .. m) {
                if (id[i][j] >= 0) {
                    pos[id[i][j]] = P(i, j);
                }
            }
        }
        int ans = 0;
        foreach(i; next(0)) {
            foreach (j; 0 .. 23) cache[j] = null;
            cur[pos[i].y][pos[i].x] = 3;
            ans += dfs(i, 1 << i-1);
            cur[pos[i].y][pos[i].x] = 1;
        }
        ans.writeln;
    }
}