public int median(int i, int j) {
        int m, n, count, t[], tmp;

        //--- Browse Kernel Matrix ---//
        t = new int[9];
        for (m = i - 1, count = 0; m <= i + 1; m++) {
            for (n = j - 1; n <= j + 1; n++) {
                if (m >= 0 && m < nrows && n >= 0 && n < ncols) {
                    t[count++] = image1[m][n];
                }
            }
        }

        //--- Apply Bubble Sort ---//
        for (m = 0; m < count - 1; m++) {
            for (n = m + 1; n < count; n++) {
                if (t[m] < t[n]) {
                    tmp = t[m];
                    t[m] = t[n];
                    t[n] = tmp;
                }
            }
        }
        return t[count / 2];
    }