#include <bits/stdc++.h>
using namespace std;
#define int long long
#define double long double
#define IOS ios_base::sync_with_stdio(0);cin.tie(0)
/* start */
 
const int N = 52;
const int mod = 998244353;

int g[N][N], tot;
int was[N];
int fat[N];
vector<int> vx[N], vy[N]; 

void dfs1(int v) {
   tot++;
   was[v]=1;
   for(int u : vx[v]) {
      if(!was[u]) {
         dfs1(u);
      }
   }
}

void dfs2(int v) {
   tot++;
   was[v]=1;
   for(int u : vy[v]) {
      if(!was[u]) {
         dfs2(u);
      }
   }
}

signed main(){
   IOS;
   fat[0] = fat[1] = 1;
   for(int i = 2; i < N; i++) {
      fat[i] = (fat[i-1] * i) % mod;
   }
   int n, K;
   cin >> n >> K;
   for(int i = 0; i < n; i++)
      for(int j = 0; j < n; j++)
         cin >> g[i][j];

   for(int i = 0; i < n; i++) {
      for(int j = i + 1; j < n; j++) {
         bool ok = true;
         for(int k = 0; k < n; k++) {
            if(g[k][i] + g[k][j] > K) {
               ok = false;
               break;
            }
         }
         if(ok) {
            vx[i].push_back(j);
            vx[j].push_back(i);
         }
      }      
   }
   int ans = 1;
   for(int i = 0; i < n; i++) {
      if(!was[i]) {
         tot = 0;
         dfs1(i);
         ans = (ans * fat[tot])%mod;
      }
   }

   for(int i = 0; i < n; i++) {
      for(int j = i + 1; j < n; j++) {
         bool ok = true;
         for(int k = 0; k < n; k++) {
            if(g[i][k] + g[j][k] > K) {
               ok = false;
               break;
            }
         }
         if(ok) {
            vy[i].push_back(j);
            vy[j].push_back(i);
         }
      }      
   }

   memset(was, 0, sizeof(was));

   for(int i = 0; i < n; i++) {
      if(!was[i]) {
         tot = 0;
         dfs2(i);
         ans = (ans * fat[tot])%mod;
      }
   }

   cout << ans << "\n";

}
 
/*
   Coisas pra se lembrar:
      * Overflow, tamanho do N
      * Casos especiais (e.g. n = 1)
      * Nao ficar parado e fazer alguma coisa
      * Escrever coisas no papel
      * NAO FICAR TRAVADO EM UMA IDEIA
*/
#include <bits/stdc++.h>
#ifndef ONLINE_JUDGE
#define debug(x) cout << #x << ": " << (x) << endl
#else
#define debug(x)
#endif
using namespace std;
typedef long long ll;
typedef vector<int> vi;
const int maxn=1e6+7,inf=0x3f3f3f3f,mod=1e9+7;

int fa[maxn];

int find(int x)
{
    return fa[x]==x?fa[x]:fa[x]=find(fa[x]);
}
string mp[maxn];

int main()
{
    ios::sync_with_stdio(false);
    cin.tie(0);
    int h,w;
    cin>>h>>w;
    for(int i=0;i<h;++i) cin>>mp[i];
    mp[0][0]='#',mp[0][w-1]='#',mp[h-1][0]='#',mp[h-1][w-1]='#';
    for(int i=0;i<=h+w;++i) fa[i]=i;
    for(int i=0;i<h;++i)
    {
        for(int j=0;j<w;++j)
        {
            if(mp[i][j]=='#')
            {
                int fx=find(i),fy=find(j+h);
                if(fx!=fy) fa[fy]=fx;
            }
        }
    }
    set<int>row,col;
    for(int i=0;i<h;++i) row.insert(find(i));
    for(int i=0;i<w;++i) col.insert(find(h+i));
    cout<<min(row.size(),col.size())-1<<'\n';
    return 0;
}
