#include #include #include #include #define repeat(i,n) for (int i = 0; (i) < (n); ++(i)) #define repeat_from(i,m,n) for (int i = (m); (i) < (n); ++(i)) #define repeat_reverse(i,n) for (int i = (n)-1; (i) >= 0; --(i)) using namespace std; double por(double p, double q) { // probability or return 1 - (1-p)*(1-q); } int main() { int h, w; cin >> h >> w; vector > p(h, vector(w)); repeat (y,h) repeat (x,w) { cin >> p[y][x]; p[y][x] /= 100; } vector > s(h, vector(w)); repeat (y,h) repeat (x,w) cin >> s[y][x]; vector > dp(h, vector(w)); repeat (y,h) { array,3> q; fill(q.begin(), q.end(), vector(w)); repeat (x,w) { if (s[y][x] == 0) { q[0][x] = p[y][x]; } if (s[y][x] == 1 and y-1 >= 0) { q[0][x] = p[y][x] * dp[y-1][x];; } } vector l(w); repeat_from (x,1,w) { if (s[y][x] == 1) { l[x] = por(l[x], p[y][x] * por(l[x-1], q[0][x-1])); } if (s[y][x] == 2 and y-1 >= 0) { l[x] = p[y][x] * dp[y-1][x] * por(l[x-1], q[0][x-1]); } } vector r(w); repeat_reverse (x,w-1) { if (s[y][x] == 1) { r[x] = por(r[x], p[y][x] * por(r[x+1], q[0][x+1])); } if (s[y][x] == 2 and y-1 >= 0) { r[x] = p[y][x] * dp[y-1][x] * por(r[x+1], q[0][x+1]); } } repeat (x,w) { q[1][x] = por(q[0][x], por(l[x], r[x])); } q[2] = q[1]; repeat (x,w) { if (s[y][x] == 2 and 0 <= x-1 and x+1 < w) { q[2][x] = por(q[2][x], p[y][x] * q[1][x-1] * q[1][x+1]); } if (s[y][x] == 3 and 0 <= x-1 and x+1 < w and y-1 >= 0) { q[2][x] = por(q[2][x], p[y][x] * q[1][x-1] * q[1][x+1] * dp[y-1][x]); } } dp[y] = q[2]; } double result = 0; repeat (y,h) repeat (x,w) result += dp[y][x]; cout << result << endl; return 0; }