#include <bits/stdc++.h>
#define LL long long
#define pb push_back
#define st first
#define nd second
#define INF 0x3f3f3f3f
#define LINF 0x3f3f3f3f3f3f3f3f
template <class T> T read(T &a) {
    a=0;char x=getchar();bool f=0;
    for(;x<'0'||x>'9';x=getchar())f|=x=='-';
	for(;x>='0'&&x<='9';x=getchar())a=(a<<3)+(a<<1)+x-'0';
    if(f)a=-a;
    return a;
}
using namespace std;

LL MOD = 998244353;
const int N = 55;

int p[N];
LL d[N][N], cnt[N];
LL pre[N];

int find(int a) {
    return p[a] == a? a: p[a] = find(p[a]);
}

int main() {
    int n, m; 
    read(n), read(m);
    pre[0] = 1;
    for (int i = 1; i <= 50; ++i) {
        pre[i] = pre[i - 1] * i % MOD;
        p[i] = i;
    }
    for (int i = 1; i <= n; ++i) {
        for (int j = 1; j <= n; ++j) {
            read(d[i][j]);
        }
    }
    for (int i = 1; i <= n; ++i) {
        for (int j = 2; j <= n; ++j) {
            bool ok = 1;
            for (int k = 1; k <= n; ++k) {
                if (d[i][k] + d[j][k] > m) {
                    ok = 0;
                    break;
                }
            }
            if (ok) {
                int a = find(i), b = find(j);
                p[a] = b;
            }
        }
    }
    LL x = 1;
    for (int i = 1; i <= n; ++i) {
        cnt[find(i)]++;
    }
    for (int i = 1; i <= n; ++i) {
        x = x * pre[cnt[i]] % MOD;
    }
    for (int i = 1; i <= n; ++i) {
        p[i] = i;
    }
    memset(cnt, 0, sizeof cnt);
    for (int i = 1; i <= n; ++i) {
        for (int j = 2; j <= n; ++j) {
            bool ok = 1;
            for (int k = 1; k <= n; ++k) {
                if (d[k][i] + d[k][j] > m) {
                    ok = 0;
                    break;
                }
            }
            if (ok) {
                int a = find(i), b = find(j);
                p[a] = b;
            }
        }
    }
    for (int i = 1; i <= n; ++i) {
        cnt[find(i)]++;
    }
    for (int i = 1; i <= n; ++i) {
        x = x * pre[cnt[i]] % MOD;
    }
    printf("%lld\n", x);
	return 0;
}

#include <bits/stdc++.h>
#define SIZE 200005
using namespace std;
typedef long long int ll;
typedef pair <int,int> P;

struct BIT
{
	int bit[SIZE];
	
	void init()
	{
		memset(bit,0,sizeof(bit));
	}
	void add(int k,int x)
	{
		k++;
		while(k<SIZE)
		{
			bit[k]+=x;
			k+=k&-k;
		}
	}
	int get(int k)
	{
		k++;
		int ret=0;
		while(k>0)
		{
			ret+=bit[k];
			k-=k&-k;
		}
		return ret;
	}
	int get(int s,int t)
	{
		return get(t)-get(s-1);
	}
	int pos(int k)
	{
		int l=-1,r=SIZE;
		while(r-l>1)
		{
			int m=(l+r)/2;
			if(get(m)<k) l=m;
			else r=m;
		}
		return r;
	}
}bit;

vector <int> vec[SIZE];
bool zan[SIZE];
int main()
{
	int n,m,k;
	scanf("%d %d %d",&n,&m,&k);
	int M=m;
	for(int i=0;i<k;i++)
	{
		int x,y;
		scanf("%d %d",&x,&y);x--,y--;
		vec[x].push_back(y);
		if(x==0) M=min(M,y);
	}
	bit.init();
	for(int i=0;i<M;i++)
	{
		bit.add(i,1);
		zan[i]=true;
	}
	bool vert=true;
	ll ret=0;
	for(int i=0;i<n;i++)
	{
		int mn=m;
		for(int j=0;j<vec[i].size();j++)
		{
			if(vec[i][j]==0) vert=false;
			mn=min(mn,vec[i][j]);
			if(zan[vec[i][j]])
			{
				bit.add(vec[i][j],-1);
				zan[vec[i][j]]=false;
			}
		}
		if(vert)
		{
			ret+=mn;
			ret+=bit.get(mn,m);
		}
		else ret+=bit.get(0,m);
	}
	printf("%lld\n",ret);
}