#include <bits/stdc++.h>
using namespace std;
using ll = long long;
void solve() {
int n, m;
cin >> n >> m;
vector<string> s(n + 1);
for (int i = 1; i <= n; i++) {
cin >> s[i];
}
if (n <= 1e4) {
vector cnt(n + 1, vector<int>(n + 1));
for (int j = 0; j < m; j++) {
int lst = n;
for (int i = n; i >= 1; i--) {
if (i == n || s[i][j] != s[i + 1][j]) {
lst = i;
}
cnt[i][lst]++;
}
}
for (int i = 1; i <= n; i++) {
for(int j = n - 1; j >= i; j--) {
cnt[i][j] += cnt[i][j + 1];
}
}
int k, x, y, z, a = 1, b = n;
cin >> k >> x >> y >> z;
ll ans = 0;
for (int i = 1; i <= k; i++) {
int l = min(a, b), r = max(a, b);
int res = cnt[l][r];
a = (1ll * a * x + 1ll * res * y + z) % n + 1;
b = (1ll * b * y + 1ll * res * z + x) % n + 1;
ans += res;
}
cout << ans << '\n';
} else {
vector<bitset<100>> val(n + 1);
for (int i = 1; i <= n; i++) {
for (int j = 0; j < m; j++) {
val[i][j] = s[i][j] - '0';
}
}
vector And(20, vector<bitset<100>>(n + 1));
vector Or(20, vector<bitset<100>>(n + 1));
for (int i = 1; i <= n; i++) {
And[0][i] = Or[0][i] = val[i];
}
for (int i = 1; i < 20; i++) {
for (int j = 1; j + (1 << i) - 1 <= n; j++) {
And[i][j] = And[i - 1][j] & And[i - 1][j + (1 << i - 1)];
Or[i][j] = Or[i - 1][j] | Or[i - 1][j + (1 << i - 1)];
}
}
vector<int> Lg(n + 1);
Lg[0] = -1;
for (int i = 1; i <= n; i++) Lg[i] = Lg[i >> 1] + 1;
int k, x, y, z, a = 1, b = n;
cin >> k >> x >> y >> z;
ll ans = 0;
for (int i = 1; i <= k; i++) {
int l = min(a, b), r = max(a, b), k = Lg[r - l + 1];
auto t1 = (And[k][l] & And[k][r - (1 << k) + 1]);
auto t2 = (Or[k][l] | Or[k][r - (1 << k) + 1]);
auto t3 = t1 ^ t2;
int res = m - (t3.count());
a = (1ll * a * x + 1ll * res * y + z) % n + 1;
b = (1ll * b * y + 1ll * res * z + x) % n + 1;
= ans += res;
}
cout << ans << '\n';
}
}
signed main() {
ios::sync_with_stdio(false);
cin.tie(0), cout.tie(0);
int t = 1;
// cin >> t;
while(t--) solve();
return 0;
}