#include<bits/stdc++.h>
using namespace std;
const int N=5e5;
int dp[N+10][2];
char a[N+10],b[N+10];
bool xora[N+10],xorb[N+10];
int main(){
	int n;
	scanf("%d%s%s",&n,a+1,b+1);
	for(int i=1;i<=n;i++){
		xora[i]=xora[i-1]^(a[i]-'0');
		xorb[i]=xorb[i-1]^(b[i]-'0');
	}
	int l=1;
	long long ans=0;
	for(int i=1;i<=n;i++){
		l=max(l,i);
		if(xora[l]==xorb[i])continue;
		while(l<=n&&xora[l]!=xorb[i])l++;
		if(l>n){
			puts("-1");
			return 0;
		}
		ans+=l-i;
	}
	cout<<ans;
}
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef long double ld;
#define FOR(i, a, b) for(ll i = (ll)a; i <= (ll)b; i++)
#define DEC(i, a, b) for(ll i = (ll)a; i >= (ll)b; i--)
typedef pair<ll, ll> pi;
typedef pair<pi, ll> pii;
typedef pair<pi, pi> pipi;
#define f first
#define s second
typedef vector<ll> vi;
typedef vector<pi> vpi;
typedef vector<pii> vpii;
#define pb push_back
#define pf push_front
#define all(v) v.begin(), v.end()
#define disc(v) sort(all(v)); v.resize(unique(all(v)) - v.begin());
#define INF (ll) 1e9 + 100
#define LLINF (ll) 1e18
#define fastio ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0)
#define sandybridge __attribute__((optimize("Ofast"), target("arch=sandybridge")))
mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());    //can be used by calling rng() or shuffle(A, A+n, rng)
inline ll rand(ll x, ll y) { ++y; return (rng() % (y-x)) + x; } //inclusivesss

ll n, K, p[105], sz[105], arr[55][55], fac[55], ans = 1, mod = 998244353;

ll fs(ll x) {
	if (x == p[x]) return x;
	return p[x] = fs(p[x]);
}

int main() {
	fastio; cin >> n >> K;
	FOR(i, 1, n) FOR(j, 1, n) cin >> arr[i][j];
	FOR(i, 1, 2*n) p[i] = i, sz[i] = 1;
	fac[0] = 1; FOR(i, 1, n) fac[i] = (fac[i-1] * i) % mod;
	FOR(i, 1, n) FOR(j, 1, n) if (fs(i) != fs(j)) {
		bool ok = 1;
		FOR(k, 1, n) if (arr[i][k] + arr[j][k] > K) ok = 0;
		if (ok) {
			sz[fs(j)] += sz[fs(i)];
			p[fs(i)] = fs(j);
		}
	}
	FOR(i, 1, n) FOR(j, 1, n) if (fs(n+i) != fs(n+j)) {
		bool ok = 1;
		FOR(k, 1, n) if (arr[k][i] + arr[k][j] > K) ok = 0;
		if (ok) {
			sz[fs(n+j)] += sz[fs(n+i)];
			p[fs(n+i)] = fs(n+j);
		}
	}
	FOR(i, 1, 2*n) if (fs(i) == i) ans = (ans * fac[sz[i]]) % mod;
	cout << ans;
}
