#include<bits/stdc++.h>

using namespace std;

int matrix[50][50];
int n, k;
long long MOD =  998244353LL;
vector<int> comp;

long long fact (long long i) {
    long long ans = 1;
    while (i > 1ll) {
        ans = (i * ans) % MOD;
        i--;
    }
    return ans;
}

void update (vector<int>* g) {

    bool vis[n];
    memset(vis, 0, sizeof vis);


    for (int i = 0; i < n; i++) {
        if (vis[i]) { continue; }
        int cont = 0;
        queue<int> q;
        q.push(i);
        vis[i] = true;
        while (!q.empty()) {
            int x = q.front(); q.pop();
            cont++;
            for (int &y: g[x]) {
                if (vis[y]) { continue; }
                vis[y] = true;
                q.push(y);
            }
        }
        comp.push_back(cont);
    }

}

int main () {

    scanf("%d %d", &n, &k);

    for (int i = 0; i < n; i++) {
        for (int j = 0; j < n; j++) {
            scanf("%d", &matrix[i][j]);
        }
    }
    vector<int> g_rows[n];
    vector<int> g_cols[n];
    for (int i = 0; i < n; i++) {
	      for (int j = i + 1; j < n; j++) {
            bool faro_row = true, faro_col = true;
            for (int l = 0; l < n; l++) {
                if (matrix[i][l] + matrix[j][l] > k) {
                    faro_row = false;
                }
                if (matrix[l][i] + matrix[l][j] > k) {
                    faro_col = false;
                }
            }
            if (faro_row) {
                g_rows[i].push_back(j);
                g_rows[j].push_back(i);
            }
            if (faro_col) {
                g_cols[i].push_back(j);
                g_cols[j].push_back(i);
            }
        }
    }

    update(g_rows);
    update(g_cols);
    
    long long ans = 1;
    for (int& i: comp) {
        ans = (ans * fact(i * 1ll)) % MOD;
    }

    printf("%lld\n", ans);

    return 0;
}

#include<bits/stdc++.h>
#define PI 3.14159265358979323846264338327950288419716939937510 
#define eps 1e-15
using namespace std;
typedef long long ll;
typedef long double db;
typedef pair<int,int> pii;
typedef pair<ll,ll> pll;
typedef pair<db,db> pdd;
int vis[1005][1005],n,m;
char c[1005][1005];
vector<int> a[1005],b[1005];
void bfs(int sx,int sy){
    queue<pii> q;
    q.push({sx,sy});
    vis[sx][sy]=1;
    while(q.size()){
        int x=q.front().first,y=q.front().second;q.pop();
        for(auto j:a[x]){
            q.push({x,j});vis[x][j]=1;
        }
        for(auto j:b[y]){
            q.push({j,y});vis[j][y]=1;
        }
        a[x].clear();b[y].clear();
    }
}
int main(){
    cin>>n>>m;
    for(int i=1;i<=n;i++){
        for(int j=1;j<=m;j++){
            cin>>c[i][j];
        }
    }
    c[1][1]='#';c[1][m]='#';
    c[n][1]='#';c[n][m]='#';
    int h=0,s=0;
    for(int i=1;i<=n;i++){
        int t=0;
        for(int j=1;j<=m;j++){
            if(c[i][j]=='#'){t=1;break;}
        }
        h+=t;
    }
    for(int i=1;i<=m;i++){
        int t=0;
        for(int j=1;j<=n;j++){
            if(c[j][i]=='#'){t=1;break;}
        }
        s+=t;
    }
    int num=min(m-s,n-h),v=0;
    for(int i=1;i<=n;i++){
        for(int j=1;j<=m;j++){
            if(c[i][j]=='#'){
                a[i].push_back(j);
                b[j].push_back(i);
            }
        }
    }
    for(int i=1;i<=n;i++){
        for(int j=1;j<=m;j++){
            if(vis[i][j]) continue;
            if(c[i][j]!='#') continue;
            bfs(i,j);v++;
        }
    }
    cout<<num+v-1<<endl;
}
